当前位置: 首页 > news >正文

智谱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)
http://www.dtcms.com/a/300898.html

相关文章:

  • 2025第15届上海生物发酵展将于8月7号启幕
  • HBuilder X打包发布微信小程序
  • PDF转图片实用指南:如何批量高效转换?
  • cuda编程笔记(10)--memory access 优化
  • 《P4568 [JLOI2011] 飞行路线》
  • Flutter开发实战之性能优化与调试
  • 自动标注软件X-AnyLabeling的使用教程
  • OpenLayers 综合案例-地图绘制
  • 深入理解Linux网络--读书笔记(二)
  • HDFS基础命令
  • 简易 BMI 身体质量指数计算器
  • 墨者:SQL注入漏洞测试(布尔盲注)
  • FastAPI入门:查询参数模型、多个请求体参数
  • (LeetCode 面试经典 150 题)71. 简化路径 (字符串)
  • 小白投资理财 - 从换手率和成交量分析股票趋势
  • Vue vuex模块化编码
  • 网络资源模板--基于Android Studio 实现的新闻App
  • 自由学习记录(74)
  • 基于混沌系统的图像加密学习日志——论文学习3
  • unity3dTextMeshPro 设置中文字体,解决中文显示为框或中文字后面带背景颜色的问题
  • Unity SMAA
  • 三、搭建springCloudAlibaba2021.1版本分布式微服务-springcloud loadbalancer负载均衡
  • 习题综合练习
  • 自然语言处理NLP (1)
  • 【笔记】系统
  • 上位机知识篇---AJAX
  • MongoDB分片集群横向扩展
  • 2.qt调试日志输出
  • 区块链共识机制与联邦学习
  • 【C++】数字cmath库常用函数