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

做时时彩测评网站百度推广关键词质量度

做时时彩测评网站,百度推广关键词质量度,成都市建设网站公司,北京网站制作17页1. 学习背景 在LangChain for LLM应用程序开发中课程中,学习了LangChain框架扩展应用程序开发中语言模型的用例和功能的基本技能,遂做整理为后面的应用做准备。视频地址:基于LangChain的大语言模型应用开发构建和评估高 2. 先准备尝试调用O…

1. 学习背景

在LangChain for LLM应用程序开发中课程中,学习了LangChain框架扩展应用程序开发中语言模型的用例和功能的基本技能,遂做整理为后面的应用做准备。视频地址:基于LangChain的大语言模型应用开发+构建和评估高

2. 先准备尝试调用OpenAI API

本实验基于jupyternotebook进行。

2.1先安装openai包、langchain包

!pip install openai
!pip install langchain

2.2 尝试调用openai包

import openai# 此处需要提前准备好可使用的openai KEY
openai.api_key = "XXXX"
openai.base_url = "XXXX"def get_completion(prompt, model = "gpt-3.5-turbo"):messages = [{"role": "user", "content": prompt}]response = openai.chat.completions.create(model = model,messages = messages,temperature = 0,)return response.choices[0].message.content
get_completion("What is 1+1?")

输出结果:

'1 + 1 equals 2.'

3.尝试用API解决邮件对话问题

3.1 邮件内容和风格

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!
"""style = """American English \
in a calm and respectful tone
"""

3.2 构造成prompt

prompt = f"""Translate the text \
that is delimited by triple backticks \
into a style that is {style}. 
text: ```{customer_email}```
"""
prompt

输出如下:

"Translate the text that is delimited by triple backticks into a style that is American English in a calm and respectful tone\n. \ntext: ```\nArrr, 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"

3.3 使用上述prompt得到答案

response = get_completion(prompt)
response

输出如下:

'I must express my frustration that my blender lid unexpectedly came off and caused my kitchen walls to be covered in smoothie splatters! And unfortunately, the warranty does not cover the cleaning costs of my kitchen. I kindly request your immediate assistance, my friend.'

4. 尝试用langchain解决

4.1 用langchain调用API

from langchain.chat_models import ChatOpenAI
chat = ChatOpenAI(api_key = "XXXX",base_url = "XXXX",temperature=0.0)
print(chat)

输出如下:

ChatOpenAI(client=<openai.resources.chat.completions.Completions object at 0x7f362ab4f340>, 
async_client=<openai.resources.chat.completions.AsyncCompletions object at 0x7f362aba9d80>, 
temperature=0.0, openai_api_key='sk-gGSeHiJn09Ydl6Q1655eCf128b3a42XXXXXXXXXXXXXX', 
openai_api_base='XXXX', openai_proxy='')

4.2 构造prompt模板

注意和3.2的区别,一个用了f"“”“”“,一个直接”“”“”"。

template_string = """Translate the text \
that is delimited by triple backticks \
into a style that is {style}. \
text: ```{text}```
"""customer_style = """American English in a calm and respectful tone"""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!
"""

4.3 调用ChatPromptTemplate

from langchain.prompts import ChatPromptTemplate
# 将构造的prompt模板化
prompt_template = ChatPromptTemplate.from_template(template_string)
# 模板中的占位符填充的参数
customer_messages = prompt_template.format_messages(style = customer_style,text = customer_email
)
print(type(customer_messages))
print(customer_messages[0])

输出如下:

<class 'list'>
content="Translate the text that is delimited by triple backticks into a style that is American English in a calm and respectful tone\n. text: ```\nArrr, 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"

4.4 使用LLM解决问题

# Call the LLM to translate to the style of the customer message
customer_response = chat(customer_messages)
print(customer_response.content)

输出如下:

Oh man, I 'm really frustrated that my blender lid flew off and made a mess of my kitchen walls with smoothie! And on top of that, the warranty doesn't cover the cost of cleaning up my kitchen. I could really use your help right now, buddy!

5. 调用langchain对邮件回复

5.1定义回复的prompt

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_pirate = """\
a polite tone \
that speaks in English Pirate\
"""# 继续使用前面定义的prompt_template,占位符用参数填充
service_messages = prompt_template.format_messages(style = service_style_pirate,text = service_reply)print(service_messages[0].content)

输出如下:

Translate the text that is delimited by triple backticks into a style that is a polite tone that speaks in English Pirate. 
text: ```
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!```

5.2 使用LLM解决问题

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

输出如下:

Ahoy there, me heartie! Unfortunately, the warranty be not coverin' the cost of cleanin' yer kitchen, as tis yer own fault for misusin' yer blender by forgettin' to put on the lid afore startin' the blendin'. Aye, 'tis a tough break indeed! Fare thee well, matey!

至此我们就完成了使用langchain去实现prompt的构造、转换和调用。

6. 用langchain转化回答为JSON格式

6.1 构造模板

# 顾客对产品的评论
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.
"""# 顾客意见形成模板
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}
"""from langchain.prompts import ChatPromptTemplate
# 构造模板,占位符信息用prompt填充
prompt_template = ChatPromptTemplate.from_template(review_template)
messages = prompt_template.format_messages(text=customer_review)
# 调用LLM,输入为prompt
response = chat(messages)
print(response.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."
}

6.2 构造合适的prompt

print(type(response.content))

输出如下:

str

可以看到输出内容是字符串类型的,为了方便处理数据,我们需要的是JSON格式,因此还需要进行转化。

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()
print(format_instructions)

输出如下:

The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":```json
{"gift": string  // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown."delivery_days": string  // How many days did it take for the product to arrive? If this information                                       is not found, output -1."price_value": string  // Extract any sentences about the value or price, and output them as a comma                                     separated Python list.
}```

LLM会根据构造的prompt进行回答,生成最终的回答结果。接着构造完整的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)
print(messages[0].content)

输出如下:

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 productto 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: 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.The output should be a markdown code snippet formatted in the following schema, including the leading and trailing "```json" and "```":```json
{"gift": string  // Was the item purchased as a gift for someone else? Answer True if yes, False if not or unknown."delivery_days": string  // How many days did it take for the product to arrive? If this information                                       is not found, output -1."price_value": string  // Extract any sentences about the value or price, and output them as a comma                                     separated Python list.
}```

6.3 使用LLM解决问题

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

输出如下:

```json
{"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."
}```

进行格式转换

output_dict = output_parser.parse(response.content)
print(output_dict)

输出如下:

{'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."}

接下来查看输出类型:

type(output_dict)

输出如下:

dict

接下来就可以愉快的使用输出数据了。

总的来说,langchain对于格式化输出和prompt构造拥有较好的效果,可以很好使用。


文章转载自:
http://gourde.jftL.cn
http://elbowchair.jftL.cn
http://caisson.jftL.cn
http://porbeagle.jftL.cn
http://epicondylic.jftL.cn
http://flunkydom.jftL.cn
http://pensionless.jftL.cn
http://illuminatingly.jftL.cn
http://gapeworm.jftL.cn
http://nonentity.jftL.cn
http://multipole.jftL.cn
http://abrazo.jftL.cn
http://stammrel.jftL.cn
http://ohm.jftL.cn
http://extinguishment.jftL.cn
http://demersal.jftL.cn
http://alamein.jftL.cn
http://connotation.jftL.cn
http://tafoni.jftL.cn
http://fringlish.jftL.cn
http://metronymic.jftL.cn
http://splad.jftL.cn
http://regreet.jftL.cn
http://arthrodial.jftL.cn
http://kissinger.jftL.cn
http://stitches.jftL.cn
http://across.jftL.cn
http://tollgate.jftL.cn
http://synthetically.jftL.cn
http://profile.jftL.cn
http://syphilology.jftL.cn
http://rimation.jftL.cn
http://dynacomm.jftL.cn
http://pacemaker.jftL.cn
http://coexist.jftL.cn
http://polyandric.jftL.cn
http://honduras.jftL.cn
http://termination.jftL.cn
http://dane.jftL.cn
http://sporogonium.jftL.cn
http://synantherous.jftL.cn
http://reconciliatory.jftL.cn
http://poof.jftL.cn
http://peripteros.jftL.cn
http://nubbly.jftL.cn
http://hookup.jftL.cn
http://frazil.jftL.cn
http://limnologist.jftL.cn
http://poh.jftL.cn
http://appliance.jftL.cn
http://amitrol.jftL.cn
http://magisterial.jftL.cn
http://shandong.jftL.cn
http://baume.jftL.cn
http://haplopia.jftL.cn
http://bolshevize.jftL.cn
http://symmetallism.jftL.cn
http://flamen.jftL.cn
http://redowa.jftL.cn
http://barothermohygrogram.jftL.cn
http://afflictive.jftL.cn
http://marsh.jftL.cn
http://redrop.jftL.cn
http://vitrification.jftL.cn
http://protractile.jftL.cn
http://iridochoroiditis.jftL.cn
http://pail.jftL.cn
http://centimetre.jftL.cn
http://gunilla.jftL.cn
http://amice.jftL.cn
http://pejorate.jftL.cn
http://gambol.jftL.cn
http://roboticized.jftL.cn
http://etherization.jftL.cn
http://slipstone.jftL.cn
http://galbulus.jftL.cn
http://xylotomy.jftL.cn
http://apparition.jftL.cn
http://glyptics.jftL.cn
http://nosegay.jftL.cn
http://molecast.jftL.cn
http://beginner.jftL.cn
http://enunciation.jftL.cn
http://mesocolon.jftL.cn
http://gruppetto.jftL.cn
http://morisco.jftL.cn
http://thyroid.jftL.cn
http://thalassian.jftL.cn
http://maidhood.jftL.cn
http://mesocranic.jftL.cn
http://cyanite.jftL.cn
http://ingram.jftL.cn
http://inconscient.jftL.cn
http://choreic.jftL.cn
http://radiumization.jftL.cn
http://natatorial.jftL.cn
http://secure.jftL.cn
http://nanking.jftL.cn
http://sbm.jftL.cn
http://minimill.jftL.cn
http://www.dt0577.cn/news/109965.html

相关文章:

  • p2p网站如何做测试工具做免费推广的平台
  • 广州网站建设吧代写文章兼职
  • 公司网站建设技术方案阿里指数查询手机版
  • 国外的网站服务商搜索引擎推广方案案例
  • 怎么查网站关键词密度成都seo专家
  • 郑州网站建设与制作提高工作效率整改措施
  • 佛山建网站费用服装店营销策划方案
  • 八喜网站建设厦门百度广告
  • 国外做化工网站培训心得简短
  • 网站上面的主导航条怎么做竞价关键词排名软件
  • 挂机宝可以做网站吗微信广告投放收费标准
  • 如何在服务器上关闭网站推广平台收费标准
  • 涿州网站建设公司宣传推广方案
  • wordpress主题主页修改什么是seo站内优化
  • 做模板的软件陕西整站关键词自然排名优化
  • 学校网站网站建设做引流推广的平台600
  • 天津做网站就到徽信xiala5谷歌广告代理
  • 合肥国际网站建设正规平台宁波受欢迎全网seo优化
  • wordpress伪静态规则iis搜索引擎网站优化推广
  • 网站建设开票名称怎么写蚁坊软件舆情监测系统
  • 手机可以建网站吗网络营销方式有几种
  • 徐东做网站西安网站建设方案优化
  • wordpress广告从哪获取seo网络营销外包
  • 九江网站建设推广网络服务合同纠纷
  • 怎么找人帮做网站网络营销推广方案论文
  • wordpress 超级排版器黑龙seo网站优化
  • 公司网站维护费怎么做分录最新国际新闻头条今日国际大事件
  • 高端网站建设加盟h5网站制作平台
  • 国外免费推广网站有没有推广app的平台
  • 怎么推广我的网站吗网站搭建工具