# 开发：GLM等大模型外接数据库

> 本文整理GLM等大模型外接数据库的实践流程，涵盖ChatGLM-6B与MOSS硬件需求、Anaconda和PyCharm环境搭建、Git集成、langchain-ChatGLM项目部署、模型下载及参数配置，适合开发者参考。

- 来源: https://www.waytoagi.com/kb/ai-coding/glm-database-integration-tutorial
- 飞书原文: https://waytoagi.feishu.cn/wiki/GzFdwwzDMi2ii5kyq13c7Hdxnvh
- 更新于: 2026-12-02

---

本内容来自群友Michael的实践编辑整理

# 原理介绍

![](https://www.waytoagi.com/api/kb-media/SIbnbgh0JoZ2vWxwplKcDLthn0e)

从文档处理角度来看，实现流程如下：

![](https://www.waytoagi.com/api/kb-media/WWPibvdBeoLdWixLxxwc5Rx6nVg)

# 配置要求

- ChatGLM-6B 模型硬件需求

- 注：模型文件下载至本地需要 15 GB 存储空间。

| 量化等级 | 最低 GPU 显存（推理） | 最低 GPU 显存（高效参数微调） |
| --- | --- | --- |
| FP16（无量化） | 13 GB | 14 GB |
| INT8 | 8 GB | 9 GB |
| INT4 | 6 GB | 7 GB |

- MOSS 模型硬件需求

- 注：模型文件下载至本地需要 70 GB 存储空间

| 量化等级 | 最低 GPU 显存（推理） | 最低 GPU 显存（高效参数微调） |
| --- | --- | --- |
| FP16（无量化） | 68 GB | - |
| INT8 | 20 GB | - |

- Embedding 模型硬件需求

- 默认选用的 Embedding 模型 [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main) 约占用显存 3GB，也可修改为在 CPU 中运行。

# 环境搭建

采用Anconda配置Python环境，在Pycharm中编译

## Anconda

下载地址：[Anconda](https://www.anaconda.com/download)

打开Anaconda Navigator，点击Environments可以看到anaconda中已经有了默认的“base”环境，其中包含了大多数我们会用到的包，在PyCharm中可以直接使用该环境，而无需自行下载需要的包。

![](https://www.waytoagi.com/api/kb-media/Fv3zbgDBiovvQ0xjxticfDJ6nOc)

## Pycharm

### 官网安装

打开已经安装好的PyCharm，点击新建项目。

![](https://www.waytoagi.com/api/kb-media/V7yRbhGRvoXinWxYpPFcL88knSe)

### 集成Git

当我们在官网下载好Git后，按照要求进行安装，在PyCharm环境中配置Git。

![](https://www.waytoagi.com/api/kb-media/LQKXb2461oxxfrxoSQzcEnLJnyg)

基础配置

在设置中，选择版本控制(Version Control)，选择Git(一般来说，Git正常安装后就可以在这里显示插件)

![](https://www.waytoagi.com/api/kb-media/H5KibALL6olawMxK2Vvcup5CnAe)

找到Git的可执行文件，可以打开Git Bash，输入：`where git`

![](https://www.waytoagi.com/api/kb-media/ZyOobzZJ6oysYPxAGSCcFTwxnqc)

选择文件的位置即可。

![](https://www.waytoagi.com/api/kb-media/KGNXbW392ov0zAxowMAcZZm1nde)

也可以添加GitHub账号或者Gitee账号，Gitee需要安装对应的Plugin。

![](https://www.waytoagi.com/api/kb-media/I1JCbhqO4oxPO5xRNDbcRkFpnwd)

# 项目部署

项目地址：[https://github.com/imClumsyPanda/langchain-ChatGLM.git](https://github.com/imClumsyPanda/langchain-ChatGLM.git)

## 下载源码

```python
git clone https://github.com/imClumsyPanda/langchain-ChatGLM.git
```

或在Pycharm中使用git clone

![](https://www.waytoagi.com/api/kb-media/LLw9bfPIOoUw3pxqTixcQ31hnsh)

## 安装依赖

```python
cd langchain-ChatGLM
pip install -r requirements.txt
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
```

## 下载模型

因模型文件较大，需要下载git lfs

```python
git lfs install
#Hugging Face下载模型
#LLM
git clone https://huggingface.co/THUDM/chatglm-6b /<your_path>/chatglm-6b
#Embedding
git clone https://huggingface.co/GanymedeNil/text2vec-large-chinese /<your_path>/text2vec
```

或在Pycharm中使用git clone

# 参数调整

在 `configs/model_config.py` 文件中：

对`embedding_model_dict`参数进行修改

```python
embedding_model_dict = {
    "ernie-tiny": "nghuyong/ernie-3.0-nano-zh",
    "ernie-base": "nghuyong/ernie-3.0-base-zh",
    "text2vec-base": "shibing624/text2vec-base-chinese",
    "text2vec": "E:/langchain-ChatGLM/text2vec",

}
```

对`llm_model_dict`参数进行修改

```python
llm_model_dict = {
    "chatglm-6b-int4-qe": {
        "name": "chatglm-6b-int4-qe",
        "pretrained_model_name": "THUDM/chatglm-6b-int4-qe",
        "local_model_path": None,
        "provides": "ChatGLM"
    },
    "chatglm-6b-int4": {
        "name": "chatglm-6b-int4",
        "pretrained_model_name": "THUDM/chatglm-6b-int4",
        "local_model_path": None,
        "provides": "ChatGLM"
    },
    "chatglm-6b-int8": {
        "name": "chatglm-6b-int8",
        "pretrained_model_name": "THUDM/chatglm-6b-int8",
        "local_model_path": None,
        "provides": "ChatGLM"
    },
    "chatglm-6b": {
        "name": "chatglm-6b",
        "pretrained_model_name": "THUDM/chatglm-6b",
        "local_model_path": "E:/langchain-ChatGLM/chatglm-6b",
        "provides": "ChatGLM"
    },
    "chatglm2-6b": {
        "name": "chatglm2-6b",
        "pretrained_model_name": "THUDM/chatglm2-6b",
        "local_model_path": None,
        "provides": "ChatGLM"
    },

    "chatyuan": {
        "name": "chatyuan",
        "pretrained_model_name": "ClueAI/ChatYuan-large-v2",
        "local_model_path": None,
        "provides": None
    },
}
```

# 项目启动

## web启动

运行`web.py`

![](https://www.waytoagi.com/api/kb-media/RMvBbz69ToTWTExagzhcOb7xnTO)

若显存不足则调整`configs/model_config.py` 文件中`LLM_MODEL`参数更换模型

若连接无法连接修改`web.py`文件末尾`lauch`中`0.0.0.0`为`127.0.0.1`

```python
.queue(concurrency_count=3)
.launch(server_name='127.0.0.1',
        server_port=7860,
        show_api=False,
        share=False,
        inbrowser=False))
```

点击URL进入UI界面

![](https://www.waytoagi.com/api/kb-media/AvfCbc57gob1bWxxW6FcpAPjnHc)

## API 模式启动

```bash
python api.py
```

## 命令行模式启动

```bash
python cli_demo.py
```

# 上传知识库

左侧知识库问答中选择新建知识库，可传输txt  pdf等

![](https://www.waytoagi.com/api/kb-media/MwsYbwwd2o6h8Bx7bO3cx2TGnLg)

可以调整 prompt，匹配不同的知识库，让 LLM 扮演不同的角色

- 上传公司财报，充当[财务分析师](https://www.zhihu.com/search?q=%E8%B4%A2%E5%8A%A1%E5%88%86%E6%9E%90%E5%B8%88&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A3030429770%7D)

- 上传客服聊天记录，充当智能客服

- 上传经典Case，充当律师助手

- 上传医院[百科全书](https://www.zhihu.com/search?q=%E7%99%BE%E7%A7%91%E5%85%A8%E4%B9%A6&search_source=Entity&hybrid_search_source=Entity&hybrid_search_extra=%7B%22sourceType%22%3A%22answer%22%2C%22sourceId%22%3A3030429770%7D)，充当在线问诊医生

等等

MOSS同理
