当前位置:   article > 正文

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

langchain打印 prompt

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

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

一、提示构造

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

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

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

  1. import openai
  2. import os
  3. os.environ["OPENAI_API_KEY"] = 'your api-key'
  4. openai.api_key = os.environ.get("OPENAI_API_KEY")
  5. def get_completion(prompt,model = 'gpt-3.5-turbo'):
  6. messages = [{'role':'user','content':prompt}]
  7. response = openai.ChatCompletion.create(
  8. model = model,
  9. messages = messages,
  10. temperature = 0
  11. )
  12. return response.choices[0].message['content']

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

  1. style = """American English in a calm and respectful tone"""
  2. prompt = f"""Translate the text that is delimited by triple \
  3. backticks into a style that is {style}.
  4. text:'''{text}'''
  5. """
  6. print(get_completion(prompt))

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

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

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

  1. from langchain_community.chat_models import ChatOpenAI
  2. from langchain.prompts import ChatPromptTemplate
  3. chat = ChatOpenAI(model_name = 'gpt-3.5-turbo',temperature= 0.7)
  4. template_str = """Translate the text that is delimited by triple \
  5. backticks into a style that is {style}.
  6. text:'''{text}'''
  7. """
  8. prompt_template = ChatPromptTemplate.from_template(template_str)
  9. customer_prompt = prompt_template.format_messages(style = style,text = text)
  10. customer_response = chat(customer_prompt)
  11. print(customer_response.content)

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

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

  输出

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

二、结构化输出

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

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

  1. customer_review = """This leaf blower is pretty amazing. It has four settings:\
  2. candle blower,gentle breeze,windy city, and tornado.It arrived in two days, just in]time for my wife'sanniversary present.\
  3. 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\
  4. using it every other morning to clear the leaves on our lawrIt's slightly more expensive than the other leaf blowers \
  5. out there, but I think it's worth it for the extra features
  6. """

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

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

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

得到输出

  1. {
  2. "gift": true,
  3. "delivery_days": 2,
  4. "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"
  5. } <class 'str'>

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

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

  1. from langchain.output_parsers import ResponseSchema
  2. from langchain.output_parsers import StructuredOutputParser

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

  1. 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.')
  2. 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.')
  3. price_value_schema = ResponseSchema(name='price_value',description='Extract any sentences about the value or price.')
  4. response_schemas = [gift_schema,delivery_days_schema,price_value_schema]
  5. output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
  6. format_instrctions = output_parser.get_format_instructions()

这个format_instrctions打印出来是这样的

  1. The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":
  2. ```json
  3. {
  4. "gift": string // Was the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.
  5. "delivery_days": string // How many days did it take for the product to arrive? if this information is not found,output -1.
  6. "price_value": string // Extract any sentences about the value or price.
  7. }
  8. ```

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

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

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

这样送给模型得到的输出

  1. ```json
  2. {
  3. "gift": "True",
  4. "delivery_days": "2",
  5. "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"
  6. }
  7. ```

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

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是个值得思考研究的方向。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1000806
推荐阅读
相关标签
  

闽ICP备14008679号