【Langchain大语言模型开发教程】模型、提示和解析

🔗 LangChain for LLM Application Development - DeepLearning.AI

学习目标

1、使用Langchain实例化一个LLM的接口

2、 使用Langchain的模板功能,将需要改动的部分抽象成变量,在具体的情况下替换成需要的内容,来达到模板复用效果。

3、使用Langchain提供的解析功能,将LLM的输出解析成你需要的格式,如字典。

模型实例化

python 复制代码
import os
from dotenv import load_dotenv ,find_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate
python 复制代码
_ = load_dotenv((find_dotenv())) //使用dotenv来管理你的环境变量

我们选用智谱的API【智谱AI开放平台】来作为我们的基座大模型,通过langchain的chatOpenAI接口来实例化我们的模型。

python 复制代码
chat = ChatOpenAI(api_key=os.environ.get('ZHIPUAI_API_KEY'),
                         base_url=os.environ.get('ZHIPUAI_API_URL'),
                         model="glm-4",
                         temperature=0.98)

这里我们选用的一个例子:通过prompt来转换表达的风格

提示模板化

我们定义一个prompt

python 复制代码
template_string = """Translate the text \
that is delimited by triple backticks \
into a style that is {style}.\
text:```{text}```
"""

使用langchain的模板功能函数实例化一个模板(从输出可以看到这里是需要两个参数style和text)

python 复制代码
prompt_template = ChatPromptTemplate.from_template(template_string)

'''
ChatPromptTemplate(input_variables=['style', 'text'], 
messages=[HumanMessagePromptTemplate(prompt=PromptTemplate(
input_variables=['style', 'text'], 
template='Translate the text that is delimited 
by triple backticks into a style that is {style}.text:```{text}```\n'))])
'''

设置我们想要转化的风格和想要转化的内容

python 复制代码
#style
customer_style = """American English in a clam and respectful tone"""
#text
customer_email = """
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 yer help \
right now,matey!
"""

这里我们实例化出我们的prompt

python 复制代码
customer_messages = prompt_template.format_messages(style = customer_style,text= customer_email)

'''
[HumanMessage(content="Translate the text that is delimited 
by triple backticks into a style 
that is American English in a clam and respectful tone.
text:
```\n
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 yer help right now,matey!
\n```\n")]
'''

这里我们给出一个回复的内容和转化的格式

python 复制代码
service_reply= 
"""
Hey there customer,the warranty does 
not cover cleaning expenses for your kitchen 
because it's your fault that you misused your blender 
by forgetting to put the lid on before starting the blender.
Tough luck! see ya!
"""

service_style = """
a polite tone that speaks in English pirate
"""

实例化

python 复制代码
service_messages = prompt_template.format_messages(style = service_style , text = service_reply)

调用LLM查看结果

python 复制代码
service_response = chat(service_messages)
print(service_response.content)

'''
Avast there, dear customer! Ye be knowin' that the warranty 
be not stretchin' to cover the cleanin' costs of yer kitchen, 
for 'tis a matter of misadventure on yer part. 
Ye did forget to secure the lid upon the blender before engagement, 
leading to a spot o' trouble. Aar, 
such be the ways of the sea! 
No hard feelings, and may the wind be at yer back on the next journey. 
Fare thee well!
'''

回复结构化

我们现在获得了某个商品的用户评价,我们想要提取其中的关键信息(下面这种形式)

python 复制代码
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's \
anniversary 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 lawn. \
It's slightly more expensive than the other leaf blowers \
out there, but I think it's worth it for the extra features.
"""

{
  "gift": False,
  "delivery_days": 5,
  "price_value": "pretty affordable!"
}

构建一个prompt 模板

python 复制代码
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,\
and output them as a comma separated Python list.

Format the output as JSON with the following keys:
gift
delivery_days
price_value

text: {text}
"""
python 复制代码
prompt_template = ChatPromptTemplate.from_template(review_template)
message = prompt_template.format_messages(text = customer_review)
reponse = chat(message)

下面是模型的回复看起来好像一样

python 复制代码
{
  "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."]
}

我们打印他的类型的时候,发现这其实是一个字符串类型,这是不能根据key来获取value值的。

引入Langchain的ResponseSchema

python 复制代码
from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParser

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, and output them as a comma separated Python list.")
python 复制代码
response_schemas = [gift_schema,delivery_days_schema,price_value_schema]
output_parser = StructuredOutputParser.from_response_schemas(response_schemas)
format_instructions = output_parser.get_format_instructions()

查看一下我们构建的这个结构

重新构建prompt模板,并进行实例

python 复制代码
review_template_2 = """\
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_instructions}
"""

prompt = ChatPromptTemplate.from_template(template=review_template_2)

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

我们将结果进行解析

python 复制代码
output_dict = output_parser.parse(reponse.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."
}

我们再次查看其类型,发现已经变成了字典类型,并可以通过key去获取value值。

相关推荐
羊小猪~~4 分钟前
神经网络基础--什么是正向传播??什么是方向传播??
人工智能·pytorch·python·深度学习·神经网络·算法·机器学习
AI小杨5 分钟前
【车道线检测】一、传统车道线检测:基于霍夫变换的车道线检测史诗级详细教程
人工智能·opencv·计算机视觉·霍夫变换·车道线检测
晨曦_子画10 分钟前
编程语言之战:AI 之后的 Kotlin 与 Java
android·java·开发语言·人工智能·kotlin
道可云12 分钟前
道可云人工智能&元宇宙每日资讯|2024国际虚拟现实创新大会将在青岛举办
大数据·人工智能·3d·机器人·ar·vr
Yawesh_best19 分钟前
思源笔记轻松连接本地Ollama大语言模型,开启AI写作新体验!
笔记·语言模型·ai写作
人工智能培训咨询叶梓21 分钟前
探索开放资源上指令微调语言模型的现状
人工智能·语言模型·自然语言处理·性能优化·调优·大模型微调·指令微调
zzZ_CMing21 分钟前
大语言模型训练的全过程:预训练、微调、RLHF
人工智能·自然语言处理·aigc
newxtc22 分钟前
【旷视科技-注册/登录安全分析报告】
人工智能·科技·安全·ddddocr
成都古河云23 分钟前
智慧场馆:安全、节能与智能化管理的未来
大数据·运维·人工智能·安全·智慧城市
UCloud_TShare26 分钟前
浅谈语言模型推理框架 vLLM 0.6.0性能优化
人工智能