当前位置:首页 > 技术分析 > 正文内容

NLP(五十七)LangChain的结构化输出

ruisui883个月前 (01-23)技术分析18

在使用大模型的时候,尽管我们的prompt已经要求大模型给出固定的输出格式,比如JSON,但很多时候,大模型还是会输出额外的信息,使得我们在对输出信息进行结构化的时候产生困难。LangChain工具可以很好地将输出信息进行结构化。
关于LangChain的结构化输出,可参考网址:https://python.langchain.com/docs/modules/model_io/output_parsers/ ,这其中,我们较为关注的是
Structured output parser,通过定义StructuredOutputParser来使用,使用方式如下:

response_schemas = [
    ResponseSchema(name="answer", description="answer to the user's question"),
    ResponseSchema(name="source", description="source used to answer the user's question, should be a website.")
]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

一个例子

首先,我们先来看一个简单的结构化输出的prompt的写法:

# -*- coding: utf-8 -*-
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate

# 告诉他我们生成的内容需要哪些字段,每个字段类型式啥
response_schemas = [
    ResponseSchema(type="string", name="bad_string", description="This a poorly formatted user input string"),
    ResponseSchema(type="string", name="good_string", description="This is your response, a reformatted response")
]

# 初始化解析器
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

# 生成的格式提示符
format_instructions = output_parser.get_format_instructions()
print(format_instructions)

# 加入至template中
template = """
You will be given a poorly formatted string from a user.
Reformat it and make sure all the words are spelled correctly

{format_instructions}

% USER INPUT:
{user_input}

YOUR RESPONSE:
"""

# 将我们的格式描述嵌入到prompt中去,告诉llm我们需要他输出什么样格式的内容
prompt = PromptTemplate(
    input_variables=["user_input"],
    partial_variables={"format_instructions": format_instructions},
    template=template
)

promptValue = prompt.format(user_input="welcom to califonya!")
print(promptValue)

此时,结构化输出的内容如下:

The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":

```json
{
    "bad_string": string  // This a poorly formatted user input string
    "good_string": string  // This is your response, a reformatted response
}
```

prompt的内容如下:

You will be given a poorly formatted string from a user.
Reformat it and make sure all the words are spelled correctly

The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":

```json
{
    "bad_string": string  // This a poorly formatted user input string
    "good_string": string  // This is your response, a reformatted response
}
```

% USER INPUT:
welcom to califonya!

YOUR RESPONSE:

将上述内容使用LLM进行回复,示例Pyhon代码如下(接上述代码):

from langchain.llms import OpenAI

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

llm = OpenAI(model_name="text-davinci-003")

llm_output = llm(promptValue)
print(llm_output)

# 使用解析器进行解析生成的内容
print(output_parser.parse(llm_output))

输出结果如下:

```json
{
    "bad_string": "welcom to califonya!",
    "good_string": "Welcome to California!"
}
```
{'bad_string': 'welcom to califonya!', 'good_string': 'Welcome to California!'}

可以看到,大模型的输出结果正是我们所要求的JSON格式,且字段和数据类型、输出结果付出预期。

结构化抽取

有了上述的结构化输出,我们尝试对文本进行结构化抽取,并使得抽取结果按JSON格式输出。示例代码如下:

# -*- coding: utf-8 -*-
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

llm = OpenAI(model_name="gpt-3.5-turbo")

# 告诉他我们生成的内容需要哪些字段,每个字段类型式啥
response_schemas = [
    ResponseSchema(type="number", name="number", description="文本中的数字"),
    ResponseSchema(type="string", name="people", description="文本中的人物"),
    ResponseSchema(type="string", name="place", description="文本中的地点"),
]

# 初始化解析器
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

# 生成的格式提示符
format_instructions = output_parser.get_format_instructions()
print(format_instructions)

template = """
给定下面的文本,找出特定的结构化信息。

{format_instructions}

% USER INPUT:
{user_input}

YOUR RESPONSE:
"""

# prompt
prompt = PromptTemplate(
    input_variables=["user_input"],
    partial_variables={"format_instructions": format_instructions},
    template=template
)

promptValue = prompt.format(user_input="张晓明今天在香港坐了2趟地铁。")
print(promptValue)
llm_output = llm(promptValue)
print(llm_output)

# 使用解析器进行解析生成的内容
print(output_parser.parse(llm_output))

在这个例子中,我们要求从输入文本中抽取出number、people、place字段,数据类型分别为number、string、string,抽取要求为文本中的数字、人物、地点。注意,ResponseSchema中的数据类型(type)与JSON数据类型一致。
对两个样例文本进行抽取,抽取结果如下:

输入:张晓明今天在香港坐了2趟地铁。
抽取结果:{'number': 2, 'people': '张晓明', 'place': '香港'}


输入:昨天B站14周年的分享会上,B站CEO陈睿对这个指标做了官方的定义,用户观看视频所花费的时间,也就是播放分钟数。
抽取结果:{'number': 14, 'people': '陈睿', 'place': 'B站'

抽取的结果大多符合预期,除了第二句中将B站识别为地点,不太合理。当然,我们写的prompt过于简单,读者可以尝试更多更好的prompt的描述方式,这样也许能提升抽取的效果。

结构化抽取进阶

在这个例子中,我们使用结构化输出,来实现NLP中的常见任务:命名实体识别(NER),我们需要从文本中抽取出其中的时间、人物、地点、组织机构等,并以JSON格式输出,每个字段都以列表形式呈现。
实现的Python代码如下:

# -*- coding: utf-8 -*-
from langchain.output_parsers import StructuredOutputParser, ResponseSchema
from langchain.prompts import PromptTemplate
from langchain.llms import OpenAI

# set api key
import os
os.environ["OPENAI_API_KEY"] = 'sk-xxx'

llm = OpenAI(model_name="gpt-3.5-turbo")

# 告诉他我们生成的内容需要哪些字段,每个字段类型式啥
response_schemas = [
    ResponseSchema(type="array", name="time", description="文本中的日期时间列表"),
    ResponseSchema(type="array", name="people", description="文本中的人物列表"),
    ResponseSchema(type="array", name="place", description="文本中的地点列表"),
    ResponseSchema(type="array", name="org", description="文本中的组织机构列表"),
]

# 初始化解析器
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

# 生成的格式提示符
format_instructions = output_parser.get_format_instructions()
print(format_instructions)

template = """
给定下面的文本,找出特定的实体信息,并以结构化数据格式返回。

{format_instructions}

% USER INPUT:
{user_input}

YOUR RESPONSE:
"""

# prompt
prompt = PromptTemplate(
    input_variables=["user_input"],
    partial_variables={"format_instructions": format_instructions},
    template=template
)

promptValue = prompt.format(user_input="6月26日,广汽集团在科技日上首次公开展示飞行汽车项目,飞行汽车GOVE完成全球首飞。广汽研究院院长吴坚表示,GOVE可以垂直起降,并搭载双备份多旋翼飞行系统,保障飞行安全。")
print(promptValue)
llm_output = llm(promptValue)
print(llm_output)

# 使用解析器进行解析生成的内容
print(output_parser.parse(llm_output))

我们在三个示例文本中进行实验,看看大模型在NER方面的表现:

输入:6月26日周一,乌克兰总统泽连斯基视察了乌克兰军队东线司令部总部,而就在几小时前,俄罗斯宣布控制该国东部顿涅茨克以南的利夫诺波尔。
抽取结果:{'time': ['6月26日周一'], 'people': ['泽连斯基'], 'place': ['乌克兰军队东线司令部总部', '顿涅茨克', '利夫诺波尔'], 'org': ['乌克兰总统', '俄罗斯']


输入:日前,马自达找来梁家辉代言汽车,引发了业内的热议。相信很多人对于马自达的品牌认知来自梁家辉的那部电影。
抽取结果:{'time': [], 'people': ['梁家辉'], 'place': [], 'org': ['马自达']


输入:6月26日,广汽集团在科技日上首次公开展示飞行汽车项目,飞行汽车GOVE完成全球首飞。广汽研究院院长吴坚表示,GOVE可以垂直起降,并搭载双备份多旋翼飞行系统,保障飞行安全。
抽取结果:{'time': ['6月26日'], 'people': ['吴坚'], 'place': [], 'org': ['广汽集团', '广汽研究院']

可以看到,在经过简单的prompt和结构化输出后,大模型的抽取结果大致上令人满意。

总结

本文主要介绍了LangChain的结构化输出,这在我们需要对模型输出结果进行结构化解析时较为有用,同时,我们也能接住结构化输出完成一些常见的NLP的任务,如NER等。

参考文献

  1. LangChain 中文入门教程: https://liaokong.gitbook.io/llm-kai-fa-jiao-cheng/
  2. Structured output parser: https://python.langchain.com/docs/modules/model_io/output_parsers/structured

扫描二维码推送至手机访问。

版权声明:本文由ruisui88发布,如需转载请注明出处。

本文链接:http://www.ruisui88.com/post/505.html

分享给朋友:

“NLP(五十七)LangChain的结构化输出” 的相关文章

GitLab-合并请求

描述合并请求可用于在您对项目进行的其他人员之间交换代码,并轻松与他们讨论更改。合并请求的步骤步骤1-在创建新的合并请求之前,GitLab中应该有一个创建的分支。您可以参考本章来创建分支-步骤2-登录到您的GitLab帐户,然后转到“ 项目”部分下的项目 -步骤3-单击“ 合并请求”选项卡,然后单击“...

理解virt、res、shr之间的关系(linux系统篇)

前言想必在linux上写过程序的同学都有分析进程占用多少内存的经历,或者被问到这样的问题——你的程序在运行时占用了多少内存(物理内存)?通常我们可以通过top命令查看进程占用了多少内存。这里我们可以看到VIRT、RES和SHR三个重要的指标,他们分别代表什么意思呢?这是本文需要跟大家一起探讨的问题。...

html5+css3做的响应式企业网站前端源码

大家好,今天给大家介绍一款,html5+css3做的响应式企业网站前端源码 (图1)。送给大家哦,获取方式在本文末尾。首页banner幻灯片切换特效(图2)首页布局简约合理(图3)关于我们页面(图4)商品列表(图5)商品详情(图6)服务介绍(图7)新闻列表(图8)联系我们(图9)源码完整,需要的朋友...

别让“跑焦”毁所有!仅需这一项设置,即可显著改善镜头对焦精度

我常常会收到一些摄影爱好者的私信,也一直在努力的帮助大家解决更多摄影中常见问题。在我收到的所有问题中。有一个问题是最麻烦的,那就是“为什么我的图像看起来模糊?”。这个问题几乎每个人都遇到过,究其原因可以说是多种多样相对复杂。起初我一直认为是对焦问题所导致,也就有了我之前所写的“后按对焦”以及“对焦模...

vue中router常见的三种传参方式

目录:我们在使用vue开发的过程中使用router跳转的时候肯定会遇到传参的情况;一般情况就三种传参是最常见的;那我们就来看看都有那几种传参方式吧!第一种:{ path: '/mall:id', name: 'Mall', component:...

首批龙芯3A6000电脑规模化应用:内置QQ、微信主流软件

6月18日消息,今日,龙芯中科宣布,近千台龙芯3A6000电脑走进福建福州某区各科室并服务于具体工作开展。据介绍,该批电脑为实现首批规模化应用的3A6000整机产品,搭载国产操作系统,内置主流办公和即时通讯等软件,可充分满足打印机利旧要求(兼容原有打印机设备)。3A6000根据官方晒出的桌面截图显示...