当前位置:   article > 正文

ChatGLM2+Langchain 构建本地知识库——环境部署_langchain环境搭建

langchain环境搭建

ChatGLM2-6B 是开源中英双语对话模型 ChatGLM-6B 的第二代版本,在保留了初代模型对话流畅、部署门槛较低等众多优秀特性的基础之上,ChatGLM2-6B 引入了如下新特性:

  1. 更强大的性能:基于 ChatGLM 初代模型的开发经验,我们全面升级了 ChatGLM2-6B 的基座模型。ChatGLM2-6B 使用了 GLM 的混合目标函数,经过了 1.4T 中英标识符的预训练与人类偏好对齐训练,评测结果显示,相比于初代模型,ChatGLM2-6B 在 MMLU(+23%)、CEval(+33%)、GSM8K(+571%) 、BBH(+60%)等数据集上的性能取得了大幅度的提升,在同尺寸开源模型中具有较强的竞争力。
  2. 更长的上下文:基于 FlashAttention 技术,我们将基座模型的上下文长度(Context Length)由 ChatGLM-6B 的 2K 扩展到了 32K,并在对话阶段使用 8K 的上下文长度训练。对于更长的上下文,我们发布了 ChatGLM2-6B-32K 模型。LongBench 的测评结果表明,在等量级的开源模型中,ChatGLM2-6B-32K 有着较为明显的竞争优势。
  3. 更高效的推理:基于 Multi-Query Attention 技术,ChatGLM2-6B 有更高效的推理速度和更低的显存占用:在官方的模型实现下,推理速度相比初代提升了 42%,INT4 量化下,6G 显存支持的对话长度由 1K 提升到了 8K。
  4. 更开放的协议:ChatGLM2-6B 权重对学术研究完全开放,在填写问卷进行登记后亦允许免费商业使用

参考视频:ChatGLM+Langchain构建本地知识库,只需6G显存,支持实时上传文档_哔哩哔哩_bilibili

一、实验环境

可以查看目前使用的系统版本信息。

  1. $cat /proc/version
  2. Linux version 5.15.133.1-microsoft-standard-WSL2 (root@1c602f52c2e4) (gcc (GCC) 11.2.0, GNU ld (GNU Binutils) 2.37) #1 SMP Thu Oct 5 21:02:42 UTC 2023

如果安装有显卡驱动,可以使用下面的代码来查看显卡信息。

  1. nvidia-smi #查看显卡信息
  2. Sat Mar 9 19:31:33 2024
  3. +-----------------------------------------------------------------------------------------+
  4. | NVIDIA-SMI 550.40.06 Driver Version: 551.23 CUDA Version: 12.4 |
  5. |-----------------------------------------+------------------------+----------------------+
  6. | GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
  7. | Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
  8. | | | MIG M. |
  9. |=========================================+========================+======================|
  10. | 0 NVIDIA GeForce RTX 3090 On | 00000000:AF:00.0 Off | N/A |
  11. | 32% 25C P8 6W / 350W | 134MiB / 24576MiB | 0% Default |
  12. | | | N/A |
  13. +-----------------------------------------+------------------------+----------------------+
  14. | 1 NVIDIA GeForce RTX 3090 On | 00000000:D8:00.0 Off | N/A |
  15. | 32% 24C P8 11W / 350W | 144MiB / 24576MiB | 0% Default |
  16. | | | N/A |
  17. +-----------------------------------------+------------------------+----------------------+
  18. +-----------------------------------------------------------------------------------------+
  19. | Processes: |
  20. | GPU GI CI PID Type Process name GPU Memory |
  21. | ID ID Usage |
  22. |=========================================================================================|
  23. | 0 N/A N/A 227 G /Xwayland N/A |
  24. | 1 N/A N/A 227 G /Xwayland N/A |
  25. +-----------------------------------------------------------------------------------------+

二、环境部署

1、下载项目

git clone https://github.com/THUDM/ChatGLM2-6B.git

国内访问GitHub可能较慢,可以使用镜像:

 git clone https://gitclone.com/github.com/THUDM/ChatGLM2-6B.git

2、下载环境

  1. conda create --name chatglm python=3.10
  2. conda activate chatglm
  3. cd ChatGLM2-6B/

下载依赖:

pip install -r requiremnets.txt
  1. Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
  2. Collecting protobuf (from -r requirements.txt (line 1))
  3. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/39/3f/16bdd9d43b024c1d178e817826e4e1ca8a25da3faff1e7566f341094143d/protobuf-5.26.0-cp37-abi3-manylinux2014_x86_64.whl (302 kB)
  4. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 302.8/302.8 kB 4.6 MB/s eta 0:00:00
  5. Collecting transformers==4.27.1 (from -r requirements.txt (line 2))
  6. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/6d/9b/2f536f9e73390209e0b27b74691355dac494b7ec8154f3012fdc6debbae7/transformers-4.27.1-py3-none-any.whl (6.7 MB)
  7. ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 6.7/6.7 MB 3.0 MB/s eta 0:00:00
  8. Collecting cpm_kernels (from -r requirements.txt (line 3))
  9. Using cached https://pypi.tuna.tsinghua.edu.cn/packages/af/84/1831ce6ffa87b8fd4d9673c3595d0fc4e6631c0691eb43f406d3bf89b951/cpm_kernels-1.0.11-py3-none-any.whl (416 kB)
  10. Collecting torch>=2.0 (from -r requirements.txt (line 4))
  11. Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a7/ad/fbe7d4cffb76da4e478438853b51305361c719cff929ab70a808e7fb75e7/torch-2.2.1-cp310-cp310-manylinux1_x86_64.whl (755.5 MB)
  12. ━━━━━━━━╸━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 162.2/755.5 MB 1.8 MB/s eta 0:05:23

3、下载模型

git clone https://gitee.com/hf-models/chatglm2-6b.git

4、代码调用

  1. from transformers import AutoTokenizer, AutoModel
  2. tokenizer = AutoTokenizer.from_pretrained("/home/david/20240207/ChatGLM2-6B/model/chatglm2-6b",trust_remote_code=True)
  3. model = AutoModel.from_pretrained("/home/david/20240207/ChatGLM2-6B/model/chatglm2-6b",trust_remote_code=True).quantize(4).half().cuda()
  4. model = model.eval()
  5. response, history = model.chat(tokenizer,"你好",history=[])
  6. print(response)
  7. response, history = model.chat(tokenizer,"晚上睡不着应该怎么办",history=history)
  8. print(response)
  1. $python main.py
  2. Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
  3. Explicitly passing a `revision` is encouraged when loading a configuration with custom code to ensure no malicious code has been contributed in a newer revision.
  4. Explicitly passing a `revision` is encouraged when loading a model with custom code to ensure no malicious code has been contributed in a newer revision.
  5. Loading checkpoint shards: 100%|███████████████████████████████████████████████████████████████████████████████████| 7/7 [00:10<00:00, 1.48s/it]
  6. 你好
    声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/817549
    推荐阅读
    相关标签