学习目标:LangChain
- 掌握LangChain的入门知识
- 学会基本实现代码
学习内容:
LangChain:用于构建大语言模型应用的开源框架
LangChain的主要作用是简化和增强由大型语言模型(LLMs)驱动的应用程序的开发和管理。它提供了一系列工具和功能,帮助开发者更有效地构建复杂的自然语言处理(NLP)
- 在百度智能云中注册账号并拿到API和SECRET
- 在kaggle上实现代码
- 模版代码
- 解析器代码
实践部分:
- 第一步pip所需要的库、并导入
!pip install -q langchain
!pip install -q langchain_wenxin
from langchain.prompts import PromptTemplate,ChatPromptTemplate
from langchain.chains import LLMChain,ConversationChain
from langchain_wenxin.chat_models import ChatWenxin
from langchain.output_parsers import ResponseSchema,StructuredOutputParser
- 第二步通过api和secret获取model
WENXIN_APP_Key = "xxx"
WENXIN_APP_SECRET = "xxx"
#创建LLMChain的大模型,这里我们用的是文心大模型
model = ChatWenxin(
temperature=0.1,
model="ernie-bot-turbo",#模型名称,可以自己替换成其它的百度模型
baidu_api_key = WENXIN_APP_Key,
baidu_secret_key = WENXIN_APP_SECRET,
verbose=True, #
)
- 第三步实现prompt代码
PromptTemplate 用于生成静态文本提示,是用于生成单一的、静态的文本提示的模板。它接受一个模板字符串和输入变量,并使用这些变量来填充模板。适用于单一文本生成任务,例如简单的问题回答、文本生成。
ChatPromptTemplate 用于生成多轮对话提示,是用于生成多轮对话的提示模板。它可以包含多个 MessagePromptTemplate,每个 MessagePromptTemplate 可以是用户消息或系统消息。适用于生成需要多轮对话的复杂提示。适用于多轮对话的复杂任务,例如对话系统、交互式问答等。
#方式一:使用PromptTemplate实现
template = "描述生产{product}的公司的一个最佳名称是什么?只返回一个答案,答案限定为3到5字。"
prompt = PromptTemplate(input_variables=["product"], template=template)
llm_chain = LLMChain(prompt=prompt, llm=model)
print(prompt)
# 打印结果:input_variables=['product'] template='描述生产{product}的公司的一个最佳名称是什么?只返回一个答案,答案限定为3到5字。'
res = llm_chain.run(product="手机") #参数product为上面input_variables
print(res)
#打印结果:考虑到描述生产手机的公司,一个最佳名称可能如下:
#名称:手机之家
#这个名称简洁明了,直接表达了公司的主营业务,同时“之家”也给人一种亲切、温馨的感觉。当然这只是建议,具体选择哪个名称还需要根据公司的具体情况和市场调研结果来决定。
#方式二:使用ChatPromptTemplate实现
prompt = ChatPromptTemplate.from_template(template)
print(prompt.messages[0].prompt)
#打印结果:input_variables=['product'] template='描述生产{product}的公司的一个最佳名称是什么?只返回一个答案,答案限定为3到5字。'
messages = prompt.format_messages(product="手机")
res = model(messages)
print(res.content)
- 第四步实现解析器代码,通常得到一段文本之后,我们需要对其进行解析,得到我们需要的信息。
customer_review='我在淘宝上看到了一个手表,看上去很不错,非常适合我拿来送给表哥当生日礼物,价格也实惠,只要399元。'
review_template='''
对于给出的文字提取以下信息:
礼物:是不是一个礼物,回答是或者不是或者不知道,
价格:这个礼物的价格是多少,回答一个数字加货币单位,
将这些输出作为json方式输出:
礼物
价格
text:{text}
'''
#对于上述,我们需要得到的信息为礼物是或者不是,价格多少
prompt = ChatPromptTemplate.from_template(review_template)
messages = prompt.format_messages(text=customer_review)
res = model(messages)
print(res.content)
print('----------')
print(type(res.content)) #发现是str信息,通常转换成字典json这类更好处理于是有了下面操作
from langchain.output_parsers import ResponseSchema,StructuredOutputParser#导入需要的包
gift_schema = ResponseSchema(name='礼物',description='是不是一个礼物,回答是或者不是或者不知道')
price_schema = ResponseSchema(name='价格',description='这个礼物的价格是多少,回答一个数字加货币单位')
response_schemas = [gift_schema,price_schema]
# 初始化解析器
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions
review_template2='''
对于给出的{text}提取以下信息:
礼物:是不是一个礼物,回答是或者不是或者不知道
价格:这个礼物的价格是多少,回答一个数字加货币单位
将这些输出作为json方式输出
{format_instructions}
'''
prompt = ChatPromptTemplate.from_template(template=review_template2)
#这里的text和format_instructions参数都是源于review_template2中的{}
messages = prompt.format_messages(text=customer_review,format_instructions=format_instructions)
res = model(messages)
print(res.content)
#打印结果:以下是根据您的要求提取的信息,并以json格式输出:
#```json
#{
# "礼物": "是",
# "价格": "399元"
#}`
output_dict = output_parser.parse(res.content)
print(output_dict.get('礼物'))
#打印结果:是
print(output_dict.get('价格'))
#打印结果:399元
学习产出:
- 初步入门langchain
- 学会prompt
- 学会解析器
下一步计划:
学习langchain中的memory机制