代码拉取完成,页面将自动刷新
from langchain_openai import ChatOpenAI
import jwt
import time
from langchain_core.messages import AIMessage, HumanMessage, SystemMessage
zhipuai_api_key = "bdc59e310deb29d48e6be230d487c518.n88YR9GP76XUePoL"
def generate_token(apikey: str, exp_seconds: int):
try:
id, secret = apikey.split(".")
except Exception as e:
raise Exception("invalid apikey", e)
payload = {
"api_key": id,
"exp": int(round(time.time() * 1000)) + exp_seconds * 1000,
"timestamp": int(round(time.time() * 1000)),
}
return jwt.encode(
payload,
secret,
algorithm="HS256",
headers={"alg": "HS256", "sign_type": "SIGN"},
)
zhipu_llm = ChatOpenAI(
model_name="glm-4",
openai_api_base="https://open.bigmodel.cn/api/paas/v4",
openai_api_key=generate_token(zhipuai_api_key,3600 * 10),
streaming=False,
verbose=True
)
# messages = [
# # AIMessage(content="Hi."),
# # SystemMessage(content="Your role is a poet."),
# # HumanMessage(content="深圳2008年的GDP多少亿"),
# HumanMessage(content="only give me the result,no other words:the result of add 3 to 4"),
# ]
# response = zhipu_llm.invoke(messages)
# print(response)
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。