智谱AI
- 配置参考 https://python.langchain.com/v0.1/docs/integrations/chat/zhipuai/
ZHIPUAI_API_KEY
从https://open.bigmodel.cn/获取
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
import os
os.environ["ZHIPUAI_API_KEY"] = "xxx"
chat = ChatZhipuAI(
model="glm-4",
temperature=0.5,
)
messages = [
# AIMessage(content="Hi."),
# SystemMessage(content="你是一个数学家,只会回答数学问题."),
HumanMessage(content="已知一张桌子的价钱是一把椅子的10倍,又知一张桌子比一把椅子多288元,一张桌子和一把椅子各多少元?"),
]
response = chat.invoke(messages)
print(response.content)
讯飞星火
- 配置参考 https://python.langchain.com/v0.1/docs/integrations/chat/sparkllm/
- 星火3.5用不了,可能跟langchain没打通?
- 星火3.0可以用
from langchain_community.utilities import SQLDatabase
from langchain_community.chat_models import ChatSparkLLM
from langchain_core.messages import HumanMessage
# 星火3.5
# app_id = 'xx'
# api_key = 'xxx'
# api_secret = 'xxx'
# 星火3.0
app_id = 'xx'
api_key = 'xxx'
api_secret = 'xxx'
spark_llm = ChatSparkLLM(
spark_app_id=app_id, spark_api_key=api_key, spark_api_secret=api_secret
)
message = HumanMessage(content="Hello")
spark_llm([message])
通义千问
- 配置参考 https://python.langchain.com/v0.1/docs/integrations/chat/tongyi/
- 配置
api-key
:https://help.aliyun.com/document_detail/611472.html?spm=a2c4g.2399481.0.0
import os
os.environ["DASHSCOPE_API_KEY"] = 'sk-xx'
from langchain_community.chat_models.tongyi import ChatTongyi
from langchain_core.messages import HumanMessage
chatLLM = ChatTongyi(
streaming=True,
)
res = chatLLM.stream([HumanMessage(content="hi")], streaming=True)
for r in res:
print("chat resp:", r)
- 开通就送6个月,200万tokens
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(
content="你是一个数学家,只会回答数学问题."
),
HumanMessage(
content="已知一张桌子的价钱是一把椅子的10倍,又知一张桌子比一把椅子多288元,一张桌子和一把椅子各多少元?"
),
]
print(chatLLM(messages))
content=‘设一把椅子的价格为 \( x \) 元,那么根据题目描述,一张桌子的价格是椅子的10倍,即 \( 10x \) 元。\n\n根据题目中的第二个条件,桌子比椅子多288元,可以得到以下等式:\n\n\[ 10x - x = 288 \]\n\n解这个方程,我们可以找到 \( x \) 的值:\n\n\[ 9x = 288 \]\n\[ x = \frac{288}{9} \]\n\[ x = 32 \]\n\n所以一把椅子的价格是32元,一张桌子的价格就是:\n\n\[ 10x = 10 \times 32 = 320 \]\n\n因此,一张桌子320元,一把椅子32元。’ response_metadata={‘model_name’: ‘qwen-turbo’, ‘finish_reason’: ‘stop’, ‘request_id’: ‘8d3720a4-129d-946f-9631-26ae1ebbf22d’, ‘token_usage’: {‘input_tokens’: 59, ‘output_tokens’: 170, ‘total_tokens’: 229}} id=‘run-93ca5458-feda-4b71-9d20-1a00e591b722-0’
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(
content="你是一个历史学家,只会回答历史问题."
),
HumanMessage(
content="多尔衮是谁?"
),
]
print(chatLLM(messages))
content=‘多尔衮是清朝开国功臣之一,努尔哈赤的第十四子,皇太极的同母弟。在清军入关前,他作为八旗中的镶白旗旗主,参与了多次重要战役,如萨尔浒之战、松锦之战等,对清朝的建立和发展有着重大贡献。在皇太极去世后,多尔衮摄政,辅佐年幼的顺治帝福临,实际上执掌朝政,期间推行了一系列改革和稳定措施。然而,他的权力过大引起了其他满洲贵族的不满,最终在顺治帝亲政后被削去大权,并在顺治五年(1650年)去世,死因存有争议。多尔衮是中国清朝前期的重要人物之一。’ response_metadata={‘model_name’: ‘qwen-turbo’, ‘finish_reason’: ‘stop’, ‘request_id’: ‘dba7bbfc-7bb1-9a1e-9a98-45d3eab7cf38’, ‘token_usage’: {‘input_tokens’: 28, ‘output_tokens’: 164, ‘total_tokens’: 192}} id=‘run-5d90cbfd-ddfc-4abe-a17f-1273a49a2813-0’
from langchain_core.messages import HumanMessage, SystemMessage
messages = [
SystemMessage(
content="你擅长翻译英文到中文,达到信达雅的翻译能力."
),
HumanMessage(
content="Studies serve for delight, for ornament, and for ability. Their chief use for delight, is in privateness and retiring; for ornament, is in discourse; and for ability, is in the judgment and disposition of business."
),
]
print(chatLLM(messages))
content=‘研习之目的,在于怡情、为了装饰,并增长才干。其主要的娱乐作用在于独处之时;用以交谈,则可作为点缀;而要处理事务,还得依赖于判断力和处事才能。’ response_metadata={‘model_name’: ‘qwen-turbo’, ‘finish_reason’: ‘stop’, ‘request_id’: ‘600f49c5-1268-96e6-ad83-61a0ff4a6c57’, ‘token_usage’: {‘input_tokens’: 74, ‘output_tokens’: 48, ‘total_tokens’: 122}} id=‘run-223b18f1-d055-44c6-8880-34b3e6784c19-0’
大家觉得翻译的怎么样?