跳到主要内容

如何解析 JSON 输出

虽然一些模型提供商支持内置方式来返回结构化输出,但并非所有都支持。我们可以使用输出解析器来帮助用户通过提示指定任意 JSON 模式,查询模型以获得符合该模式的输出,并最终将该模式解析为 JSON。

注意

请记住,大型语言模型是泄漏的抽象!您需要使用具有足够容量的 LLM 来生成格式良好的 JSON。

JsonOutputParser 是一个内置选项,用于提示和解析 JSON 输出。

选择您的聊天模型

安装依赖项

提示

请参阅 此部分,获取有关安装集成包的通用说明.

yarn add @langchain/groq 

添加环境变量

GROQ_API_KEY=your-api-key

实例化模型

import { ChatGroq } from "@langchain/groq";

const model = new ChatGroq({
model: "llama-3.3-70b-versatile",
temperature: 0
});
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4o",
temperature: 0,
});

import { JsonOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";

// Define your desired data structure. Only used for typing the parser output.
interface Joke {
setup: string;
punchline: string;
}

// A query and format instructions used to prompt a language model.
const jokeQuery = "Tell me a joke.";
const formatInstructions =
"Respond with a valid JSON object, containing two fields: 'setup' and 'punchline'.";

// Set up a parser + inject instructions into the prompt template.
const parser = new JsonOutputParser<Joke>();

const prompt = ChatPromptTemplate.fromTemplate(
"Answer the user query.\n{format_instructions}\n{query}\n"
);

const partialedPrompt = await prompt.partial({
format_instructions: formatInstructions,
});

const chain = partialedPrompt.pipe(model).pipe(parser);

await chain.invoke({ query: jokeQuery });
{
setup: "Why don't scientists trust atoms?",
punchline: "Because they make up everything!"
}

流式传输

JsonOutputParser 也支持流式传输部分块。当模型在多个块中返回部分 JSON 输出时,这非常有用。解析器将跟踪部分块,并在模型完成生成输出时返回最终的 JSON 输出。

for await (const s of await chain.stream({ query: jokeQuery })) {
console.log(s);
}
{}
{ setup: "" }
{ setup: "Why" }
{ 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!"
}

下一步

您现在已经学习了一种提示模型返回结构化 JSON 的方法。接下来,查看关于获取结构化输出的更广泛指南,了解其他技术。


此页面是否对您有帮助?


您也可以留下详细的反馈 在 GitHub 上.