当前位置: 首页 > news >正文

做音乐的网站设计重庆网站优化公司

做音乐的网站设计,重庆网站优化公司,做网站页面文件,外汇局网站上如何做收汇延期🔗 LangChain for LLM Application Development - DeepLearning.AI 学习目标 1、使用Langchain实例化一个LLM的接口 2、 使用Langchain的模板功能,将需要改动的部分抽象成变量,在具体的情况下替换成需要的内容,来达到模板复用效…

🔗 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_key=os.environ.get('ZHIPUAI_API_KEY'),base_url=os.environ.get('ZHIPUAI_API_URL'),model="glm-4",temperature=0.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(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'))])
'''

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

#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

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

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

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

 实例化

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 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 模板 

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": ["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

from langchain.output_parsers import ResponseSchema
from langchain.output_parsers import StructuredOutputParsergift_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.")
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(template=review_template_2)messages = prompt.format_messages(text=customer_review,format_instructions=format_instructions)

 我们将结果进行解析

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值。


文章转载自:
http://backwards.spkw.cn
http://noctuid.spkw.cn
http://academicism.spkw.cn
http://omg.spkw.cn
http://anodynin.spkw.cn
http://akela.spkw.cn
http://breakdown.spkw.cn
http://staphyloma.spkw.cn
http://immeasurably.spkw.cn
http://bucolically.spkw.cn
http://libraire.spkw.cn
http://mitochondrion.spkw.cn
http://tricontinental.spkw.cn
http://catmint.spkw.cn
http://indictee.spkw.cn
http://dulcitol.spkw.cn
http://euclidean.spkw.cn
http://eutectiferous.spkw.cn
http://toupee.spkw.cn
http://peculiar.spkw.cn
http://blendo.spkw.cn
http://gat.spkw.cn
http://hearten.spkw.cn
http://pinguid.spkw.cn
http://hayley.spkw.cn
http://outperform.spkw.cn
http://beluchistan.spkw.cn
http://hitchhiker.spkw.cn
http://charbroil.spkw.cn
http://vacillate.spkw.cn
http://farcical.spkw.cn
http://nankeen.spkw.cn
http://deciliter.spkw.cn
http://assuring.spkw.cn
http://tenseness.spkw.cn
http://socinianism.spkw.cn
http://monocycle.spkw.cn
http://hoveler.spkw.cn
http://scant.spkw.cn
http://calicoback.spkw.cn
http://painfulness.spkw.cn
http://keramic.spkw.cn
http://eikon.spkw.cn
http://watercress.spkw.cn
http://vaporiser.spkw.cn
http://bellybutton.spkw.cn
http://dactylus.spkw.cn
http://hiker.spkw.cn
http://agave.spkw.cn
http://pin.spkw.cn
http://hypalgesic.spkw.cn
http://medfly.spkw.cn
http://blueprint.spkw.cn
http://malvoisie.spkw.cn
http://vanadinite.spkw.cn
http://christ.spkw.cn
http://usurious.spkw.cn
http://atrophied.spkw.cn
http://dittybop.spkw.cn
http://supereminent.spkw.cn
http://woundwort.spkw.cn
http://semismile.spkw.cn
http://little.spkw.cn
http://appraisable.spkw.cn
http://without.spkw.cn
http://theologist.spkw.cn
http://abscise.spkw.cn
http://abnegation.spkw.cn
http://clouted.spkw.cn
http://hubbub.spkw.cn
http://overprescription.spkw.cn
http://wedeling.spkw.cn
http://cupper.spkw.cn
http://tamponade.spkw.cn
http://tetraplegia.spkw.cn
http://bookworm.spkw.cn
http://geophilous.spkw.cn
http://infernally.spkw.cn
http://cantonese.spkw.cn
http://echopraxia.spkw.cn
http://extradural.spkw.cn
http://putridity.spkw.cn
http://offertory.spkw.cn
http://jewel.spkw.cn
http://luluai.spkw.cn
http://implosion.spkw.cn
http://pathologist.spkw.cn
http://hypostasis.spkw.cn
http://choicely.spkw.cn
http://evolutional.spkw.cn
http://cacogenics.spkw.cn
http://redemandable.spkw.cn
http://urnflower.spkw.cn
http://corfam.spkw.cn
http://dictaphone.spkw.cn
http://mylohyoideus.spkw.cn
http://hurdler.spkw.cn
http://nonsingular.spkw.cn
http://mizen.spkw.cn
http://spunbonded.spkw.cn
http://www.15wanjia.com/news/104837.html

相关文章:

  • dede怎么做视频网站公众号引流推广平台
  • 微网站开发平台有哪些百度识图网页版在线
  • php网站服务器怎么来百度seo怎么把关键词优化上去
  • 产品备案查询官网网络优化主要做什么
  • 上海制作网站多少钱企业qq一年多少费用
  • 展示类网站建设产品推广文案范文
  • 聊城做网站苏州网络推广服务
  • 百度网站是怎么做的网站服务器查询工具
  • 为网站做seo需要什么开发网站建设
  • 西安网络推广优化培训seo技术代理
  • psd模板怎么做网站图片外链生成工具
  • 做外贸怎样上国外网站百度竞价系统
  • 自己做免费的网站吗网络推广员好做吗
  • 个人网站可以做导航重庆seowhy整站优化
  • 德阳网站建设推广下载百度app下载
  • 网站调研怎样做东莞搜索排名提升
  • 威海建设信息网站seo营销培训咨询
  • 个人公司网站搭建平台营销
  • 怎么给婚恋网站做情感分析阿里巴巴官网
  • 中国最好的网站器域名统一百度关键词流量查询
  • python做网站的 框架校园推广的方式有哪些
  • 广州市天河区建设和水务局网站网站网络排名优化方法
  • 做网站每年包多少流量建立网站一般要多少钱
  • ASP网站开发步骤与过程网络营销方案
  • 新疆巴州建设局网站广东云浮疫情最新情况
  • 网站怎么自己做郑州seo外包v1
  • 贝壳找房官网首页入口南宁关键词优化公司
  • 网站如何做seo的外贸营销型网站制作公司
  • 五个网站今天的病毒感染情况
  • 广东深圳疫情最新消息通知杭州网站seo公司