LangChain大型语言模型(LLM)应用开发(一):Models, Prompts and Output Parsers

LangChain是一个基于大语言模型(如ChatGPT)用于构建端到端语言模型应用的 Python 框架。它提供了一套工具、组件和接口,可简化创建由大型语言模型 (LLM) 和聊天模型提供支持的应用程序的过程。LangChain 可以轻松管理与语言模型的交互,将多个组件链接在一起,以便在不同的应用程序中使用。

今天我们来学习DeepLearning.AI的在线课程:LangChain for LLM Application Development的第一门课:Models, Prompts and Output Parsers,该门课程主要讲解如何在Langchain中定义模型,如何编写Prompt以及如何对输出结果进行解析。

设置访问LLM的API key

因为Langchain本身只是一个LLM的应用框架,所以它必须和LLM对接才能使用,这里和Langchain对接的LLM主要是openai的语言模型,所以我们需要设置访问Openai的api key:

#!pip install python-dotenv
#!pip install openai

import os
import openai

from dotenv import load_dotenv, find_dotenv
_ = load_dotenv(find_dotenv()) # read local .env file
openai.api_key = os.environ["OPENAI_API_KEY"]

Chat API : OpenAI

首先让我们从直接从调用 OpenAI 的API 开始。下面是我们访问openai的“gpt-3.5-turbo”模型的主要代码:

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"]

 下面我们我们用openai的api来实现一个简单的语调转换的简单例子,首先我们需要定义一个prompt:

customer_email="""
哎呀,我很生气,因为我的搅拌机盖子飞走了,冰沙溅到了我的厨房墙壁上! 
更糟糕的是,保修不包括清理厨房的费用。 我现在需要你的帮助,朋友!
"""

style ="""
委婉和耐心的语气表达
"""

prompt =f"""将由三个反引号分隔的文本
转换为{style}风格。
文本:```{customer_email}```
"""

print(prompt)

 这里我们定义了一个prompt,该prompt是由f-string定义的一个长字符串,其中嵌入了两个变量,style和customer_email,并且主要的文本内容customer_email用一对3个反引号隔离。这是创建openai的prompt的典型方法。

response = get_completion(prompt)
print(response)

Chat API : LangChain

让我们尝试一下如何使用 LangChain 来做同样的事情。

Model

首先我们需要通过langchain来创建一个chat model对象:

#!pip install --upgrade langchain

from langchain.chat_models import ChatOpenAI

#要减少LLM 生成文本的随机性,请设置温度参数= 0.0
chat = ChatOpenAI(temperature=0.0)
chat

 这里我们定义了一个Langchain的聊天模型,该模型的默认参数均为openai的API的参数,如“model_name”=“gpt-3.5-turbo”,“temperature”=0.0等,这里需要说明的时候temperature参数的默认值为0.7,只是这里我们人为设置为0.0,除此之外其他参数均为默认参数。

定义Langchain的Prompt 模板

style ="""
委婉和耐心的语气表达
"""
customer_email="""
哎呀,我很生气,因为我的搅拌机盖子飞走了,冰沙溅到了我的厨房墙壁上! 
更糟糕的是,保修不包括清理厨房的费用。 我现在需要你的帮助,朋友!
"""

template_string = """将由三个反引号分隔的文本
转换为 {style} 风格。
文本:```{text}```
"""

这里需要说明的是template_string并不是f-string,所以其中的{style}和{text}并不是变量,而是属于字符串本身的一部分。接下来我们需要定义一个prompt模板:ChatPromptTemplate

from langchain.prompts import ChatPromptTemplate

prompt_template = ChatPromptTemplate.from_template(template_string)
prompt_template

 这里我们可以看到虽然我们没有将template_string定义为f-string,但是当我们创建prompt_template 时,会自动将template_string的中{style}和{text}识别为变量,并且将模板的格式定义为f-string。

下面我们创建一个message,该message是由prompt 模板的format_messages方法生成的,该方法包含了两个参数:style和text, 这两个参数对应于template_string 中的两个变量{style}和{text}。

customer_messages = prompt_template.format_messages(
                    style=style,
                    text=customer_email)
customer_messages

下面我们调用LLM 来实现客户信息的语气风格转换:

#调用LLM来翻译客户信息的风格
customer_response = chat(customer_messages)
print(customer_response.content)

 

 这里我们可以看到输出结果与之前直接调用openai api的方法的结果是一样的。下面我们再看一个例子:

service_reply ="""
嘿,顾客,保修不包括厨房的清洁费用,\
因为您在启动搅拌机之前忘记盖上盖子而误用搅拌机,这是您的错。 \
倒霉! 再见!
"""

service_style = """
和蔼和礼貌的口吻
"""

service_messages = prompt_template.format_messages(
    style=service_style,
    text=service_reply)

print(service_messages[0].content)

service_response = chat(service_messages)
print(service_response.content)

 

 输出解析(Output Parsers)

 在之前的博客:使用大型语言模(LLM)构建系统(七):评估1中我们发现有时候LLM的输出结果数据中除了会包含我们需要的指定格式数据以外还会附带一些文本说明信息,而这些文本说明信息的存在不利于我们解析LLM的输出结果,在之前的博客中我们通过修改prompt,在其中加入禁止输出某些的内容的语句后,输出结果有了改善。下面我们看看Langchain是如果来解析LLM的输出结果的。

customer_review = """
这款吹风机非常神奇。 它有四个设置:\
吹蜡烛、微风、风城、龙卷风。 \
两天后就到了,正好赶上我妻子的周年纪念礼物。 \
我想我的妻子非常喜欢它,她说不出话来。 \
到目前为止,我是唯一一个使用它的人,\
而且我每隔一天早上都会使用它来清除草坪上的树叶。\
它比其他吹叶机稍微贵一点,但我认为它的额外功能是值得的。
"""

review_template ="""
对于下面的文本,提取以下信息:

gift:该商品是作为礼物送给别人的吗? \
如果是,则回答 True;如果否或未知,则回答 False。

delivery_days:产品需要多少天到达? 如果没有找到该信息,则输出-1。

price_value:提取有关价值或价格的任何句子,\
并将它们输出为逗号分隔的 Python 列表。

输出包含下面的三个key的JSON格式:
gift
delivery_days
price_value

文本:{text}
"""

 这里我们提供了一段用户对商品的评语customer_review 和评语模板review_template,在review_template中我们要求LLM提取用户评语中的gift、delivery_days和price_value三部分信息,并以JSON格式输出。

from langchain.prompts import ChatPromptTemplate

prompt_template = ChatPromptTemplate.from_template(review_template)

messages = prompt_template.format_messages(text=customer_review)
chat = ChatOpenAI(temperature=0.0)
response = chat(messages)
print(response.content)

 从上面的输出结果中我们可以看到LLM输出了一个符合要求的JSON格式,不过这里需要说明的是response.content返回的其实都是字符串,也就是说LLM只能返回字符串,无法直接返回特定类型的数据,下面我们用访问python字典的方式来访问返回结果中的数据,我们会看到它会报错:

# You will get an error by running this line of code 
# because'gift' is not a dictionary
# 'gift' is a string
response.content.get('gift')

 

 这里报错的原因是string对象没有get属性。

将LLM输出字符串解析为Python字典

要解析输出结果中的格式化数据,我们需要定义ResponseSchema变量,这里我们需要解析3个变量gift、delivery_days和price_value,所以需要定义3个ResponseSchema,最后把它们打包在一起放在list中:

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

gift_schema = ResponseSchema(name="gift",
                             description="该商品是作为礼物送给别人的吗?\
                             如果是,则回答 True,如果否或未知,则回答 False。")

delivery_days_schema = ResponseSchema(name="delivery_days",
                                      description="产品需要多少天才能送达? \
                                                   如果没有找到该信息,则输出-1。")

price_value_schema = ResponseSchema(name="price_value",
                                    description="提取有关价值或价格的任何句子,\
                                    并将它们输出为逗号分隔的 Python 列表。")

response_schemas = [gift_schema, 
                    delivery_days_schema,
                    price_value_schema]

 接下来我们要定义一个解析器StructuredOutputParser。

output_parser = StructuredOutputParser.from_response_schemas(response_schemas)

 然后让该解析器给我们生成一个prompt,该prompt是一个格式化指令,它用来指导LLM产生格式化的输出结果:

format_instructions = output_parser.get_format_instructions()
print(format_instructions)

 接下来我们要定义一个新的review_template_2模板,在之前的review_template模板中我们明确要求LLM生成一个包含3个key的JSON格式的结果,而在下面定义的review_template_2模板中我们不再要求LLM生成一个JSON格式的结果:

review_template_2 ="""
对于下面的文本,提取以下信息:

gift:该商品是作为礼物送给别人的吗? \
如果是,则回答 True;如果否或未知,则回答 False。

delivery_days:产品需要多少天送达? \
如果没有找到该信息,则输出-1。

price_value:提取有关价值或价格的任何句子,\
并将它们输出为逗号分隔的 Python 列表。

文本:{text}

{format_instructions}
"""

在review_template_2中除了一个{text}变量以为,我们还增加了一个{format_instructions}变量,该变量为先前的输出解析器output_parser创建的prompt, 它的作用就是告诉LLM怎么提取text中的格式化数据:

prompt = ChatPromptTemplate.from_template(template=review_template_2)

messages = prompt.format_messages(text=customer_review, 
                                format_instructions=format_instructions)

print(messages[0].content)

 上面是我们将review_template_2、text,format_instructions三部分内容整合在一起的一个完整的prompt,下面我们让LLM读取这个完整的prompt,并查看返回结果:

response = chat(messages)
print(response.content)

 下面我们用输出解析器来解析这个结果,看看能否得到我们想要的字典格式:

output_dict = output_parser.parse(response.content)
output_dict

我们使用type命令来查看它的类型:

 总结

今天我们学习了如何使用langchain来创建prompt模板,并且langchain的prompt模板会自动识别prompt中的内嵌变量,在生成message时只需在prompt模板的format_messages方法中传递所需变量即可。另外我们还学习了如何通过Langchain来解析LLM的输出结果,通过创建ResponseSchema和StructuredOutputParser,可以产生用来指导LLM如何产生格式化数据的prompt即format_instructions ,LLM会根据format_instructions的要求自动生产格式化的输出结果,而无需我们告诉LLM该如何生成格式化的输出结果。

参考资料

DLAI - Learning Platform Beta

猜你喜欢

转载自blog.csdn.net/weixin_42608414/article/details/131449895