赞
踩
from langchain.chains import LLMChain from langchain_core.prompts import ChatPromptTemplate from langchain_community.chat_models import ChatZhipuAI # from langchain_community.chat_models import ChatOpenAI 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"}, ) chat = ChatOpenAI( model_name="glm-4", openai_api_base="https://open.bigmodel.cn/api/paas/v4", openai_api_key=generate_token(zhipuai_api_key,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 = chat.invoke(messages) print(response)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。