diff --git a/README.md b/README.md index a946318..755eb56 100644 --- a/README.md +++ b/README.md @@ -16,20 +16,16 @@ 🚩 本项目未涉及微调、训练过程,但可利用微调或训练对本项目效果进行优化。 -[TOC] - ## 更新信息 -**[2023/04/07]** -1. 解决加载 ChatGLM 模型时发生显存占用为双倍的问题 (感谢 [@suc16](https://github.com/suc16) 和 [@myml](https://github.com/myml)) ; -2. 新增清理显存机制; -3. 新增`nghuyong/ernie-3.0-nano-zh`和`nghuyong/ernie-3.0-base-zh`作为 Embedding 模型备选项,相比`GanymedeNil/text2vec-large-chinese`占用显存资源更少 (感谢 [@lastrei](https://github.com/lastrei))。 +**[2023/04/15]** +1. 重构项目结构,在根目录下保留命令行 Demo [cli_demo.py](cli_demo.py) 和 Web UI Demo [webui.py](webui.py); +2. 对 Web UI 进行改进,修改为运行 Web UI 后首先按照默认选项加载模型,并增加报错提示信息等; +3. 对常见问题进行补充说明。 -**[2023/04/09]** -1. 使用`langchain`中的`RetrievalQA`替代之前选用的`ChatVectorDBChain`,替换后可以有效减少提问 2-3 次后因显存不足而停止运行的问题; -2. 在`knowledge_based_chatglm.py`中增加`EMBEDDING_MODEL`、`VECTOR_SEARCH_TOP_K`、`LLM_MODEL`、`LLM_HISTORY_LEN`、`REPLY_WITH_SOURCE`参数值设置; -3. 增加 GPU 显存需求更小的`chatglm-6b-int4`、`chatglm-6b-int4-qe`作为 LLM 模型备选项; -4. 更正`README.md`中的代码错误(感谢 [@calcitem](https://github.com/calcitem))。 +**[2023/04/12]** +1. 替换 Web UI 中的样例文件,避免出现 Ubuntu 中出现因文件编码无法读取的问题; +2. 替换`knowledge_based_chatglm.py`中的 prompt 模版,避免出现因 prompt 模版包含中英双语导致 chatglm 返回内容错乱的问题。 **[2023/04/11]** 1. 加入 Web UI V0.1 版本(感谢 [@liangtongt](https://github.com/liangtongt)); @@ -37,9 +33,16 @@ 3. 增加 LLM 和 Embedding 模型运行设备是否可用`cuda`、`mps`、`cpu`的自动判断。 4. 在`knowledge_based_chatglm.py`中增加对`filepath`的判断,在之前支持单个文件导入的基础上,现支持单个文件夹路径作为输入,输入后将会遍历文件夹中各个文件,并在命令行中显示每个文件是否成功加载。 -**[2023/04/12]** -1. 替换 Web UI 中的样例文件,避免出现 Ubuntu 中出现因文件编码无法读取的问题; -2. 替换`knowledge_based_chatglm.py`中的 prompt 模版,避免出现因 prompt 模版包含中英双语导致 chatglm 返回内容错乱的问题。 +**[2023/04/09]** +1. 使用`langchain`中的`RetrievalQA`替代之前选用的`ChatVectorDBChain`,替换后可以有效减少提问 2-3 次后因显存不足而停止运行的问题; +2. 在`knowledge_based_chatglm.py`中增加`EMBEDDING_MODEL`、`VECTOR_SEARCH_TOP_K`、`LLM_MODEL`、`LLM_HISTORY_LEN`、`REPLY_WITH_SOURCE`参数值设置; +3. 增加 GPU 显存需求更小的`chatglm-6b-int4`、`chatglm-6b-int4-qe`作为 LLM 模型备选项; +4. 更正`README.md`中的代码错误(感谢 [@calcitem](https://github.com/calcitem))。 + +**[2023/04/07]** +1. 解决加载 ChatGLM 模型时发生显存占用为双倍的问题 (感谢 [@suc16](https://github.com/suc16) 和 [@myml](https://github.com/myml)) ; +2. 新增清理显存机制; +3. 新增`nghuyong/ernie-3.0-nano-zh`和`nghuyong/ernie-3.0-base-zh`作为 Embedding 模型备选项,相比`GanymedeNil/text2vec-large-chinese`占用显存资源更少 (感谢 [@lastrei](https://github.com/lastrei))。 ## 使用方式 @@ -56,7 +59,7 @@ 本项目中默认选用的 Embedding 模型 [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main) 约占用显存 3GB,也可修改为在 CPU 中运行。 ### 软件需求 -本项目已在 python 3.8,cuda11.7 环境下完成测试。 +本项目已在 python 3.8,cuda 11.7 环境下完成测试。 @@ -64,7 +67,7 @@ - 环境检查 -``` +```shell # 首先,确信你的机器安装了 Python 3.8 及以上版本 $ python --version Python 3.8.13 @@ -84,32 +87,28 @@ $ conda env remove -p /your_path/env_name - 项目依赖 -```commandline - +```shell # 拉取仓库 $ git clone https://github.com/imClumsyPanda/langchain-ChatGLM.git # 安装依赖 $ pip install -r requirements.txt - ``` 注:使用 langchain.document_loaders.UnstructuredFileLoader 进行非结构化文件接入时,可能需要依据文档进行其他依赖包的安装,请参考 [langchain 文档](https://python.langchain.com/en/latest/modules/indexes/document_loaders/examples/unstructured_file.html) +### 2. 设置模型默认参数 +在开始执行 Web UI 或命令行交互前,请先检查 [configs/model_config.py](configs/model_config.py) 中的各项模型参数设计是否符合需求。 -### 2. 执行脚本体验 Web UI 或命令行交互 -执行 [webui.py](webui.py) 脚本体验 **Web 交互** -```commandline -python webui.py +### 3. 执行脚本体验 Web UI 或命令行交互 +执行 [webui.py](webui.py) 脚本体验 **Web 交互** +```shell +$ python webui.py ``` -注:执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,至少15G - - +注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,至少15G 执行后效果如下图所示: ![webui](img/ui1.png) -Web UI 中提供的 API 接口如下图所示: -![webui](img/ui2.png) Web UI 可以实现如下功能: 1. 自动读取`knowledge_based_chatglm.py`中`LLM`及`embedding`模型枚举,选择后点击`setting`进行模型加载,可随时切换模型进行测试 @@ -118,31 +117,23 @@ Web UI 可以实现如下功能: 4. 底部添加`use via API`可对接到自己系统 或执行 [knowledge_based_chatglm.py](cli_demo.py) 脚本体验**命令行交互** -```commandline -python knowledge_based_chatglm.py +```shell +$ python knowledge_based_chatglm.py ``` ### 常见问题 Q1: 本项目支持哪些文件格式? -A1: 目前已测试支持 txt、docx、md 格式文件,更多文件格式请参考 [langchain 文档](https://python.langchain.com/en/latest/modules/indexes/document_loaders/examples/unstructured_file.html)。目前已知文档中若含有特殊字符,可能存在文件无法加载的问题。 +A1: 目前已测试支持 txt、docx、md、pdf 格式文件,更多文件格式请参考 [langchain 文档](https://python.langchain.com/en/latest/modules/indexes/document_loaders/examples/unstructured_file.html)。目前已知文档中若含有特殊字符,可能存在文件无法加载的问题。 -Q2: 读取特定格式文件时遇到缺少`detectron2`时如何解决? - -A2: 因该包安装过程中遇到问题较多,且仅部分格式文件需要,所以未加入`requirements.txt`。可以通过一下命令安装 - -```commandline -pip install "detectron2@git+https://github.com/facebookresearch/detectron2.git@v0.6#egg=detectron2" -``` - -Q3: `Resource punkt not found.` 如何解决? +Q3: 使用过程中 Python 包`nltk`发生了`Resource punkt not found.`报错,该如何解决? A3: https://github.com/nltk/nltk_data/raw/gh-pages/packages/tokenizers/punkt.zip 中的 `packages/tokenizers` 解压,放到 `nltk_data/tokenizers` 存储路径下。 `nltk_data` 存储路径可以通过 `nltk.data.path` 查询。 -Q4: `Resource averaged_perceptron_tagger not found.` 如何解决? +Q4: 使用过程中 Python 包`nltk`发生了`Resource averaged_perceptron_tagger not found.`报错,该如何解决? A4: 将 https://github.com/nltk/nltk_data/blob/gh-pages/packages/taggers/averaged_perceptron_tagger.zip 下载,解压放到 `nltk_data/taggers` 存储路径下。 @@ -153,13 +144,31 @@ Q5: 本项目可否在 colab 中运行? A5: 可以尝试使用 chatglm-6b-int4 模型在 colab 中运行,需要注意的是,如需在 colab 中运行 Web UI,需将`webui.py`中`demo.queue(concurrency_count=3).launch( server_name='0.0.0.0', share=False, inbrowser=False)`中参数`share`设置为`True`。 -Q6: 在Anaconda中使用pip安装包无效问题 +Q6: 在 Anaconda 中使用 pip 安装包无效如何解决? -此问题是系统环境问题,详细见 [在Anaconda中使用pip安装包无效问题](docs/在Anaconda中使用pip安装包无效问题.md) +A6: 此问题是系统环境问题,详细见 [在Anaconda中使用pip安装包无效问题](docs/在Anaconda中使用pip安装包无效问题.md) +Q7: 本项目中所需模型如何下载至本地? -Q7: 本项目用到的模型权重文件百度网盘地址: +A7: 本项目中使用的模型均为`huggingface.com`中可下载的开源模型,以默认选择的`chatglm-6b`和`text2vec-large-chinese`模型为例,下载模型可执行如下代码: +```shell +# 安装 git lfs +$ git lfs install + +# 下载 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 + +# 模型需要更新时,可打开模型所在文件夹后拉取最新模型文件/代码 +$ git pull +``` + +Q8: `huggingface.com`中模型下载速度较慢怎么办? + +A8: 可使用本项目用到的模型权重文件百度网盘地址: - ernie-3.0-base-zh.zip 链接: https://pan.baidu.com/s/1CIvKnD3qzE-orFouA8qvNQ?pwd=4wih - ernie-3.0-nano-zh.zip 链接: https://pan.baidu.com/s/1Fh8fgzVdavf5P1omAJJ-Zw?pwd=q6s5 - text2vec-large-chinese.zip 链接: https://pan.baidu.com/s/1sMyPzBIXdEzHygftEoyBuA?pwd=4xs7 @@ -167,6 +176,26 @@ Q7: 本项目用到的模型权重文件百度网盘地址: - chatglm-6b-int4.zip 链接: https://pan.baidu.com/s/1pvZ6pMzovjhkA6uPcRLuJA?pwd=3gjd - chatglm-6b.zip 链接: https://pan.baidu.com/s/1B-MpsVVs1GHhteVBetaquw?pwd=djay +Q9: 下载完模型后,如何修改代码以执行本地模型? + +A9: 模型下载完成后,请在 [configs/model_config.py](configs/model_config.py) 文件中,对`embedding_model_dict`和`llm_model_dict`参数进行修改,如把`llm_model_dict`从 +``` +embedding_model_dict = { + "ernie-tiny": "nghuyong/ernie-3.0-nano-zh", + "ernie-base": "nghuyong/ernie-3.0-base-zh", + "text2vec": "GanymedeNil/text2vec-large-chinese" +} +``` + +修改为 + +``` +embedding_model_dict = { + "ernie-tiny": "nghuyong/ernie-3.0-nano-zh", + "ernie-base": "nghuyong/ernie-3.0-base-zh", + "text2vec": "/Users/liuqian/Downloads/ChatGLM-6B/text2vec-large-chinese" +} +``` ## DEMO @@ -205,15 +234,14 @@ Q7: 本项目用到的模型权重文件百度网盘地址: - [x] .pdf(需要按照常见问题 Q2 中描述进行`detectron2`的安装) - [x] .docx - [x] .txt + - [ ] 搜索引擎与本地网页 - [ ] 增加更多 LLM 模型支持 - [x] THUDM/chatglm-6b - [x] THUDM/chatglm-6b-int4 - [x] THUDM/chatglm-6b-int4-qe - [ ] 增加 Web UI DEMO - [x] 利用 gradio 实现 Web UI DEMO - - [ ] 添加模型加载进度条 - - [ ] 添加输出内容及错误提示 - - [ ] 国际化语言切换 + - [x] 添加输出内容及错误提示 - [ ] 引用标注 - [ ] 利用 fastapi 实现 API 部署方式,并实现调用 API 的 web ui DEMO diff --git a/img/ui1.png b/img/ui1.png index 74df8a6..76cf0c9 100644 Binary files a/img/ui1.png and b/img/ui1.png differ diff --git a/img/ui2.png b/img/ui2.png deleted file mode 100644 index 0f57265..0000000 Binary files a/img/ui2.png and /dev/null differ