Skip to content

正则解析器

RegexParser 的本质是通过你提供的 正则表达式,从模型返回的纯文本内容中通过你提供的正则表达式来提取数据,并映射为结构化对象。

你只需要提供:

LangChain 会自动处理文本匹配与结果封装。

这个解析器不在 @langchain/core 这个包里面,在 langchain 这个包里面。

先安装 langchain 包:

Terminal window
pnpm add langchain

快速上手

const parser = new RegexParser(
/Title:\s*(.+)\nScore:\s*(\d{1,3})/,
['title', 'score'], // 捕获组顺序对应
'raw' // 兜底:不匹配时把全文放在 raw
)
console.log(parser.getFormatInstructions())

得到的提示词:

Your response should match the following regex: /Title:\s*(.+)\nScore:\s*(\d{1,3})/

要求大模型返回的答案,必须能够满足我们提供的正则表达式。

快速上手

import { ChatOllama } from '@langchain/ollama'
import { PromptTemplate } from '@langchain/core/prompts'
import { RegexParser } from 'langchain/output_parsers'
const parser = new RegexParser(
/Title:\s*(.+)\nScore:\s*(\d{1,3})/,
['title', 'score'], // 捕获组顺序对应
'raw' // 兜底:不匹配时把全文放在 raw
)
console.log(parser.getFormatInstructions())
const prompt = PromptTemplate.fromTemplate(`
请严格按下面格式输出:
{format_instructions}
问题:{question}
`)
const chain = prompt.pipe(new ChatOllama({ model: 'llama3', temperature: 0 }))
const txt =
'苹果是一种营养丰富的水果,被誉为“全方位的健康水果”。它富含多种维生素和矿物质,尤其是维生素C(增强免疫力、抗氧化)和膳食纤维(促进消化、增加饱腹感)。苹果中的多酚类物质(如槲皮素)具有抗炎和抗氧化作用,有助于降低慢性病风险。此外,苹果的钾含量较高,能帮助调节血压,而低热量(约52千卡/100克)的特点也使其成为健康饮食的理想选择。'
const result = await chain.invoke({
question: `帮我为${txt}写个标题并给出信心分数(0-100)。`,
format_instructions: parser.getFormatInstructions(),
})
console.log(result.content)

完整的提示词:

请严格按下面格式输出:
Your response should match the following regex: /Title:\s*(.+)\nScore:\s*(\d{1,3})/
问题:
帮我为"苹果是一种营养丰富的水果,被誉为“全方位的健康水果”。它富含多种维生素和矿物质,尤其是维生素C(增强免疫力、抗氧化)和膳食纤维(促进消化、增加饱腹感)。苹果中的多酚类物质(如槲皮素)具有抗炎和抗氧化作用,有助于降低慢性病风险。此外,苹果的钾含量较高,能帮助调节血压,而低热量(约52千卡/100克)的特点也使其成为健康饮食的理想选择。"写个标题并给出信心分数(0-100)。

输出:

Here is the output in the required format:
Title: Apple - A Nutrient-Rich Fruit Packed with Health Benefits
Score: 95

接下来添加上正则解析器:

const chain = prompt
.pipe(new ChatOllama({ model: 'llama3', temperature: 0 }))
.pipe(parser)
// ...
console.log(result)

输出:

{ "title": "Apple - The All-Round Healthy Fruit", "score": "92" }

更多解析器参阅官方文档:https://v03.api.js.langchain.com/modules/langchain.output_parsers.html