智谱AI GLM大模型 GLM-4-Plus的快速使用 ChatOpenAI类来调用GLM-4模型
智谱AI
GLM-4,2024年1月16日发布的第四代基座大模型,其整体性能相较前代提升近60%,多维度指标逼近OpenAI的GPT-4水平。该模型支持128K上下文窗口(约300页文本处理能力),在长文本信息处理中实现100%精度召回。https://www.zhipuai.cn/,https://bigmodel.cn/console/overview。
API keys:https://www.bigmodel.cn/usercenter/proj-mgmt/apikeys
Python本地调用
安装: pip install --upgrade zhipuai
from zhipuai import ZhipuAI# 填写您自己的APIKey
client = ZhipuAI(api_key="53cc8cc9db0c42b5800eed3ca93259b8.txLoEReMDXXxxxxx")
response = client.chat.completions.create(model="glm-4-plus", # 填写需要调用的模型编码messages=[{"role": "user", "content": "俄罗斯民族为什么喜欢战争"},],
)
print(response.choices[0].message.content)
from zhipuai import ZhipuAI# 请填写您自己的API Key
client = ZhipuAI(api_key="53cc8cc9db0c42b5800eed3ca93259b8.txLoEReMDXXxxxxx")
response = client.chat.completions.create(model="glm-4-plus", # 填写需要调用的模型编码messages=[# {"role": "user", "content": "你好"},# {"role": "assistant", "content": "我是人工智能助手"},{"role": "user", "content": "俄罗斯民族为什么喜欢战争"},],stream=True,
)
for chunk in response:print(chunk.choices[0].delta.content)
ChatOpenAI类来调用GLM-4模型
LangChain,开源框架,旨在帮助开发者使用大语言模型(LLM)和聊天模型构建端到端的应用程序。它提供了一套工具、组件和接口,以简化创建由这些模型支持的应用程序的过程。官方文档:https://python.langchain.com/
LangChain,核心概念:组件(Components)、链(Chains)、模型输入/输出(Model I/O)、数据连接(Data Connection)、内存(Memory)和代理(Agents)等。
Langchain的ChatOpenAI类是对OpenAI SDK的封装,可以更方便调用。ChatOpenAI类来调用GLM-4模型。
pip install openai -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install --upgrade langchain -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install langchain_openai -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install langchain_community -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install langchainhub
pip install --upgrade httpx httpx-sse PyJWT
import os
from langchain_openai import ChatOpenAI
from langchain.prompts import (ChatPromptTemplate,MessagesPlaceholder,SystemMessagePromptTemplate,HumanMessagePromptTemplate,
)
from langchain.chains import LLMChain
from langchain.memory import ConversationBufferMemoryllm = ChatOpenAI(temperature=0.95,model="glm-4-air-250414",openai_api_key="53cc8cc9db0c42b5800eed3ca93259b8.txLoEReMDXXxxxxx",openai_api_base="https://open.bigmodel.cn/api/paas/v4/"
)
prompt = ChatPromptTemplate(messages=[SystemMessagePromptTemplate.from_template("你是一个很好的聊天机器人,正在与人类对话。"),MessagesPlaceholder(variable_name="chat_history"),HumanMessagePromptTemplate.from_template("{question}")]
)memory = ConversationBufferMemory(memory_key="chat_history", return_messages=True)
conversation = LLMChain(llm=llm,prompt=prompt,verbose=True,memory=memory
)
conversation.invoke({"question": "给我讲个笑话"})
from langchain_community.chat_models import ChatZhipuAI
from langchain_core.messages import AIMessage, HumanMessage, SystemMessageimport os# 填写您自己的APIKey
os.environ["ZHIPUAI_API_KEY"] = "53cc8cc9db0c42b5800eed3ca93259b8.txLoEReMDXXxxxxx"chat = ChatZhipuAI(model="glm-4-air-250414",temperature=0.5,
)messages = [AIMessage(content="His."),SystemMessage(content="你的角色是一个诗人."),HumanMessage(content="用七言绝句的形式写一首关于AI的诗."),
]response = chat.invoke(messages)
print(response.content)