国产开源大模型 ChatGLM3-6B 部署与使用指南
国产开源大模型 ChatGLM3-6B 的部署流程与使用方法。内容涵盖本地及云端(AutoDL)环境的搭建步骤,包括 Python 环境配置、依赖安装、模型文件下载及测试验证。同时讲解了如何通过 WebUI 界面进行对话、插件调用及代码解释器操作,并提供了基于 OpenAI 规范的 API 调用示例,包含简单的聊天程序与天气插件集成代码。最后补充了量化部署、并发控制及监控等性能优化建议,帮助开发者快速上手私有化大模型应用开发。

国产开源大模型 ChatGLM3-6B 的部署流程与使用方法。内容涵盖本地及云端(AutoDL)环境的搭建步骤,包括 Python 环境配置、依赖安装、模型文件下载及测试验证。同时讲解了如何通过 WebUI 界面进行对话、插件调用及代码解释器操作,并提供了基于 OpenAI 规范的 API 调用示例,包含简单的聊天程序与天气插件集成代码。最后补充了量化部署、并发控制及监控等性能优化建议,帮助开发者快速上手私有化大模型应用开发。

部署一个自己的大模型,进行本地或云端的测试与应用开发,是许多技术同学的目标。常见的顾虑包括硬件成本过高或官方部署文档过于简略导致环境配置困难。本文分享 ChatGLM3-6B 的本地及云服务器部署经验,涵盖 API 调用与 WebUI 使用。
本次部署使用的模型为 ChatGLM3-6B,由清华智谱研发并开源。该模型基于 GLM(Gated Linear Units with Memory)架构,拥有 60 亿参数量,在对话理解与生成方面表现优异。它支持中英双语对话、函数调用以及代码解释执行,允许开发者通过 API 拓展应用场景。此外,模型支持微调与量化,可在消费级显卡甚至 CPU 上运行。
模型具备工具调用能力,能处理上下文信息。

模型具备绘图能力,可生成图像。

部分场景下模型表现仍有优化空间,提示词工程对效果影响较大。

多模态识别能力尚在发展中。

以 AutoDL 为例,ChatGLM3-6B 需要 13G 以上显存,推荐 RTX4090、RTX3090 等规格。
创建容器实例时选择'社区镜像',输入 yinghuoai,选择 ChatGLM3 最新镜像。开机后点击 JupyterLab 即可使用。镜像包含启动 WebUI 和 API 服务器的 Notebook。
选择基础镜像 Miniconda -> conda3 -> Python 3.10 (ubuntu22.04) -> Cuda11.8。
source /etc/network_turbo
git clone https://github.com/THUDM/ChatGLM3
cd ChatGLM3
conda create -n chatglm3-6b python=3.10.8
source activate chatglm3-6b
uv 管理版本以避免兼容性问题。pip install uv
uv pip install --resolution=lowest-direct -r requirements.txt
/root/autodl-tmp。pip install codewithgpu
cg down xxxiu/chatglm3-6b/config.json -t /root/autodl-tmp
cg down xxxiu/chatglm3-6b/configuration_chatglm.py -t /root/autodl-tmp
cg down xxxiu/chatglm3-6b/modeling_chatglm.py -t /root/autodl-tmp
cg down xxxiu/chatglm3-6b/tokenizer.model -t /root/autodl-tmp
# ... 其他分片文件 ...
basic_demo/cli_demo.py 中的模型路径,执行 python basic_demo/cli_demo.py。需 13G 显存以上的 Nvidia 显卡,以 Windows 系统为例。
cd C:\
git clone https://github.com/THUDM/ChatGLM3
cd ChatGLM3
conda create -n chatglm3-6b python=3.10.8
conda activate chatglm3-6b
C:\ChatGLM3\THUDM。python basic_demo/cli_demo.py。若遇到 RuntimeError: 'addmm_impl_cpu_' not implemented for 'Half' 错误,需补充安装 CUDA 包:
conda install pytorch==2.1.0 torchvision==0.16.0 torchaudio==2.1.0 pytorch-cuda=11.8 -c pytorch -c nvidia
pip install chardet
ChatGLM 提供 Streamlit 构建的 Web 界面,支持聊天、插件调用及代码解释器。
composite_demo/client.py 设置 MODEL_PATH。ipython kernel install --name chatglm3-6b --user
composite_demo/demo_ci.py 中的 IPYKERNEL 值。streamlit run composite_demo/main.py
若在云端部署,需通过 SSH 隧道映射端口:
sudo ssh -CNg -L 8501:127.0.0.1:8501 root@connect.westb.seetacloud.com -p 12357
composite_demo/tool_registry.py 中注册工具。@register_tool
def get_weather(
city_name: Annotated[str, 'The name of the city to be queried', True],
) -> str:
"""
Get the weather for `city_name` in the following week
"""
...
API 服务遵循 OpenAI 接口规范,便于集成。
修改 openai_api_demo/api_server.py 中的 MODEL_PATH。若部署在云端,需将端口改为 6006 并通过自定义服务暴露外网。
安装 SDK:
pip install --upgrade openai httpx[socks]
简单聊天示例:
from openai import OpenAI
client = OpenAI(api_key='not-need-key',base_url="http://127.0.0.1:6006/v1")
stream = client.chat.completions.create(
messages=[{
"role": "system", "content": "你是一名数学老师,从事小学数学教育 30 年,精通设计各种数学考试题"
},{
"role": "user", "content": "请给我出 10 道一年级的计算题。"
}],
model='chatglm3-6b',
max_tokens=1024,
top_p=0.3,
response_format={ "type": "json_object" },
stream=True
)
for chunk in stream:
msg = chunk.choices[0].delta.content
if msg is not None:
print(msg, end='')
插件调用示例:
from openai import OpenAI
import json
import requests
def get_city_weather(param):
city = json.loads(param)["city"]
r = requests.get(f"https://wttr.in/{city}?format=j1")
data = r.json()["current_condition"]
temperature = data[0]['temp_C']
humidity= data[0]['humidity']
text = data[0]['weatherDesc'][0]["value"]
return "当前天气:"+text+",温度:"+temperature+ "℃,湿度:"+humidity+"%"
weather_tool = {
"type": "function",
"function": {
"name": "get_city_weather",
"description": "获取某个城市的天气",
"parameters": {
"type": "object",
"properties": {
"city": {
"type": "string",
"description": "城市名称",
},
},
"required": ["city"],
},
}
}
client = OpenAI(api_key='no-need-key', base_url="http://127.0.0.1:6006/v1")
messages=[]
questions = ["请问上海天气怎么样?","请问广州天气怎么样?"]
for question questions:
messages.append({: , : question})
response_message = client.chat.completions.create(
messages=messages,
model=,
stream=,
tool_choice=,
tools=[weather_tool]
).choices[].message
messages.append(response_message)
response_message.function_call :
function_call = response_message.function_call
weather_info = get_city_weather(function_call.arguments)
messages.append({
: ,
: weather_info,
: function_call.name
})
second_completion = client.chat.completions.create(messages=messages, model=)
(second_completion.choices[].message.content)
为了提升部署效率与资源利用率,建议关注以下优化点:
max_batch_size 和 max_total_tokens 参数,避免 OOM(内存溢出)错误。通过以上步骤,即可完成 ChatGLM3-6B 的完整部署与基础应用开发。

微信公众号「极客日志」,在微信中扫描左侧二维码关注。展示文案:极客日志 zeeklog
使用加密算法(如AES、TripleDES、Rabbit或RC4)加密和解密文本明文。 在线工具,加密/解密文本在线工具,online
生成新的随机RSA私钥和公钥pem证书。 在线工具,RSA密钥对生成器在线工具,online
基于 Mermaid.js 实时预览流程图、时序图等图表,支持源码编辑与即时渲染。 在线工具,Mermaid 预览与可视化编辑在线工具,online
解析常见 curl 参数并生成 fetch、axios、PHP curl 或 Python requests 示例代码。 在线工具,curl 转代码在线工具,online
将字符串编码和解码为其 Base64 格式表示形式即可。 在线工具,Base64 字符串编码/解码在线工具,online
将字符串、文件或图像转换为其 Base64 表示形式。 在线工具,Base64 文件转换器在线工具,online