1 Star 0 Fork 0

贺辉0912/bigmodel

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
ChatGLM_new.py 1.29 KB
一键复制 编辑 原始数据 按行查看 历史
andy.he 提交于 2024-03-02 17:18 . 1
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)
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/he-hui-0912/bigmodel.git
git@gitee.com:he-hui-0912/bigmodel.git
he-hui-0912
bigmodel
bigmodel
master

搜索帮助

0d507c66 1850385 C8b1a773 1850385