跳至主要内容

如何解析 XML 输出

先决条件

本指南假设您熟悉以下概念: - 聊天模型 - 输出解析器 - 提示模板 - 结构化输出 - 将 Runnable 链在一起

来自不同提供商的 LLM 通常具有不同的优势,具体取决于它们训练的特定数据。这也意味着有些模型可能“更好”并且更可靠地生成除 JSON 格式以外的输出格式。

本指南向您展示如何使用 XMLOutputParser 来提示模型以 XML 输出,然后将该输出解析为可用的格式。

注意

请记住,大型语言模型是松散的抽象!您必须使用具有足够容量的 LLM 来生成格式良好的 XML。

在以下示例中,我们使用 Anthropic 的 Claude (https://docs.anthropic.com/claude/docs),它是专门针对 XML 标签优化的模型之一。

yarn add @langchain/anthropic @langchain/core

让我们从对模型的简单请求开始。

import { ChatAnthropic } from "@langchain/anthropic";

const model = new ChatAnthropic({
model: "claude-3-sonnet-20240229",
maxTokens: 512,
temperature: 0.1,
});

const query = `Generate the shortened filmograph for Tom Hanks.`;

const result = await model.invoke(
query + ` Please enclose the movies in "movie" tags.`
);

console.log(result.content);
Here is the shortened filmography for Tom Hanks, with movies enclosed in "movie" tags:

<movie>Forrest Gump</movie>
<movie>Saving Private Ryan</movie>
<movie>Cast Away</movie>
<movie>Apollo 13</movie>
<movie>Catch Me If You Can</movie>
<movie>The Green Mile</movie>
<movie>Toy Story</movie>
<movie>Toy Story 2</movie>
<movie>Toy Story 3</movie>
<movie>Toy Story 4</movie>
<movie>Philadelphia</movie>
<movie>Big</movie>
<movie>Sleepless in Seattle</movie>
<movie>You've Got Mail</movie>
<movie>The Terminal</movie>

这实际上非常有效!但最好将该 XML 解析为更易于使用的格式。我们可以使用 XMLOutputParser 来向提示添加默认格式说明并解析输出的 XML 到字典。

import { XMLOutputParser } from "@langchain/core/output_parsers";

// We will add these instructions to the prompt below
const parser = new XMLOutputParser();

parser.getFormatInstructions();
"The output should be formatted as a XML file.\n" +
"1. Output should conform to the tags below. \n" +
"2. If tag"... 434 more characters
import { ChatPromptTemplate } from "@langchain/core/prompts";

const prompt = ChatPromptTemplate.fromTemplate(
`{query}\n{format_instructions}`
);
const partialedPrompt = await prompt.partial({
format_instructions: parser.getFormatInstructions(),
});

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

const output = await chain.invoke({
query: "Generate the shortened filmograph for Tom Hanks.",
});

console.log(JSON.stringify(output, null, 2));
{
"filmography": [
{
"actor": [
{
"name": "Tom Hanks"
},
{
"films": [
{
"film": [
{
"title": "Forrest Gump"
},
{
"year": "1994"
},
{
"role": "Forrest Gump"
}
]
},
{
"film": [
{
"title": "Saving Private Ryan"
},
{
"year": "1998"
},
{
"role": "Captain Miller"
}
]
},
{
"film": [
{
"title": "Cast Away"
},
{
"year": "2000"
},
{
"role": "Chuck Noland"
}
]
},
{
"film": [
{
"title": "Catch Me If You Can"
},
{
"year": "2002"
},
{
"role": "Carl Hanratty"
}
]
},
{
"film": [
{
"title": "The Terminal"
},
{
"year": "2004"
},
{
"role": "Viktor Navorski"
}
]
}
]
}
]
}
]
}

您会在上面注意到,我们的输出不再仅限于 movie 标签之间。我们还可以添加一些标签以根据我们的需求定制输出。

const parserWithTags = new XMLOutputParser({
tags: ["movies", "actor", "film", "name", "genre"],
});

// We will add these instructions to the prompt below
parserWithTags.getFormatInstructions();
"The output should be formatted as a XML file.\n" +
"1. Output should conform to the tags below. \n" +
"2. If tag"... 460 more characters

您可以在提示的其他部分添加自己的格式提示,并应进行尝试,以增强或替换默认说明。

以下是调用它时的结果。

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

const promptWithTags = ChatPromptTemplate.fromTemplate(
`{query}\n{format_instructions}`
);
const partialedPromptWithTags = await promptWithTags.partial({
format_instructions: parserWithTags.getFormatInstructions(),
});

const chainWithTags = partialedPromptWithTags.pipe(model).pipe(parserWithTags);

const outputWithTags = await chainWithTags.invoke({
query: "Generate the shortened filmograph for Tom Hanks.",
});

console.log(JSON.stringify(outputWithTags, null, 2));
{
"movies": [
{
"actor": [
{
"film": [
{
"name": "Forrest Gump"
},
{
"genre": "Drama"
}
]
},
{
"film": [
{
"name": "Saving Private Ryan"
},
{
"genre": "War"
}
]
},
{
"film": [
{
"name": "Cast Away"
},
{
"genre": "Drama"
}
]
},
{
"film": [
{
"name": "Catch Me If You Can"
},
{
"genre": "Biography"
}
]
},
{
"film": [
{
"name": "The Terminal"
},
{
"genre": "Comedy-drama"
}
]
}
]
}
]
}

下一步

您现在已经了解了如何提示模型返回 XML。接下来,查看 获取结构化输出的更广泛指南,以了解其他相关技术。


本页对您有帮助吗?


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