一架梯子,一头程序猿,仰望星空!
LangChain教程(JS版本) > 内容正文

模型输出解析器


输出解析器

语言模型输出文本。但是很多时候,您可能希望获得比仅文本更结构化的信息。这就是输出解析器的作用。

输出解析器是帮助组织语言模型响应的类。输出解析器必须实现两个主要方法:

  • “获取格式说明”:返回一个字符串,包含语言模型输出的格式说明。
  • “解析”:接受一个字符串(假设是语言模型的响应),并将其解析成某种结构。

然后有一个可选的方法:

  • “带提示解析”:接受一个字符串(假设是语言模型的响应)和一个提示(假设是生成此响应的提示),并将其解析成某种结构。提示通常提供,以便OutputParser想要重试或修复输出时,需要提示中的信息。

入门

下面我们将介绍一种有用的输出解析器类型,即StructuredOutputParser

结构化输出解析器

当您希望返回多个字段时,可以使用此输出解析器。如果您希望返回复杂的模式(即一个带有字符串数组的JSON对象),可以使用下面详细介绍的Zod Schema。

import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { StructuredOutputParser } from "langchain/output_parsers";

// 使用 `StructuredOutputParser` 可以为输出定义一个模式。
const parser = StructuredOutputParser.fromNamesAndDescriptions({
  answer: "用户问题的答案",
  source: "回答用户问题所使用的来源,应该是一个网站。",
});

const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
  template:
    "尽可能完善地回答用户的问题。\n{format_instructions}\n{question}",
  inputVariables: ["question"],
  partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({
  question: "法国的首都是哪里?",
});
const response = await model.call(input);

console.log(input);
/*
尽可能完善地回答用户的问题。
您必须将输出格式化为符合给定 "JSON Schema" 实例的 JSON 值。

"JSON Schema" 是一种声明性语言,允许您注释和验证 JSON 文档。

例如,示例的 "JSON Schema" 实例 {{"properties": {{"foo": {{"description": "a list of test words", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}}
将与具有一个必需属性 "foo" 的对象匹配。"type" 属性指定 "foo" 必须是数组,"description" 属性在语义上将其描述为 "a list of test words"。 "foo" 中的项必须为字符串。
因此,对象 {{"foo": ["bar", "baz"]}} 是此示例 "JSON Schema" 的一个格式良好的实例。对象 {{"properties": {{"foo": ["bar", "baz"]}}}} 则不是格式良好的。

您的输出将根据提供的模式实例进行解析和类型检查,因此请确保输出中的所有字段与模式完全匹配,并且没有尾随逗号!

这是您的输出必须遵循的 JSON Schema 实例。包括外部的 markdown 代码块:
\`\`\`json
{"type":"object","properties":{"answer":{"type":"string","description":"用户问题的答案"},"source":{"type":"string","description":"回答用户问题所使用的来源,应该是一个网站。"}},"required":["answer","source"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
\`\`\`

法国的首都是哪里?
*/

console.log(response);
/*
{"answer": "巴黎", "source": "https://en.wikipedia.org/wiki/Paris"}
*/

console.log(await parser.parse(response));
// { answer: '巴黎', source: 'https://en.wikipedia.org/wiki/Paris' }

使用Zod Schema的结构化输出解析器

当您想要使用Zod(一种TypeScript验证库)定义输出模式时,可以使用此输出解析器。传入的Zod模式需要可以从JSON字符串中解析,因此例如z.date()是不允许的。

import { z } from "zod";
import { OpenAI } from "langchain/llms/openai";
import { PromptTemplate } from "langchain/prompts";
import { StructuredOutputParser } from "langchain/output_parsers";

// 我们可以使用zod通过`StructuredOutputParser`的`fromZodSchema`方法来定义输出的模式。
const parser = StructuredOutputParser.fromZodSchema(
  z.object({
    answer: z.string().describe("回答用户的问题"),
    sources: z
      .array(z.string())
      .describe("用于回答问题的来源,应为网站。"),
  })
);

const formatInstructions = parser.getFormatInstructions();

const prompt = new PromptTemplate({
  template:
    "尽量最好地回答用户的问题。\n{format_instructions}\n{question}",
  inputVariables: ["question"],
  partialVariables: { format_instructions: formatInstructions },
});

const model = new OpenAI({ temperature: 0 });

const input = await prompt.format({
  question: "法国的首都是哪里?",
});
const response = await model.call(input);

console.log(input);
/*
尽量最好地回答用户的问题。
输出应该格式化为符合以下JSON模式的JSON实例。

举个例子,对于模式 {{"properties": {{"foo": {{"title": "Foo", "description": "一个字符串列表", "type": "array", "items": {{"type": "string"}}}}}}, "required": ["foo"]}}}
对象 {{"foo": ["bar", "baz"]}} 是一个符合模式的格式良好的实例。对象 {{"properties": {{"foo": ["bar", "baz"]}}}} 不是格式良好的。

这是输出模式:
\`\`\`
{"type":"object","properties":{"answer":{"type":"string","description":"回答用户的问题"},"sources":{"type":"array","items":{"type":"string"},"description":"用于回答问题的来源,应为网站。"}},"required":["answer","sources"],"additionalProperties":false,"$schema":"http://json-schema.org/draft-07/schema#"}
\`\`\`
*
console.log(response);
/*
{"answer": "巴黎", "sources": ["https://en.wikipedia.org/wiki/Paris"]}
*/

console.log(await parser.parse(response));
/*
{ answer: '巴黎', sources: [ 'https://en.wikipedia.org/wiki/Paris' ] }
*/


关联主题