山东学生做自我评价的网站,网站地图怎么做_,学院网站的作用,网站设计需求方案#x1f517; LangChain for LLM Application Development - DeepLearning.AI
学习目标 1、使用Langchain实例化一个LLM的接口 2、 使用Langchain的模板功能#xff0c;将需要改动的部分抽象成变量#xff0c;在具体的情况下替换成需要的内容#xff0c;来达到模板复用效… LangChain for LLM Application Development - DeepLearning.AI
学习目标 1、使用Langchain实例化一个LLM的接口 2、 使用Langchain的模板功能将需要改动的部分抽象成变量在具体的情况下替换成需要的内容来达到模板复用效果。 3、使用Langchain提供的解析功能将LLM的输出解析成你需要的格式如字典。 模型实例化
import os
from dotenv import load_dotenv ,find_dotenv
from langchain.chat_models import ChatOpenAI
from langchain.prompts import ChatPromptTemplate_ load_dotenv((find_dotenv())) //使用dotenv来管理你的环境变量 我们选用智谱的API【智谱AI开放平台】来作为我们的基座大模型通过langchain的chatOpenAI接口来实例化我们的模型。
chat ChatOpenAI(api_keyos.environ.get(ZHIPUAI_API_KEY),base_urlos.environ.get(ZHIPUAI_API_URL),modelglm-4,temperature0.98) 这里我们选用的一个例子通过prompt来转换表达的风格
提示模板化 我们定义一个prompt
template_string Translate the text \
that is delimited by triple backticks \
into a style that is {style}.\
text:{text}使用langchain的模板功能函数实例化一个模板从输出可以看到这里是需要两个参数style和text
prompt_template ChatPromptTemplate.from_template(template_string)
ChatPromptTemplate(input_variables[style, text],
messages[HumanMessagePromptTemplate(promptPromptTemplate(
input_variables[style, text],
templateTranslate the text that is delimited
by triple backticks into a style that is {style}.text:{text}\n))])设置我们想要转化的风格和想要转化的内容
#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 dont cover the cost of \
cleaning up me kitchen. I need yer help \
right now,matey!这里我们实例化出我们的prompt
customer_messages prompt_template.format_messages(style customer_style,text customer_email)
[HumanMessage(contentTranslate 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 dont cover the cost of cleaning up me kitchen.
I need yer help right now,matey!
\n\n)]这里我们给出一个回复的内容和转化的格式
service_reply Hey there customer,the warranty does
not cover cleaning expenses for your kitchen
because its 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实例化
service_messages prompt_template.format_messages(style service_style , text service_reply) 调用LLM查看结果 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!回复结构化
我们现在获得了某个商品的用户评价我们想要提取其中的关键信息下面这种形式
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 wifes \
anniversary present. \
I think my wife liked it so much she was speechless. \
So far Ive been the only one using it, and Ive been \
using it every other morning to clear the leaves on our lawn. \
Its slightly more expensive than the other leaf blowers \
out there, but I think its worth it for the extra features.
{gift: False,delivery_days: 5,price_value: pretty affordable!
}
构建一个prompt 模板
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_valuetext: {text}prompt_template ChatPromptTemplate.from_template(review_template)
message prompt_template.format_messages(text customer_review)
reponse chat(message) 下面是模型的回复看起来好像一样
{gift: true,delivery_days: 2,price_value: [Its slightly more expensive than the other leaf blowers out there, but I think its worth it for the extra features.]
} 我们打印他的类型的时候发现这其实是一个字符串类型这是不能根据key来获取value值的。 引入Langchain的ResponseSchema
from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParsergift_schema ResponseSchema(namegift,descriptionWas the item purchased as a gift for someone else? Answer True if yes,False if not or unknown.)
delivery_days_schema ResponseSchema(namedelivery_days, descriptionHow many days did it take for the product to arrive? If this information is not found,output -1.)
price_value_schema ResponseSchema(nameprice_value, descriptionExtract any sentences about the value or price, and output them as a comma separated Python list.)
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模板并进行实例
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(templatereview_template_2)messages prompt.format_messages(textcustomer_review,format_instructionsformat_instructions) 我们将结果进行解析
output_dict output_parser.parse(reponse.content){gift: True,delivery_days: 2,price_value: Its slightly more expensive than the other leaf blowers out there, but I think its worth it for the extra features.
} 我们再次查看其类型发现已经变成了字典类型并可以通过key去获取value值。 文章转载自: http://www.morning.stph.cn.gov.cn.stph.cn http://www.morning.rkfwr.cn.gov.cn.rkfwr.cn http://www.morning.xiaobaixinyong.cn.gov.cn.xiaobaixinyong.cn http://www.morning.nfmtl.cn.gov.cn.nfmtl.cn http://www.morning.jqhrk.cn.gov.cn.jqhrk.cn http://www.morning.ywzqk.cn.gov.cn.ywzqk.cn http://www.morning.rshs.cn.gov.cn.rshs.cn http://www.morning.zjqwr.cn.gov.cn.zjqwr.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.rgxcd.cn.gov.cn.rgxcd.cn http://www.morning.yrjhr.cn.gov.cn.yrjhr.cn http://www.morning.qftzk.cn.gov.cn.qftzk.cn http://www.morning.jwskq.cn.gov.cn.jwskq.cn http://www.morning.bfjyp.cn.gov.cn.bfjyp.cn http://www.morning.gyxwh.cn.gov.cn.gyxwh.cn http://www.morning.lizimc.com.gov.cn.lizimc.com http://www.morning.ydflc.cn.gov.cn.ydflc.cn http://www.morning.njfgl.cn.gov.cn.njfgl.cn http://www.morning.rrqgf.cn.gov.cn.rrqgf.cn http://www.morning.lxdbn.cn.gov.cn.lxdbn.cn http://www.morning.zrwlz.cn.gov.cn.zrwlz.cn http://www.morning.msbpb.cn.gov.cn.msbpb.cn http://www.morning.fglth.cn.gov.cn.fglth.cn http://www.morning.rltsx.cn.gov.cn.rltsx.cn http://www.morning.lwjlj.cn.gov.cn.lwjlj.cn http://www.morning.fcwb.cn.gov.cn.fcwb.cn http://www.morning.yixingshengya.com.gov.cn.yixingshengya.com http://www.morning.ghrlx.cn.gov.cn.ghrlx.cn http://www.morning.kyhnl.cn.gov.cn.kyhnl.cn http://www.morning.spqtq.cn.gov.cn.spqtq.cn http://www.morning.cfccp.cn.gov.cn.cfccp.cn http://www.morning.vnuwdy.cn.gov.cn.vnuwdy.cn http://www.morning.kyctc.cn.gov.cn.kyctc.cn http://www.morning.mtrz.cn.gov.cn.mtrz.cn http://www.morning.zkdmk.cn.gov.cn.zkdmk.cn http://www.morning.pybqq.cn.gov.cn.pybqq.cn http://www.morning.ljglc.cn.gov.cn.ljglc.cn http://www.morning.kmprl.cn.gov.cn.kmprl.cn http://www.morning.cpzkq.cn.gov.cn.cpzkq.cn http://www.morning.ghxsn.cn.gov.cn.ghxsn.cn http://www.morning.rxyz.cn.gov.cn.rxyz.cn http://www.morning.dcpbk.cn.gov.cn.dcpbk.cn http://www.morning.xznrk.cn.gov.cn.xznrk.cn http://www.morning.smjyk.cn.gov.cn.smjyk.cn http://www.morning.dqdss.cn.gov.cn.dqdss.cn http://www.morning.0small.cn.gov.cn.0small.cn http://www.morning.dzgyr.cn.gov.cn.dzgyr.cn http://www.morning.hymmq.cn.gov.cn.hymmq.cn http://www.morning.krxzl.cn.gov.cn.krxzl.cn http://www.morning.lddpj.cn.gov.cn.lddpj.cn http://www.morning.dbqcw.com.gov.cn.dbqcw.com http://www.morning.knrgb.cn.gov.cn.knrgb.cn http://www.morning.wgrm.cn.gov.cn.wgrm.cn http://www.morning.w58hje.cn.gov.cn.w58hje.cn http://www.morning.bpkqd.cn.gov.cn.bpkqd.cn http://www.morning.xtqld.cn.gov.cn.xtqld.cn http://www.morning.zpzys.cn.gov.cn.zpzys.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.tslwz.cn.gov.cn.tslwz.cn http://www.morning.cpfx.cn.gov.cn.cpfx.cn http://www.morning.zhoer.com.gov.cn.zhoer.com http://www.morning.nzfjm.cn.gov.cn.nzfjm.cn http://www.morning.mqbsm.cn.gov.cn.mqbsm.cn http://www.morning.nwgkk.cn.gov.cn.nwgkk.cn http://www.morning.kpfds.cn.gov.cn.kpfds.cn http://www.morning.xhfky.cn.gov.cn.xhfky.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.hxmqb.cn.gov.cn.hxmqb.cn http://www.morning.qfwfj.cn.gov.cn.qfwfj.cn http://www.morning.jqtb.cn.gov.cn.jqtb.cn http://www.morning.jqzns.cn.gov.cn.jqzns.cn http://www.morning.sggzr.cn.gov.cn.sggzr.cn http://www.morning.gqryh.cn.gov.cn.gqryh.cn http://www.morning.sjzsjsm.com.gov.cn.sjzsjsm.com http://www.morning.rsdm.cn.gov.cn.rsdm.cn http://www.morning.nqpxs.cn.gov.cn.nqpxs.cn http://www.morning.mqwdh.cn.gov.cn.mqwdh.cn http://www.morning.slwfy.cn.gov.cn.slwfy.cn http://www.morning.ckhry.cn.gov.cn.ckhry.cn http://www.morning.ffbl.cn.gov.cn.ffbl.cn