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

LangChain实践1-使用 LangChain 开发应用程序

通过LangChain使用OpenAI

下载

pip install langchain_community

模型

from langchain.chat_models import ChatOpenAI

# 这里我们将参数temperature设置为0.0,从而减少生成答案的随机性。

# 如果你想要每次得到不一样的有新意的答案,可以尝试调整该参数。

chat = ChatOpenAI(temperature=0.0,api_key="sk-xxx", base_url="https://xxx")

chat

from langchain.prompts import ChatPromptTemplate

# 首先,构造一个提示模版字符串:`template_string`

template_string = """把由三个反引号分隔的文本\

翻译成一种{style}风格。\

文本: ```{text}```

"""

# 然后,我们调用`ChatPromptTemplatee.from_template()`函数将

# 上面的提示模版字符`template_string`转换为提示模版`prompt_template`

prompt_template = ChatPromptTemplate.from_template(template_string)

print("\n", prompt_template.messages[0].prompt)

提示模版 prompt_template 需要两个输入变量: customer_style : 我们想要的顾客邮件风格 customer_email : 顾客的原始邮件文本。

customer_style = """正式普通话 \

用一个平静、尊敬的语气

"""

customer_email = """

style 和

prompt_template 的

text 。 这里分别对应

嗯呐,我现在可是火冒三丈,我那个搅拌机盖子竟然飞了出去,把我厨房的墙壁都溅上了果汁!

更糟糕的是,保修条款可不包括清理我厨房的费用。

伙计,赶紧给我过来!

"""

# 使用提示模版

customer_messages = prompt_template.format_messages(

style=customer_style,

text=customer_email)

# 打印客户消息类型

print("客户消息类型:",type(customer_messages),"\n")

# 打印第一个客户消息类型

print("第一个客户客户消息类型类型:", type(customer_messages[0]),"\n")

# 打印第一个元素

print("第一个客户客户消息类型类型: ", customer_messages[0],"\n")

现在我们可以调用模型部分定义的 chat 模型来实现转换客户消息风格。

customer_response = chat(customer_messages)

print(customer_response.content)

用海盗方言回复邮件

service_reply = """嘿,顾客, \

保修不包括厨房的清洁费用, \

因为您在启动搅拌机之前 \

忘记盖上盖子而误用搅拌机, \

这是您的错。 \

倒霉! 再见!

"""

service_style_pirate = """\

一个有礼貌的语气 \

使用海盗风格\

"""

service_messages = prompt_template.format_messages(

style=service_style_pirate,

text=service_reply)

print("\n", service_messages[0].content)

# 调用模型部分定义的chat模型来转换回复消息风格

service_response = chat(service_messages)

print(service_response.content)

输出解析器

不使用输出解释器提取客户评价中的信息

from langchain.prompts import ChatPromptTemplate

customer_review = """\

这款吹叶机非常神奇。 它有四个设置:\

吹蜡烛、微风、风城、龙卷风。 \

两天后就到了,正好赶上我妻子的\

周年纪念礼物。 \

我想我的妻子会喜欢它到说不出话来。 \

到目前为止,我是唯一一个使用它的人,而且我一直\

每隔一天早上用它来清理草坪上的叶子。 \

它比其他吹叶机稍微贵一点,\

但我认为它的额外功能是值得的。

"""

review_template = """\

对于以下文本,请从中提取以下信息:

礼物:该商品是作为礼物送给别人的吗? \

如果是,则回答 是的;如果否或未知,则回答 不是。

交货天数:产品需要多少天\

到达? 如果没有找到该信息,则输出-1。

价钱:提取有关价值或价格的任何句子,\

并将它们输出为逗号分隔的 Python 列表。

使用以下键将输出格式化为 JSON:

礼物

交货天数

价钱

文本: {text}

"""

prompt_template = ChatPromptTemplate.from_template(review_template)

print("提示模版:", prompt_template)

messages = prompt_template.format_messages(text=customer_review)

response = chat(messages)

print("结果类型:", type(response.content))

print("结果:", response.content)

使用输出解析器提取客户评价中的信息

review_template_2 = """\

对于以下文本,请从中提取以下信息::

礼物:该商品是作为礼物送给别人的吗?

如果是,则回答 是的;如果否或未知,则回答 不是。

交货天数:产品到达需要多少天? 如果没有找到该信息,则输出-1。

dict ), 如果想要从中更方便的提取信息,我们需要

价钱:提取有关价值或价格的任何句子,并将它们输出为逗号分隔的 Python 列表。

文本: {text}

{format_instructions}

"""

prompt = ChatPromptTemplate.from_template(template=review_template_2)

from langchain.output_parsers import ResponseSchema

from langchain.output_parsers import StructuredOutputParser

gift_schema = ResponseSchema(name="礼物",

                         description="这件物品是作为礼物送给别人的吗?\

                        如果是,则回答 是的,\

                        如果否或未知,则回答 不是。")

delivery_days_schema = ResponseSchema(name="交货天数",

                                  description="产品需要多少天才能到达?\

                                  如果没有找到该信息,则输出-1。")

price_value_schema = ResponseSchema(name="价钱",

                                description="提取有关价值或价格的任何句子,\

                                并将它们输出为逗号分隔的 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()

print("输出格式规定:",format_instructions)

messages = prompt.format_messages(text=customer_review,

format_instructions=format_instructions)

print("第一条客户消息:",messages[0].content)

response = chat(messages)

print("结果类型:", type(response.content))

print("结果:", response.content)

output_dict = output_parser.parse(response.content)

print("解析后的结果类型:", type(output_dict))

print("解析后的结果:", output_dict)


http://www.kler.cn/a/534456.html

相关文章:

  • Hugging Face 的研究人员正致力于打造 OpenAI 深度研究工具的“开源版
  • 数据结构-堆和PriorityQueue
  • github下载失败网页打开失败 若你已经知道github地址如何cmd下载
  • 怀旧经典:1200+款红白机游戏合集,Windows版一键畅玩
  • 人类心智逆向工程:AGI的认知科学基础
  • 谈谈你所了解的AR技术吧!
  • 基于遗传算法的256QAM星座图的最优概率整形matlab仿真,对比优化前后整形星座图和误码率
  • 深入理解k8s中的容器存储接口(CSI)
  • 0 帧起步,腾讯云 TI 平台 5 分钟 私有化 DeepSeek
  • 51单片机 04 编程
  • 【Elasticsearch】 Composite Aggregation 详解
  • React中key值的正确使用指南:为什么需要它以及如何选择
  • 【MySql】数据库Redo日志介绍
  • Slint的学习
  • 文本生图的提示词prompt和参数如何设置(基于Animagine XL V3.1)
  • 2025.2.5总结
  • Deepseek v3R1 学习笔记
  • 孟加拉国_行政边界省市边界arcgis数据shp格式wgs84坐标
  • 网络爬虫js逆向之某音乐平台案例
  • input 超出maxlength限制后,输入框变红
  • 9-收纳的知识
  • LabVIEW自定义测量参数怎么设置?
  • 基于springboot的体质测试数据分析及可视化设计
  • 教程 | i.MX RT1180 ECAT_digital_io DEMO 搭建(一)
  • 2025.2.5
  • 【分布式】服务端高并发分布式结构演进