智譜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)