参考文档
– https://cloud.baidu.com/qianfandev/topic/267899
– https://python.langchain.com/v0.1/docs/integrations/llms/tongyi/
运行环境
本文档的内容是 .ipynb 文件格式,因此可以在 Google Colab 上运行。
当然也可以在安装了 Jupter 插件的 VSCode 下运行。
因为本文使用的是国内通义千问的模型,因此不用科学上网也可以执行。
源码
https://gitee.com/tanggaowei/langchain-llm-app-dev
安装相关模块
%pip install python-dotenv
%pip install --upgrade langchain
%pip install --upgrade langchain_community
%pip install --upgrade --quiet dashscope
加载环境变量
from pathlib import Path
from dotenv import load_dotenv, find_dotenv
# 加载环境变量,每次都覆盖旧数据
env_path = Path(‘.’) / ‘test.env’
load_dotenv(dotenv_path=env_path, override=True)
test.env 中保存了环境变量的值:
DASHSCOPE_API_KEY=sk-***
初始化大语言模型对象
import os
from langchain_community.llms import Tongyi
Tongyi().invoke(“哪支NFL球队在贾斯汀·比伯出生的那一年赢得了超级碗?”)
使用大模型翻译文本
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!
"""
language = “chinese”
prompt = f”””Translate the text \
into {language}.
text: “`{customer_email}“`
“””
print(prompt)
response = Tongyi().invoke(prompt)
print(response)
通过LangChain的方式来翻译文本
from langchain.prompts import PromptTemplate
llm = Tongyi()
template = “””Translate the text \
into {language}. \
text: “`{text}“`
“””
prompt = PromptTemplate.from_template(template)
chain = prompt | llm
language = “chinese”
text = “””
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!
“””
response = chain.invoke({“language”: “chinese”, “text”: text})
print(response)
response = chain.invoke({“language”: “janpnese”, “text”: text})
print(response)
通过解析器来解析模型输出
解析器的作用就是将一定格式的文本(例如json文本),解析成python对象(例如dict类型对象),以便进一步处理。
注意:解析器并不是格式化文本的工具,它是将已经格式化的文本转化为python对象。
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, without ““`”:
gift
delivery_days
price_value
text: {text}
“””
from langchain.prompts import ChatPromptTemplate
prompt = ChatPromptTemplate.from_template(review_template)
chain = prompt | llm
response = chain.invoke({“text”:customer_review})
print(response)
from langchain.output_parsers import ResponseSchema, StructuredOutputParser
# description 相当于字段的描述,不影响解析结果
gift_schema = ResponseSchema(name=”gift”,
type=”bool”,
description=”是否为礼物”)
delivery_days_schema = ResponseSchema(name=”delivery_days”,
type=”string”,
description=”几天到货”)
price_value_schema = ResponseSchema(name=”price_value”,
type=”string”,
description=”价值或价格内容”)
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)
prompt = ChatPromptTemplate.from_template(template=review_template,format_instructions=format_instructions)
chain = prompt | llm
response = chain.invoke({“text”:customer_review})
print(response)
# 将字符串格式化为 dict 类型变量
output_dict = output_parser.parse(response)
print(output_dict)
print(output_dict.get(‘gift’))