Passa al contenuto principale

Outputs strutturati

Risolvere quale problema

Forza l'output LLM in formato JSON e lo schema specificato

La vecchia soluzione

Nel prompt richiede che il LLM produca il formato JSON e il nome e il tipo di ciascun campo, ad esempio

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

LangChain ha un parser che ti aiuta a generare un suggerimento.

Il problema 1

Tuttavia, il LLM ha ancora la probabilità di produrre un formato non JSON, o il campo non è previsto.

Soluzione successiva

L'API di OpenAI introduce lo schema json _ object, che forza le LLM a restituire il formato JSON.

Domanda 2

Il campo restituito da LLM potrebbe non essere quello previsto.

Le ultime soluzioni

Lo schema Structured Outputs di OpenAI permette di passare uno schema JSON esplicito nei campi dell 'API, in modo che l'LLM possa produrre un formato specificato.

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

È possibile specificare il formato in json _ schema, ad esempio:

{
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
}
}

In Node.js, è più semplice usare:

Definizione dello schema JSON

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(),
});

inserire il campo 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"),
});

Molto conveniente.

    • Come utilizzarlo in 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);
}

Referenc

官网