跳到主要内容

Structured Outputs

解决什么问题

强制LLM输出JSON格式并且是指定的Schema

老的解决方案

在提示词中要求LLM输出 JSON格式 和 每个字段的名称和类型,例如

output in JSON object with follow fileds:
- name: string
- age: number
- isFemale: boolean

LangChain有Parser可以帮你生成提示词。

问题1

但是,LLM还是会有概率输出非JSON格式,或者,字段不是预期的。

后来的解决方案

OpenAI的API引进了json_object模式,可以强制LLM返回JSON格式。

问题2

LLM返回的字段可能不是预期的。

最新的解决方案

OpenAI的·Structured Outputs·方案,可以在API的字段中传入明确的JSON Schema,这样LLM可以输出指定的格式。

response_format: { "type": "json_schema", "json_schema": … , "strict": true }

你可以在json_schema中指定格式,例如:

{
type: "json_schema",
json_schema: {
name: "math_response",
schema: {
type: "object",
properties: {
steps: {
type: "array",
items: {
type: "object",
properties: {
explanation: { type: "string" },
output: { type: "string" }
},
required: ["explanation", "output"],
additionalProperties: false
}
},
final_answer: { type: "string" }
},
required: ["steps", "final_answer"],
additionalProperties: false
},
strict: true
}
}

在Node.js中,你可以更简单地使用:

先定义JSON Schema

import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

const Step = z.object({
explanation: z.string(),
output: z.string(),
});

const MathResponse = z.object({
steps: z.array(Step),
final_answer: z.string(),
});

再放到response_format字段

const completion = await openai.beta.chat.completions.parse({
model: "gpt-4o-mini",
messages: [
{ role: "system", content: "You are a helpful math tutor. Guide the user through the solution step by step." },
{ role: "user", content: "how can I solve 8x + 7 = -23" },
],
response_format: zodResponseFormat(MathResponse, "math_response"),
});

非常地方便。

在LangChain中如何使用

import { ChatOpenAI } from "@langchain/openai";
import { HumanMessage, SystemMessage } from "@langchain/core/messages";
import { StringOutputParser } from "@langchain/core/output_parsers";
import { z } from "zod";
import { zodResponseFormat } from "openai/helpers/zod";

export async function main() {
const CalendarEvent = z.object({
name: z.string(),
date: z.string(),
participants: z.array(z.string()),
});

const model = new ChatOpenAI({
model: "gpt-4o-mini",
// 在这里添加
modelKwargs: {
response_format: zodResponseFormat(CalendarEvent, "event"),
},
});
const messages = [
new SystemMessage("Extract the event information."),
new HumanMessage("我和小明参加婚礼"),
];
const parser = new StringOutputParser();

const chain = model.pipe(parser);
const resp = await chain.invoke(messages);
console.log(resp);
}

参考

官网