中文亚洲精品无码_熟女乱子伦免费_人人超碰人人爱国产_亚洲熟妇女综合网

當前位置: 首頁 > news >正文

網(wǎng)站色情營銷特點鄭州seo管理

網(wǎng)站色情營銷特點,鄭州seo管理,嘉興網(wǎng)站建設(shè)維護,競價推廣平臺有哪些在 LangChain解鎖LLM大語言模型的結(jié)構(gòu)化輸出能力:調(diào)用 with_structured_output() 方法 這篇博客中,我們了解了格式化LLM輸出內(nèi)容的必要性以及如何通過調(diào)用langchain框架中提供的 with_structured_output() 方法對LLM輸出進行格式化(三種可選方…

在 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)如下:

  1. 我們首先還是基于 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)
  1. 接著,我們使用 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
  1. 接著我們調(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, llmparser 通過管道創(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了報錯的問題)

http://www.risenshineclean.com/news/55616.html

相關(guān)文章:

  • 在百度做網(wǎng)站需要什么資料2023重大新聞事件10條
  • app網(wǎng)站維護廣州網(wǎng)站建設(shè)推薦
  • 網(wǎng)站優(yōu)化有前途嗎b站好看的紀錄片免費
  • 咸陽b2c網(wǎng)站制作價格ai智能營銷系統(tǒng)
  • 男女做暖暖到網(wǎng)站手機上怎么制作網(wǎng)頁
  • 網(wǎng)站舉報網(wǎng)怎樣申請自己的電商平臺
  • 建設(shè)一個政府部門網(wǎng)站商丘網(wǎng)站推廣公司
  • 字節(jié)跳動小程序開發(fā)平臺seo網(wǎng)站編輯是做什么的
  • wordpress加密文章班級優(yōu)化大師免費下載app
  • 做網(wǎng)站公司百度關(guān)鍵詞點擊器
  • 溫州網(wǎng)站建設(shè)優(yōu)化自己的品牌怎么做加盟推廣
  • 模板網(wǎng)站 怎么做優(yōu)化谷歌官方網(wǎng)站注冊
  • 三門峽網(wǎng)站seo優(yōu)化網(wǎng)站排名的方法
  • 網(wǎng)站制作鄭州網(wǎng)站制作yoast seo
  • 電商網(wǎng)站用php做的嗎游戲推廣賺傭金平臺
  • 手機網(wǎng)站開發(fā) 手機模擬器發(fā)帖推廣平臺
  • 海外建站服務(wù)平臺網(wǎng)絡(luò)營銷策略分析報告
  • 做網(wǎng)站公司賺不賺錢成都百度網(wǎng)站排名優(yōu)化
  • wordpress站內(nèi)搜索統(tǒng)計制作網(wǎng)頁的工具軟件
  • 軟件開發(fā)模型及其特點優(yōu)化神馬網(wǎng)站關(guān)鍵詞排名價格
  • wordpress會員瀏覽網(wǎng)站排名優(yōu)化推廣
  • 網(wǎng)站備案名稱的影響嗎網(wǎng)絡(luò)推廣優(yōu)化
  • 鉆井網(wǎng)站建設(shè)電商代運營一般收多少服務(wù)費
  • 網(wǎng)站開發(fā)需要哪些人員產(chǎn)品推廣軟件有哪些
  • 外國人可以在中國做網(wǎng)站嗎百度線上推廣
  • 北京市住房和城鄉(xiāng)建設(shè)委員會網(wǎng)站6優(yōu)化seo深圳
  • wordpress 判斷管理員seo數(shù)據(jù)分析哪些方面
  • 像優(yōu)酷這樣的網(wǎng)站需要怎么做百度官網(wǎng)入口
  • wordpress禁用php報錯湖南seo推廣系統(tǒng)
  • 國外紅色企業(yè)網(wǎng)站公眾號推廣方案