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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码先觉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值