一、 用智谱AI依赖包调用
from zhipuai import ZhipuAI
zhipuai_api_key='XXXXXXXXXXXXXXXXXX'
messages=[
{"role": "system", "content": "你是一名经验丰富的人工智能工程师,请你解答用户的问题"},
{"role": "user", "content": "请你为我解释什么是RAG技术?"}
]
client = ZhipuAI(api_key=zhipuai_api_key)
response = client.chat.completions.create(
model="glm-4",
temperature=0.5,
messages=messages
)
print(response.choices[0].message)
二、 用langchain调用
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
chat = ChatZhipuAI(
zhipuai_api_key = zhipuai_api_key,
model="glm-4",
temperature=0.5,
)
chat.invoke('请你介绍一下自己')
messages = [
SystemMessage(content="你是一位诗人"),
HumanMessage(content="请给我写一首关于小鸭子的古诗"),
]
response = chat.invoke(messages)
print(response.content)