核心模块(Modules)
模型 I/O(ModelIO)
输出解析器(OutputParsers)
类型(Types)
YAML(Yaml)

YAML 解析器

该输出解析器允许用户指定任意模式并查询 LLMs,以符合该模式的输出,使用 YAML 格式化其响应。

请记住,大型语言模型是有缺陷的抽象!您必须使用具有足够容量的 LLM 来生成格式良好的 YAML。在 OpenAI 系列中,DaVinci 可以可靠地执行,但 Curie 的能力已经急剧下降。

您可以选择使用 Pydantic 声明数据模型。

from typing import List
 
from langchain.output_parsers import YamlOutputParser
from langchain_core.prompts import PromptTemplate
from langchain_core.pydantic_v1 import BaseModel, Field
from langchain_openai import ChatOpenAI
model = ChatOpenAI(temperature=0)

定义您期望的数据结构。

class Joke(BaseModel):
    setup: str = Field(description="提出一个笑话的问题")
    punchline: str = Field(description="解决笑话的答案")

以及一个旨在提示语言模型填充数据结构的查询。

joke_query = "告诉我一个笑话。"
 
# 设置解析器+将指令注入到提示模板中。
 
parser = YamlOutputParser(pydantic_object=Joke)
 
prompt = PromptTemplate(
    template="回答用户提问。\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})
 
 

查看 YamlOutputParser (opens in a new tab) 的 API 文档。