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

JSON 解析器

这个输出解析器允许用户指定任意 JSON schema,并向 LLM 查询符合该 schema 的输出。

请记住,大语言模型是有泄漏的抽象!你必须使用容量足够强的 LLM 来生成格式良好的 JSON。在 OpenAI 系列中,DaVinci 可以可靠地做到,但 Curie 的能力已经明显下降。

你可以选择用 Pydantic 声明你的数据模型。

from langchain_core.output_parsers import JsonOutputParser
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="question to set up a joke")
    punchline: str = Field(description="answer to resolve the joke")
# 以及一个旨在提示语言模型填充该数据结构的查询。
joke_query = "Tell me a joke."
 
# 设置解析器 + 把说明注入提示模板。
parser = JsonOutputParser(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})
{'setup': "Why don't scientists trust atoms?",
 'punchline': 'Because they make up everything!'}

流式输出

这个输出解析器支持流式输出。

for s in chain.stream({"query": joke_query}):
    print(s)
{'setup': ''}
{'setup': 'Why'}
{'setup': 'Why don'}
{'setup': "Why don't"}
{'setup': "Why don't scientists"}
{'setup': "Why don't scientists trust"}
{'setup': "Why don't scientists trust atoms"}
{'setup': "Why don't scientists trust atoms?", 'punchline': ''}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because'}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because they'}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because they make'}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because they make up'}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because they make up everything'}
{'setup': "Why don't scientists trust atoms?", 'punchline': 'Because they make up everything!'}

不使用 Pydantic

你也可以在不使用 Pydantic 的情况下使用它。这会提示它返回 JSON,但不会具体说明 schema 应该是什么。

joke_query = "Tell me a joke."
 
parser = JsonOutputParser()
 
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': "Why don't scientists trust atoms? Because they make up everything!"}

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