【AI基础】第三步:纯天然保姆喂饭级-安装并运行chatglm2-6b

chatglm2构建时使用了RUST,所以在安装chatglm2之前,先安装RUST。

此系列文章列表:

【AI基础】第一步:安装python开发环境-windows篇_下载安装ai环境python-CSDN博客

【AI基础】第一步:安装python开发环境-conda篇_minicode怎么换虚拟环境-CSDN博客

【AI基础】第二步:安装AI运行环境-CSDN博客

【AI基础】第三步:纯天然手动安装并运行chatglm2-6b-CSDN博客

【AI基础】第四步:保姆喂饭级-langchain+chatglm2-6b+m3e-base-CSDN博客

一、安装miniconda

参考 【AI基础】第一步:安装python开发环境-conda篇_minicode怎么换虚拟环境-CSDN博客

二、安装CUDA、cuDNN和pyTorch

参考 【AI基础】第二步:安装AI运行环境-CSDN博客

三、安装RUST

3.1 配置安装源

如果从官方安装,速度奇慢。

配置环境变量 RUSTUP_DIST_SERVER 到国内源:

这里指定了清华源,其余国内源还有:

字节跳动

RUSTUP_DIST_SERVER=https://rsproxy.cn

RUSTUP_UPDATE_ROOT=https://rsproxy.cn/rustup

中国科学技术大学

RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static

RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

清华大学

RUSTUP_DIST_SERVER=https://mirrors.tuna.tsinghua.edu.cn/rustup

上海交通大学

RUSTUP_DIST_SERVER=https://mirrors.sjtug.sjtu.edu.cn/rust-static/

3.2 安装依赖msvc

下载地址:https://aka.ms/vs/17/release/vs_BuildTools.exe

下载后双击安装,选择如下组件:

安装完成即可。

备注:msvc的安装可以参考rust官方文档:MSVC prerequisites - The rustup book

这里描述了去官方下载安装文件:下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux

3.3 下载安装

下载地址:Getting started - Rust Programming Language

选择对应版本下载:

这里选择64位版本,下载文件 rustup-init.exe。下载后双击执行:

安装完成。

3.4 安装检验

运行命令:

bash 复制代码
> cargo --version
> rustc --version

3.5 配置RUST安装包的国内源

新版配置文件在 ~/.cargo/config.toml,如果没有则新建,配置如下:

其余的国内源:

中国科学技术大学

source.ustc

registry = "https://mirrors.ustc.edu.cn/crates.io-index"

#registry = "git://mirrors.ustc.edu.cn/crates.io-index"

上海交通大学

source.sjtu

registry = "https://mirrors.sjtug.sjtu.edu.cn/git/crates.io-index/"

清华大学

source.tuna

registry = "https://mirrors.tuna.tsinghua.edu.cn/git/crates.io-index.git"

rustcc社区

source.rustcc

registry = "https://code.aliyun.com/rustcc/crates.io-index.git"

四、运行chatglm2

4.1 安装

bash 复制代码
> conda activate chatglm2
> git clone https://github.com/THUDM/ChatGLM2-6B
> cd chatglm2-6b
> pip install -r requirements.txt --verbose -i https://pypi.tuna.tsinghua.edu.cn/simple/

安装完成。

4.2 调用模型代码

创建文件 dev_hello.py,并使用官方代码:

python 复制代码
from transformers import AutoTokenizer, AutoModel
tokenizer = AutoTokenizer.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True)
model = AutoModel.from_pretrained("THUDM/chatglm2-6b", trust_remote_code=True, device='cuda')
model = model.eval()
response, history = model.chat(tokenizer, "你好", history=[])
print(response)
response, history = model.chat(tokenizer, "晚上睡不着应该怎么办", history=history)
print(response)

查看第二行和第二行的 THUDM/chatglm2-6b,这是大模型的地址,表示大模型存放在当前目录下的THUDM/chatglm2-6b中。如果没有,则会从HuggingFace下载并保存在此路径中。

因为大模型文件很大,我们可以先下载到本地,然后再运行代码。

4.3 下载大模型文件

4.3.1 HuggingFace下载

下载地址:https://huggingface.co/THUDM/chatglm2-6b

4.3.2 国内下载

综合镜像站:

也有国内厂商自己的单独站点:

下载到本地的默认下载地址,文件结构如图(图中文件,一个都不能少):

4.4 运行代码

4.4.1 执行官方演示代码

bash 复制代码
> python dev_hello.py

4.4.2 运行网页端

bash 复制代码
> streamlit run web_demo2.py

运行成功。

相关推荐
江畔柳前堤5 小时前
roLabelImg 详细安装教程
开发语言·人工智能·后端·云原生
阿里云大数据AI技术5 小时前
分链路差异化设计的DSP准实时数仓|钛动科技基于阿里云实时计算 Flink 版 + DLF Paimon + EMR Serverless StarRocks 的实践
人工智能·flink
陕西企来客5 小时前
2026年7月AI智能搜索曝光趋势研判
大数据·人工智能·机器学习·ai智能搜索曝光
阿里云大数据AI技术5 小时前
从算力到智能体,面向 Agentic AI 的基础设施演进
人工智能·agent
hangyuekejiGEO6 小时前
GEO技术服务选型指南
大数据·人工智能·python
阿里云大数据AI技术6 小时前
EMR Serverless Spark AI Function 的双维降本实践
人工智能·sql·spark
维基框架7 小时前
GitHub源码处理提速 一趟扫描反而更慢
人工智能·github
冬奇Lab7 小时前
代码库知识库系列(05):向量检索 vs 知识图谱——加了调用图并没有变更好
人工智能
AKAMAI7 小时前
你的源服务器可能是你做出的最昂贵决定
运维·人工智能·云计算
冬奇Lab7 小时前
【无标题】
人工智能·开源