LangChain学习记录(一)prompt与输出解析

        langchain是个非常实用的开源LLM应用构建框架,里面集成了很多我们在构建自己的大模型应用时可能需要用到的模块组件,显著降低了开发者的工作量。最近也在学习调研langchain这个框架,看看这个开源工具都能为我们做什么,以及现阶段我们能利用语言大模型做出什么样的应用。这里分几篇博客简单记录一下学习过程。

        本篇首先记录的是langchain是怎样构造提示(prompt)以及提供我们想要的结构化的输出,也就是输出解析。

一、提示构造

举例说明。假设我们现在有这么个任务需要大模型处理

text  =  """Arrr,I be fuming that me blender lid\
flew off and splattered me kitchen walls \
with smoothie! And to make matters worse,\
the warranty don't cover the cost of \
cleaning up me kitchen. I need your help right now,matey \
"""

这是一段海盗风格的英语文字,想把其风格改为比较平静的叙述方式,且用美式英语来表述。我们先直接调用chatgpt的api来实现

import openai
import  os

os.environ["OPENAI_API_KEY"] = 'your api-key'
openai.api_key = os.environ.get("OPENAI_API_KEY")

def get_completion(prompt,model = 'gpt-3.5-turbo'):
    messages = [{'role':'user','content':prompt}]
    response = openai.ChatCompletion.create(
        model = model,
        messages = messages,
        temperature = 0
    )
    return response.choices[0].message['content']

构造一个调用大模型api的函数,再按照我们想要的风格构建提示

style = """American English in a calm and respectful tone"""

prompt = f"""Translate the text that is delimited by triple \
backticks into a style that is {style}.
text:'''{text}'''
"""
print(get_completion(prompt))

这里其实就是用一个格式化的字符串,将我们想要的风格以及源文字作为变量放进字符串,最后送给模型,得到输出

Arrr, I am quite upset that my blender lid \
 flew off and splattered my kitchen walls with \
 smoothie! And to make matters worse, \
the warranty does not cover the cost of \
cleaning up my kitchen. I could really \
use your help at this moment, friend.

如果构建一个应用,处理大量的不同的修改需求,比如使用日语、德语,或其他风格等,langchain里面提供了方便的提示构建组件。在langchain里如何实现呢

from langchain_community.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate

chat = ChatOpenAI(model_name = 'gpt-3.5-turbo',temperature= 0.7)
template_str = """Translate the text that is delimited by triple \
backticks into a style that is {style}.
text:'''{text}'''
"""
prompt_template = ChatPromptTemplate.from_template(template_str)

customer_prompt = prompt_template.format_messages(style = style,text = text)

customer_response = chat(customer_prompt)
print(customer_response.content)

通过langchain当中from_template方法构造一个模板,模板的内容就是你的具体要求,其实就是上文的格式化字符串去掉‘f’。这个方法做的工作就是识别你字符串当中所想要定义的变量,并且根据这个构造一个模板对象。后面可以用format_messages方法构造最终的prompt。 有了这个模板,就可以定义若干个不同style的prompt来供你使用 ,也可以用同一个模板处理不同的文字任务。比如把风格改成翻译成中文

style = """Chinese in a calm and respectful tone"""

  输出

"啊,我很生气,我的搅拌机盖飞了出去,把我的厨房墙壁都弄满了果汁!更糟糕的是,保修不包括清理厨房的费用。伙计,我现在需要你的帮助。"

二、结构化输出

        利用语言大模型开发应用时,模型输出往往只是整个应用链路中的一环,我们往往希望模型提供的输出是结构化的,以便下游任务进一步利用这个结果。

        依然举例说明。比如希望模型帮我提炼一段文字中的信息,文字如下

customer_review = """This leaf blower is pretty amazing. It has four settings:\
candle blower,gentle breeze,windy city, and tornado.It arrived in two days, just in]time for my wife'sanniversary present.\
I think my wife liked it so much she was speechless.So far I've been the only one using it, and I've been\
using it every other morning to clear the leaves on our lawrIt's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features
"""

我希望模型提炼出:1.这段话中谈论的物体是不是一个礼物 2.运送用了多长时间 3.价值多少           

那么,按照上文的方法,首先构建一个模板

review_template = """For the following text,extract the following information:
gift: Was the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.
delivery days: How many days did it take for the product to arrive? if this information is not found,output -1.
price_value: Extract any sentences about the value or price.
Format the output as JsoN with the following keys:
gift
delivery_days
price_value
text:{text}
"""

得到输出

{
  "gift": true,
  "delivery_days": 2,
  "price_value": "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features"
} <class 'str'>

这个输出的形式看上去满足我们的要求,但实际上没有经过处理的输出都是一段长字符串,比如下游任务想取到这个运送时间的值‘2’,在字符串中就比较麻烦。那么如何让变成字典呢

langchain里面同样封装好了输出解析的工具

from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser

首先定义我们希望输出的结构化形式,存到format_instructions里面

gift_schema = ResponseSchema(name='gift',description='Was the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.')

delivery_days_schema = ResponseSchema(name = 'delivery_days',description='How many days did it take for the product to arrive? if this information is not found,output -1.')

price_value_schema = ResponseSchema(name='price_value',description='Extract any sentences about the value or price.')

response_schemas = [gift_schema,delivery_days_schema,price_value_schema]

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instrctions = output_parser.get_format_instructions()

这个format_instrctions打印出来是这样的

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

```json
{
	"gift": string  // Was the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.
	"delivery_days": string  // How many days did it take for the product to arrive? if this information is not found,output -1.
	"price_value": string  // Extract any sentences about the value or price.
}
```

相当于生成了一段结构化的prompt用于提示生成内容的结构,方便解析

然后定义模板,这里用到了前面的format_instrctions

review_template2 = """For the following text,extract the following information:
gift: Was the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.
delivery days: How many days did it take for the product to arrive? if this information is not found,output -1.
price_value: Extract any sentences about the value or price,and output them as a comma separated python list.
text:{text}
{format_instrctions}
"""

这样送给模型得到的输出

```json
{
	"gift": "True",
	"delivery_days": "2",
	"price_value": "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features"
}
```

但这里依然是字符串,我们最后再给他解析成字典格式

output_dict = output_parser.parse(customer_response.content)

最后得到字典格式的输出

{'gift': 'True', 'delivery_days': '2', 'price_value': "It's slightly more expensive than the other leaf blowers out there, but I think it's worth it for the extra features"} <class 'dict'>

三、小结

        langchain里面封装了构建提示以及结构化输出的相关方法,方便我们在构建应用时根据需求去使用。学习这部分内容时,看到大模型能够根据prompt的显式提示就能理解意图,生成我们想要的风格以及内容格式,还是挺令人惊叹的,但前提是我们给的prompt给的足够好。也就是说目前大模型是具备很强的潜力去做到很多事,有时候效果不尽如人意可能只是因为给的提示还不够好。如何更好让大模型为我们服务,怎样去根据我们的需求构建合适的prompt是个值得思考研究的方向。

  • 10
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值