跳到主要內容
Open In ColabOpen on GitHub

如何解析 YAML 輸出

來自不同供應商的 LLM 通常具有不同的優勢,具體取決於它們訓練的特定資料。這也意味著有些在生成 JSON 以外的格式的輸出時可能「更好」且更可靠。

此輸出解析器允許使用者指定任意架構,並查詢 LLM 以取得符合該架構的輸出,並使用 YAML 格式化其回應。

注意

請記住,大型語言模型是洩漏的抽象概念!您必須使用具有足夠容量的 LLM 來產生格式正確的 YAML。

%pip install -qU langchain langchain-openai

import os
from getpass import getpass

if "OPENAI_API_KEY" not in os.environ:
os.environ["OPENAI_API_KEY"] = getpass()

我們將 PydanticYamlOutputParser 搭配使用,以宣告我們的資料模型,並為模型提供更多關於它應產生何種類型 YAML 的上下文

from langchain.output_parsers import YamlOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI
from pydantic import BaseModel, Field


# Define your desired data structure.
class Joke(BaseModel):
setup: str = Field(description="question to set up a joke")
punchline: str = Field(description="answer to resolve the joke")


model = ChatOpenAI(temperature=0)

# And a query intented to prompt a language model to populate the data structure.
joke_query = "Tell me a joke."

# Set up a parser + inject instructions into the prompt template.
parser = YamlOutputParser(pydantic_object=Joke)

prompt = PromptTemplate(
template="Answer the user query.\n{format_instructions}\n{query}\n",
input_variables=["query"],
partial_variables={"format_instructions": parser.get_format_instructions()},
)

chain = prompt | model | parser

chain.invoke({"query": joke_query})
Joke(setup="Why couldn't the bicycle find its way home?", punchline='Because it lost its bearings!')

解析器將自動解析輸出 YAML 並使用資料建立 Pydantic 模型。我們可以查看解析器的 format_instructions,它會新增至提示

parser.get_format_instructions()
'The output should be formatted as a YAML instance that conforms to the given JSON schema below.\n\n# Examples\n## Schema\n\`\`\`\n{"title": "Players", "description": "A list of players", "type": "array", "items": {"$ref": "#/definitions/Player"}, "definitions": {"Player": {"title": "Player", "type": "object", "properties": {"name": {"title": "Name", "description": "Player name", "type": "string"}, "avg": {"title": "Avg", "description": "Batting average", "type": "number"}}, "required": ["name", "avg"]}}}\n\`\`\`\n## Well formatted instance\n\`\`\`\n- name: John Doe\n  avg: 0.3\n- name: Jane Maxfield\n  avg: 1.4\n\`\`\`\n\n## Schema\n\`\`\`\n{"properties": {"habit": { "description": "A common daily habit", "type": "string" }, "sustainable_alternative": { "description": "An environmentally friendly alternative to the habit", "type": "string"}}, "required": ["habit", "sustainable_alternative"]}\n\`\`\`\n## Well formatted instance\n\`\`\`\nhabit: Using disposable water bottles for daily hydration.\nsustainable_alternative: Switch to a reusable water bottle to reduce plastic waste and decrease your environmental footprint.\n\`\`\` \n\nPlease follow the standard YAML formatting conventions with an indent of 2 spaces and make sure that the data types adhere strictly to the following JSON schema: \n\`\`\`\n{"properties": {"setup": {"title": "Setup", "description": "question to set up a joke", "type": "string"}, "punchline": {"title": "Punchline", "description": "answer to resolve the joke", "type": "string"}}, "required": ["setup", "punchline"]}\n\`\`\`\n\nMake sure to always enclose the YAML output in triple backticks (\`\`\`). Please do not add anything other than valid YAML output!'

您可以而且應該嘗試在提示的其他部分新增您自己的格式提示,以增強或取代預設指示。

下一步

您現在已了解如何提示模型傳回 YAML。接下來,查看關於取得結構化輸出的更廣泛指南,以了解其他相關技術。


此頁面是否對您有幫助?