網(wǎng)站色情營銷特點鄭州seo管理
在 LangChain解鎖LLM大語言模型的結(jié)構(gòu)化輸出能力:調(diào)用 with_structured_output() 方法 這篇博客中,我們了解了格式化LLM輸出內(nèi)容的必要性以及如何通過調(diào)用langchain框架中提供的 with_structured_output()
方法對LLM輸出進行格式化(三種可選方式:基于 TypedDict 類(類型化字典)、JSON Schema(JSON 模式)和 Pydantic 類)。在本篇博客中,我們將進一步學習了解對模型輸出進行結(jié)構(gòu)化控制的其他方案,分別是 少樣本示例引導(Few-shot prompting)、結(jié)構(gòu)化方法指定(Specifying the method for structuring outputs)和 直接解析模型輸出(Direct prompting and parsing)。
少樣本示例引導(Few-shot prompting)
當我們希望規(guī)范LLM輸出格式比較復雜的時候,模型的格式化輸出可能不會如預期穩(wěn)定,這個時候我們可以采用一個非常簡單高效的方法對模型輸出質(zhì)量的穩(wěn)定性進行控制,那就是 few-shot prompting(少樣本提示是一種在自然語言處理中,通過借助少量示例來引導語言模型生成預期輸出的技術(shù))。
代碼實現(xiàn)如下:
- 我們首先還是基于 Pydantic 的方式去定義LLM輸出的格式,然后調(diào)用
.with_structured_output()
方法創(chuàng)建structured_llm
變量;
from langchain_ollama import ChatOllama
from typing import Optional
from pydantic import BaseModel, Field
from langchain_core.prompts import ChatPromptTemplate# Pydantic
class Joke(BaseModel):"""Joke to tell user."""setup: str = Field(description="The setup of the joke")punchline: str = Field(description="The punchline to the joke")rating: Optional[int] = Field(default=None, description="How funny the joke is, from 1 to 10")llm = ChatOllama(model = "llama3.1:latest", temperature = 0.8)
structured_llm = llm.with_structured_output(Joke)
- 接著,我們使用
ChatPromptTemplate
從消息列表中創(chuàng)建一個提示詞模板,并且將幾個符合輸出格式的實例放入到 system prompt 中實現(xiàn) few-shot prompt,然后再通過|
管道操作將 prompt 傳遞給結(jié)構(gòu)化的語言模型進行處理(以下代碼中文注釋由 DeepSeek-R1 生成);
# 定義系統(tǒng)提示信息
# 告知語言模型它的角色是一個滑稽的喜劇演員,專長是敲敲門笑話
# 并說明了笑話的結(jié)構(gòu),即需要包含設(shè)置(對 "Who's there?" 的回應)和最終笑點(對 "<設(shè)置> who?" 的回應)
# 還給出了幾個關(guān)于不同主題的笑話示例,讓語言模型了解輸出的格式
system = """You are a hilarious comedian. Your specialty is knock - knock jokes. \
Return a joke which has the setup (the response to "Who's there?") and the final punchline (the response to "<setup> who?").Here are some examples of jokes:example_user: Tell me a joke about planes
example_assistant: {{"setup": "Why don't planes ever get tired?", "punchline": "Because they have rest wings!", "rating": 2}}example_user: Tell me another joke about planes
example_assistant: {{"setup": "Cargo", "punchline": "Cargo 'vroom vroom', but planes go 'zoom zoom'!", "rating": 10}}example_user: Now about caterpillars
example_assistant: {{"setup": "Caterpillar", "punchline": "Caterpillar really slow, but watch me turn into a butterfly and steal the show!", "rating": 5}}"""# 使用 ChatPromptTemplate 從消息列表中創(chuàng)建一個提示模板
# 消息列表包含一個系統(tǒng)消息和一個用戶消息
# 系統(tǒng)消息是上面定義的系統(tǒng)提示信息
# 用戶消息使用占位符 {input},表示后續(xù)可以傳入具體的用戶輸入,例如用戶想要的笑話主題
prompt = ChatPromptTemplate.from_messages([("system", system), ("human", "{input}")])# 將提示模板與結(jié)構(gòu)化的語言模型進行組合
# 這里將 prompt 和 structured_llm 進行管道操作(|)
# 意味著先根據(jù)提示模板處理輸入,然后將處理后的結(jié)果傳遞給結(jié)構(gòu)化的語言模型進行處理
few_shot_structured_llm = prompt | structured_llm
- 接著我們調(diào)用模型,讓LLM基于用戶提問進行符合預期的結(jié)構(gòu)化輸出。
# 調(diào)用組合后的模型,傳入用戶輸入 "what's something funny about woodpeckers"
# 表示用戶想要一個關(guān)于啄木鳥的有趣笑話
# 模型會根據(jù)系統(tǒng)提示中的要求和示例,生成一個符合格式的敲敲門笑話
response = few_shot_structured_llm.invoke("what's something funny about woodpeckers")
print(response)
setup='Woodpecker' punchline="They're always drumming up some laughter!" rating=8
結(jié)構(gòu)化方法指定(Specifying the method for structuring outputs)
其實還有一種調(diào)用 with_structured_output()
方法但只給 method
的參數(shù)傳遞變量的方式可以對LLM的輸出進行結(jié)構(gòu)化,我們先來看一下代碼實現(xiàn)(代碼中文注釋由 Doubao-1.5-pro-32k 生成):
# 從 langchain_ollama 庫中導入 ChatOllama 類
from langchain_ollama import ChatOllama# 創(chuàng)建一個 ChatOllama 實例
# model 參數(shù)指定要使用的模型,這里使用的是 "llama3.1:latest" 模型
# temperature 參數(shù)控制生成文本的隨機性,值越大越隨機,這里設(shè)置為 0.8
llm = ChatOllama(model="llama3.1:latest", temperature=0.8)# 為 llm 實例添加結(jié)構(gòu)化輸出功能
# 第一個參數(shù)傳入 None,表示不使用自定義的schema
# method 參數(shù)指定使用 "json_mode" 方法,即輸出為 JSON 格式
structured_llm = llm.with_structured_output(None, method="json_mode")# 調(diào)用結(jié)構(gòu)化的語言模型
# 傳入的提示信息是 "Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
# 意思是讓模型講一個關(guān)于貓的笑話,并以包含 `setup`(笑話的鋪墊)和 `punchline`(笑話的笑點)鍵的 JSON 格式響應
structured_llm.invoke("Tell me a joke about cats, respond in JSON with `setup` and `punchline` keys"
)
從以上代碼可以注意到,雖然我們調(diào)用的依舊是 with_structured_output()
方法,但第一個參數(shù)我們傳遞的是 None
,也就是我們并沒有傳入基于 TypedDict 類(類型化字典)、JSON Schema(JSON 模式)和 Pydantic 類 這三種方式聲明的schema,而是通過制定 method="json_mode"
,加上在用戶提問中特別說明的 “respond in JSON with setup
and punchline
keys”的方式實現(xiàn)對LLM輸出內(nèi)容的結(jié)構(gòu)化。當然對于輸出schema比較復雜的情況,這種方式的處理效果有待考量和驗證。
直接解析模型輸出(Direct prompting and parsing)
需要特別注意的是,并非所有模型都支持 .with_structured_output()
方法,因為并非所有模型都支持工具調(diào)用或 JSON 模式。那么對于這類模型,我們可以直接提示模型使用特定的格式,然后使用輸出解析器從模型的原始輸出中提取結(jié)構(gòu)化的響應內(nèi)容。以下結(jié)合代碼介紹兩種實現(xiàn)方法,分別是使用langchain 框架內(nèi)置的PydanticOutputParser
和自定義解析器。
使用 PydanticOutputParser
以下示例使用langchain內(nèi)置的 PydanticOutputParser
來解析LLM的輸出,即通過提示詞工程,直接將定義模型輸出的Pydantic 模式添加到system prompt中進行實現(xiàn)(本質(zhì)上就是提示詞工程),然后再對LLM的輸出內(nèi)容進行后置解析的思路進行實現(xiàn)。
實現(xiàn)代碼如下(中文注釋由 DeepSeek-R1 生成):
# 導入 List 類型提示,用于類型注解,表明變量將是一個列表
from typing import List# 從 langchain_core.output_parsers 模塊導入 PydanticOutputParser 類
# 該類用于將文本輸出解析為 Pydantic 模型實例
from langchain_core.output_parsers import PydanticOutputParser
# 從 langchain_core.prompts 模塊導入 ChatPromptTemplate 類
# 該類用于創(chuàng)建聊天提示模板
from langchain_core.prompts import ChatPromptTemplate
# 從 pydantic 模塊導入 BaseModel 和 Field 類
# BaseModel 是 Pydantic 模型的基類,Field 用于定義模型字段的元數(shù)據(jù)
from pydantic import BaseModel, Field# 定義一個名為 Person 的 Pydantic 模型類
# 該類用于表示一個人的信息
class Person(BaseModel):"""Information about a person."""# 定義 name 字段,為字符串類型# ... 表示該字段是必需的# description 為該字段提供描述信息name: str = Field(..., description="The name of the person")# 定義 height_in_meters 字段,為浮點數(shù)類型# 表示人的身高,單位為米# ... 表示該字段是必需的# description 為該字段提供描述信息height_in_meters: float = Field(..., description="The height of the person expressed in meters.")# 定義一個名為 People 的 Pydantic 模型類
# 該類用于表示文本中所有人物的識別信息
class People(BaseModel):"""Identifying information about all people in a text."""# 定義 people 字段,為 Person 類型的列表people: List[Person]# 創(chuàng)建一個 PydanticOutputParser 實例
# 將其 pydantic_object 屬性設(shè)置為 People 類
# 用于將文本輸出解析為 People 模型實例
parser = PydanticOutputParser(pydantic_object=People)# 創(chuàng)建一個聊天提示模板實例
# 使用 from_messages 方法從消息列表中創(chuàng)建模板
prompt = ChatPromptTemplate.from_messages([(# 系統(tǒng)消息,提示回答用戶查詢# 并將輸出用 `json` 標簽包裹# {format_instructions} 是一個占位符,將在后續(xù)填充解析指令"system","Answer the user query. Wrap the output in `json` tags\n{format_instructions}"),(# 用戶消息,{query} 是一個占位符,將在后續(xù)填充用戶的查詢內(nèi)容"human","{query}")]
).partial(format_instructions=parser.get_format_instructions())
我們來看一下將 user query傳入之后,構(gòu)成的完整prompt內(nèi)容是什么樣的。
query = "Anna is 23 years old and she is 6 feet tall"
print(prompt.invoke({"query": query}).to_string())
prompt
System: Answer the user query. Wrap the output in `json` tags
The output should be formatted as a JSON instance that conforms to the JSON schema below.As an example, for the schema {"properties": {"foo": {"title": "Foo", "description": "a list of strings", "type": "array", "items": {"type": "string"}}}, "required": ["foo"]}
the object {"foo": ["bar", "baz"]} is a well-formatted instance of the schema. The object {"properties": {"foo": ["bar", "baz"]}} is not well-formatted.Here is the output schema:
```
{"$defs": {"Person": {"description": "Information about a person.", "properties": {"name": {"description": "The name of the person", "title": "Name", "type": "string"}, "height_in_meters": {"description": "The height of the person expressed in meters.", "title": "Height In Meters", "type": "number"}}, "required": ["name", "height_in_meters"], "title": "Person", "type": "object"}}, "description": "Identifying information about all people in a text.", "properties": {"people": {"items": {"$ref": "#/$defs/Person"}, "title": "People", "type": "array"}}, "required": ["people"]}
```
Human: Anna is 23 years old and she is 6 feet tall
最后我們基于 prompt
, llm
和 parser
通過管道創(chuàng)建一個鏈式調(diào)用:
# 以下代碼注釋由 Doubao-1.5-pro-32k 生成# 使用 LangChain 提供的管道操作符 `|` 來創(chuàng)建一個鏈式調(diào)用。
# 這個鏈式調(diào)用的作用是將多個組件按順序連接起來,形成一個處理流程。
# 具體來說,這個鏈式調(diào)用包含三個組件:prompt、llm 和 parser。
# 1. prompt:這是之前創(chuàng)建的聊天提示模板實例。它的作用是根據(jù)用戶輸入的查詢內(nèi)容和解析指令生成合適的提示信息。
# 2. llm:這是一個語言模型實例,例如 OpenAI 的 GPT 系列模型等。它接收來自 prompt 生成的提示信息,然后根據(jù)這個提示信息生成相應的文本輸出。
# 3. parser:這是之前創(chuàng)建的 PydanticOutputParser 實例。它的作用是將 llm 生成的文本輸出解析為 People 模型實例,方便后續(xù)對輸出數(shù)據(jù)進行結(jié)構(gòu)化處理。
# 最終,chain 就是一個包含了提示生成、語言模型調(diào)用和輸出解析這三個步驟的處理鏈。
chain = prompt | llm | parser# 調(diào)用 chain 的 invoke 方法來執(zhí)行這個處理鏈。
# invoke 方法接收一個字典作為參數(shù),字典中的鍵 "query" 對應的值就是用戶輸入的查詢內(nèi)容。
# 處理鏈會按照之前定義的順序依次執(zhí)行各個組件:
# 首先,prompt 會根據(jù)傳入的查詢內(nèi)容和解析指令生成提示信息。
# 然后,這個提示信息會被傳遞給 llm,llm 基于提示信息生成文本輸出。
# 最后,parser 會將 llm 生成的文本輸出解析為 People 模型實例。
# 整個處理過程完成后,會返回經(jīng)過解析后的結(jié)構(gòu)化數(shù)據(jù),方便后續(xù)的業(yè)務(wù)邏輯使用。
chain.invoke({"query": query})
打印查看格式化后的輸出,完全符合 People
類中定義的schema。
People(people=[Person(name='Anna', height_in_meters=1.8288)])
自定義解析器
為了增加結(jié)構(gòu)化LLM輸出的靈活性,我們可以使用LangChain表達語言( LangChain Expression Language (LCEL)),來自定義prompt和解析器(通過創(chuàng)建一個函數(shù)的方式)。
import json
import re
from typing import List
from langchain_ollama import ChatOllama
from langchain_core.messages import AIMessage
from langchain_core.prompts import ChatPromptTemplate
from pydantic import BaseModel, Fieldllm = ChatOllama(model = "llama3.1:latest", temperature = 0.8)class Person(BaseModel):"""Information about a person."""name: str = Field(..., description="The name of the person")height_in_meters: float = Field(..., description="The height of the person expressed in meters.")class People(BaseModel):"""Identifying information about all people in a text."""people: List[Person]# 自定義prompt,我們通過system prompt,告知LLM要將輸出內(nèi)容wrap在<json></json>標簽中
prompt = ChatPromptTemplate.from_messages([("system","Answer the user query. Output your answer as JSON that ""matches the given schema: <json>\n{schema}\n</json>. ""Make sure to wrap the answer in <json> and </json> tags",),("human", "{query}"),]
).partial(schema=People.model_json_schema())
我們看一下傳入 user query后完整的prompt內(nèi)容:
query = "Anna is 23 years old and she is 6 feet tall"
print(prompt.format_prompt(query=query).to_string())
prompt
System: Answer the user query. Output your answer as JSON that matches the given schema: <json>
{'$defs': {'Person': {'description': 'Information about a person.', 'properties': {'name': {'description': 'The name of the person', 'title': 'Name', 'type': 'string'}, 'height_in_meters': {'description': 'The height of the person expressed in meters.', 'title': 'Height In Meters', 'type': 'number'}}, 'required': ['name', 'height_in_meters'], 'title': 'Person', 'type': 'object'}}, 'description': 'Identifying information about all people in a text.', 'properties': {'people': {'items': {'$ref': '#/$defs/Person'}, 'title': 'People', 'type': 'array'}}, 'required': ['people'], 'title': 'People', 'type': 'object'}
</json>. Make sure to wrap the answer in <json> and </json> tags
Human: Anna is 23 years old and she is 6 feet tall
通過創(chuàng)建名為 extract_json
的函數(shù)自定義 parser:
# Custom parser
def extract_json(message: AIMessage) -> List[dict]:"""Extracts JSON content from a string where JSON is embedded between <json> and </json> tags.Parameters:text (str): The text containing the JSON content.Returns:list: A list of extracted JSON strings."""text = message.content# Define the regular expression pattern to match JSON blockspattern = r"<json>(.*?)</json>"# Find all non-overlapping matches of the pattern in the stringmatches = re.findall(pattern, text, re.DOTALL)# Return the list of matched JSON strings, stripping any leading or trailing whitespacetry:return [json.loads(match.strip()) for match in matches]except Exception:raise ValueError(f"Failed to parse: {message}")
最后我們基于 prompt
, llm
和 自定義用來解析大模型輸出內(nèi)容的parser,通過管道創(chuàng)建一個鏈式調(diào)用:
chain = prompt | llm | extract_json
chain.invoke({"query": query})
調(diào)用 chain
后得到的結(jié)構(gòu)化輸出如下:
[{'$defs': {'Person': {'description': 'Information about a person.','properties': {'name': {'description': 'The name of the person','title': 'Name','type': 'string'},'height_in_meters': {'description': 'The height of the person expressed in meters.','title': 'Height In Meters','type': 'number'}},'required': ['name', 'height_in_meters'],'title': 'Person','type': 'object'}},'description': 'Identifying information about all people in a text.','properties': {'people': {'items': {'$ref': '#/$defs/Person'},'title': 'People','type': 'array'}},'required': ['people'],'title': 'People','type': 'object','people': [{'name': 'Anna', 'height_in_meters': 1.8288}]}]
但以上并非預期的輸出,預期輸出應該如下:
[{'people': [{'name': 'Anna', 'height_in_meters': 1.8288}]}]
至于原因目前還不清楚,初步猜測是因為選用的 llama3.1:latest
模型因為量化程度較高導致對帶輸出格式說明的system prompt的理解力不夠;當然也有可能是官方文檔里提供的示例代碼有些問題。(我在本地第一次運行的時候是報錯的,對代碼稍微調(diào)整了一下才fix了報錯的問題)