diff --git a/knowledge_base/samples/content/README.md b/knowledge_base/samples/content/README.md index 9a7afb4..73bce4c 100644 --- a/knowledge_base/samples/content/README.md +++ b/knowledge_base/samples/content/README.md @@ -1,16 +1,33 @@ -# 基于本地知识的 ChatGLM 应用实现 +# 基于本地知识库的 ChatGLM 等大语言模型应用实现 + +## 目录 + +* [介绍](README.md#介绍) +* [变更日志](README.md#变更日志) +* [模型支持](README.md#模型支持) +* [Docker 整合包](README.md#Docker-整合包) +* [Docker 部署](README.md#Docker-部署) +* [开发部署](README.md#开发部署) + * [软件需求](README.md#软件需求) + * [1. 开发环境准备](README.md#1.-开发环境准备) + * [2. 下载模型至本地](README.md#2.-下载模型至本地) + * [3. 设置配置项](README.md#3.-设置配置项) + * [4. 启动 API 服务或 Web UI](README.md#4.-启动-API-服务或-Web-UI) +* [常见问题](README.md#常见问题) +* [路线图](README.md#路线图) +* [项目交流群](README.md#项目交流群) ## 介绍 -🌍 [_READ THIS IN ENGLISH_](README_en.md) +🤖️ 一种利用 [langchain](https://github.com/hwchase17/langchain) 思想实现的基于本地知识库的问答应用,目标期望建立一套对中文场景与开源模型支持友好、可离线运行的知识库问答解决方案。 -🤖️ 一种利用 [ChatGLM-6B](https://github.com/THUDM/ChatGLM-6B) + [langchain](https://github.com/hwchase17/langchain) 实现的基于本地知识的 ChatGLM 应用。增加 [clue-ai/ChatYuan](https://github.com/clue-ai/ChatYuan) 项目的模型 [ClueAI/ChatYuan-large-v2](https://huggingface.co/ClueAI/ChatYuan-large-v2) 的支持。 +💡 受 [GanymedeNil](https://github.com/GanymedeNil) 的项目 [document.ai](https://github.com/GanymedeNil/document.ai) 和 [AlexZhangji](https://github.com/AlexZhangji) 创建的 [ChatGLM-6B Pull Request](https://github.com/THUDM/ChatGLM-6B/pull/216) 启发,建立了全流程可使用开源模型实现的本地知识库问答应用。本项目的最新版本中通过使用 [FastChat](https://github.com/lm-sys/FastChat) 接入 Vicuna, Alpaca, LLaMA, Koala, RWKV 等模型,依托于 [langchain](https://github.com/langchain-ai/langchain) 框架支持通过基于 [FastAPI](https://github.com/tiangolo/fastapi) 提供的 API 调用服务,或使用基于 [Streamlit](https://github.com/streamlit/streamlit) 的 WebUI 进行操作。 -💡 受 [GanymedeNil](https://github.com/GanymedeNil) 的项目 [document.ai](https://github.com/GanymedeNil/document.ai) 和 [AlexZhangji](https://github.com/AlexZhangji) 创建的 [ChatGLM-6B Pull Request](https://github.com/THUDM/ChatGLM-6B/pull/216) 启发,建立了全部基于开源模型实现的本地知识问答应用。 +✅ 依托于本项目支持的开源 LLM 与 Embedding 模型,本项目可实现全部使用**开源**模型**离线私有部署**。与此同时,本项目也支持 OpenAI GPT API 的调用,并将在后续持续扩充对各类模型及模型 API 的接入。 -✅ 本项目中 Embedding 默认选用的是 [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main),LLM 默认选用的是 [ChatGLM-6B](https://github.com/THUDM/ChatGLM-6B)。依托上述模型,本项目可实现全部使用**开源**模型**离线私有部署**。 +⛓️ 本项目实现原理如下图所示,过程包括加载文件 -> 读取文本 -> 文本分割 -> 文本向量化 -> 问句向量化 -> 在文本向量中匹配出与问句向量最相似的 `top k`个 -> 匹配出的文本作为上下文和问题一起添加到 `prompt`中 -> 提交给 `LLM`生成回答。 -⛓️ 本项目实现原理如下图所示,过程包括加载文件 -> 读取文本 -> 文本分割 -> 文本向量化 -> 问句向量化 -> 在文本向量中匹配出与问句向量最相似的`top k`个 -> 匹配出的文本作为上下文和问题一起添加到`prompt`中 -> 提交给`LLM`生成回答。 +📺 [原理介绍视频](https://www.bilibili.com/video/BV13M4y1e7cN/?share_source=copy_web&vd_source=e6c5aafe684f30fbe41925d61ca6d514) ![实现原理图](img/langchain+chatglm.png) @@ -20,193 +37,234 @@ 🚩 本项目未涉及微调、训练过程,但可利用微调或训练对本项目效果进行优化。 -🌐 [AutoDL 镜像](https://www.codewithgpu.com/i/imClumsyPanda/langchain-ChatGLM/langchain-ChatGLM) +🐳 Docker镜像:registry.cn-beijing.aliyuncs.com/isafetech/chatmydata:1.0 (感谢 @InkSong🌲 ) -📓 [ModelWhale 在线运行项目](https://www.heywhale.com/mw/project/643977aa446c45f4592a1e59) +💻 运行方式:docker run -d -p 80:7860 --gpus all registry.cn-beijing.aliyuncs.com/isafetech/chatmydata:1.0 ## 变更日志 -参见 [变更日志](docs/CHANGELOG.md)。 +参见 [版本更新日志](https://github.com/imClumsyPanda/langchain-ChatGLM/releases)。 -## 硬件需求 +## 模型支持 -- ChatGLM-6B 模型硬件需求 +本项目中默认使用的 LLM 模型为 [THUDM/chatglm2-6b](https://huggingface.co/THUDM/chatglm2-6b),默认使用的 Embedding 模型为 [moka-ai/m3e-base](https://huggingface.co/moka-ai/m3e-base) 为例。 - 注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,模型文件下载至本地需要 15 GB 存储空间。 +### LLM 模型支持 - 模型下载方法可参考 [常见问题](docs/FAQ.md) 中 Q8。 - - | **量化等级** | **最低 GPU 显存**(推理) | **最低 GPU 显存**(高效参数微调) | - | -------------- | ------------------------- | --------------------------------- | - | FP16(无量化) | 13 GB | 14 GB | - | INT8 | 8 GB | 9 GB | - | INT4 | 6 GB | 7 GB | +本项目最新版本中基于 [FastChat](https://github.com/lm-sys/FastChat) 进行本地 LLM 模型接入,支持模型如下: -- MOSS 模型硬件需求 - - 注:如未将模型下载至本地,请执行前检查`$HOME/.cache/huggingface/`文件夹剩余空间,模型文件下载至本地需要 70 GB 存储空间 +- [meta-llama/Llama-2-7b-chat-hf](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) +- Vicuna, Alpaca, LLaMA, Koala +- [BlinkDL/RWKV-4-Raven](https://huggingface.co/BlinkDL/rwkv-4-raven) +- [camel-ai/CAMEL-13B-Combined-Data](https://huggingface.co/camel-ai/CAMEL-13B-Combined-Data) +- [databricks/dolly-v2-12b](https://huggingface.co/databricks/dolly-v2-12b) +- [FreedomIntelligence/phoenix-inst-chat-7b](https://huggingface.co/FreedomIntelligence/phoenix-inst-chat-7b) +- [h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b](https://huggingface.co/h2oai/h2ogpt-gm-oasst1-en-2048-open-llama-7b) +- [lcw99/polyglot-ko-12.8b-chang-instruct-chat](https://huggingface.co/lcw99/polyglot-ko-12.8b-chang-instruct-chat) +- [lmsys/fastchat-t5-3b-v1.0](https://huggingface.co/lmsys/fastchat-t5) +- [mosaicml/mpt-7b-chat](https://huggingface.co/mosaicml/mpt-7b-chat) +- [Neutralzz/BiLLa-7B-SFT](https://huggingface.co/Neutralzz/BiLLa-7B-SFT) +- [nomic-ai/gpt4all-13b-snoozy](https://huggingface.co/nomic-ai/gpt4all-13b-snoozy) +- [NousResearch/Nous-Hermes-13b](https://huggingface.co/NousResearch/Nous-Hermes-13b) +- [openaccess-ai-collective/manticore-13b-chat-pyg](https://huggingface.co/openaccess-ai-collective/manticore-13b-chat-pyg) +- [OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5](https://huggingface.co/OpenAssistant/oasst-sft-4-pythia-12b-epoch-3.5) +- [project-baize/baize-v2-7b](https://huggingface.co/project-baize/baize-v2-7b) +- [Salesforce/codet5p-6b](https://huggingface.co/Salesforce/codet5p-6b) +- [StabilityAI/stablelm-tuned-alpha-7b](https://huggingface.co/stabilityai/stablelm-tuned-alpha-7b) +- [THUDM/chatglm-6b](https://huggingface.co/THUDM/chatglm-6b) +- [THUDM/chatglm2-6b](https://huggingface.co/THUDM/chatglm2-6b) +- [tiiuae/falcon-40b](https://huggingface.co/tiiuae/falcon-40b) +- [timdettmers/guanaco-33b-merged](https://huggingface.co/timdettmers/guanaco-33b-merged) +- [togethercomputer/RedPajama-INCITE-7B-Chat](https://huggingface.co/togethercomputer/RedPajama-INCITE-7B-Chat) +- [WizardLM/WizardLM-13B-V1.0](https://huggingface.co/WizardLM/WizardLM-13B-V1.0) +- [WizardLM/WizardCoder-15B-V1.0](https://huggingface.co/WizardLM/WizardCoder-15B-V1.0) +- [baichuan-inc/baichuan-7B](https://huggingface.co/baichuan-inc/baichuan-7B) +- [internlm/internlm-chat-7b](https://huggingface.co/internlm/internlm-chat-7b) +- [Qwen/Qwen-7B-Chat](https://huggingface.co/Qwen/Qwen-7B-Chat) +- [HuggingFaceH4/starchat-beta](https://huggingface.co/HuggingFaceH4/starchat-beta) +- 任何 [EleutherAI](https://huggingface.co/EleutherAI) 的 pythia 模型,如 [pythia-6.9b](https://huggingface.co/EleutherAI/pythia-6.9b) +- 在以上模型基础上训练的任何 [Peft](https://github.com/huggingface/peft) 适配器。为了激活,模型路径中必须有 `peft` 。注意:如果加载多个peft模型,你可以通过在任何模型工作器中设置环境变量 `PEFT_SHARE_BASE_WEIGHTS=true` 来使它们共享基础模型的权重。 - 模型下载方法可参考 [常见问题](docs/FAQ.md) 中 Q8。 +以上模型支持列表可能随 [FastChat](https://github.com/lm-sys/FastChat) 更新而持续更新,可参考 [FastChat 已支持模型列表](https://github.com/lm-sys/FastChat/blob/main/docs/model_support.md)。 - | **量化等级** | **最低 GPU 显存**(推理) | **最低 GPU 显存**(高效参数微调) | - |-------------------|-----------------------| --------------------------------- | - | FP16(无量化) | 68 GB | - | - | INT8 | 20 GB | - | +### Embedding 模型支持 -- Embedding 模型硬件需求 +本项目支持调用 [HuggingFace](https://huggingface.co/models?pipeline_tag=sentence-similarity) 中的 Embedding 模型,已支持的 Embedding 模型如下: - 本项目中默认选用的 Embedding 模型 [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese/tree/main) 约占用显存 3GB,也可修改为在 CPU 中运行。 +- [moka-ai/m3e-small](https://huggingface.co/moka-ai/m3e-small) +- [moka-ai/m3e-base](https://huggingface.co/moka-ai/m3e-base) +- [moka-ai/m3e-large](https://huggingface.co/moka-ai/m3e-large) +- [BAAI/bge-small-zh](https://huggingface.co/BAAI/bge-small-zh) +- [BAAI/bge-base-zh](https://huggingface.co/BAAI/bge-base-zh) +- [BAAI/bge-large-zh](https://huggingface.co/BAAI/bge-large-zh) +- [text2vec-base-chinese-sentence](https://huggingface.co/shibing624/text2vec-base-chinese-sentence) +- [text2vec-base-chinese-paraphrase](https://huggingface.co/shibing624/text2vec-base-chinese-paraphrase) +- [text2vec-base-multilingual](https://huggingface.co/shibing624/text2vec-base-multilingual) +- [shibing624/text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) +- [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese) +- [nghuyong/ernie-3.0-nano-zh](https://huggingface.co/nghuyong/ernie-3.0-nano-zh) +- [nghuyong/ernie-3.0-base-zh](https://huggingface.co/nghuyong/ernie-3.0-base-zh) + +## Docker 整合包 + +🐳 Docker镜像地址:`registry.cn-beijing.aliyuncs.com/isafetech/chatmydata:1.0 `🌲 + +💻 一行命令运行: + +```shell +docker run -d -p 80:7860 --gpus all registry.cn-beijing.aliyuncs.com/isafetech/chatmydata:1.0 +``` + +- 该版本镜像大小 `25.2G`,使用[v0.1.16](https://github.com/imClumsyPanda/langchain-ChatGLM/releases/tag/v0.1.16),以 `nvidia/cuda:12.1.1-cudnn8-runtime-ubuntu22.04`为基础镜像 +- 该版本内置两个 `embedding`模型:`m3e-base`,`text2vec-large-chinese`,内置 `fastchat+chatglm-6b` +- 该版本目标为方便一键部署使用,请确保您已经在Linux发行版上安装了NVIDIA驱动程序 +- 请注意,您不需要在主机系统上安装CUDA工具包,但需要安装 `NVIDIA Driver`以及 `NVIDIA Container Toolkit`,请参考[安装指南](https://docs.nvidia.com/datacenter/cloud-native/container-toolkit/latest/install-guide.html) +- 首次拉取和启动均需要一定时间,首次启动时请参照下图使用 `docker logs -f `查看日志 +- 如遇到启动过程卡在 `Waiting..`步骤,建议使用 `docker exec -it bash`进入 `/logs/`目录查看对应阶段日志 + ![](img/docker_logs.png) ## Docker 部署 + 为了能让容器使用主机GPU资源,需要在主机上安装 [NVIDIA Container Toolkit](https://github.com/NVIDIA/nvidia-container-toolkit)。具体安装步骤如下: + ```shell sudo apt-get update sudo apt-get install -y nvidia-container-toolkit-base sudo systemctl daemon-reload sudo systemctl restart docker ``` + 安装完成后,可以使用以下命令编译镜像和启动容器: + ``` docker build -f Dockerfile-cuda -t chatglm-cuda:latest . docker run --gpus all -d --name chatglm -p 7860:7860 chatglm-cuda:latest -#若要使用离线模型,请配置好模型路径,然后此repo挂载到Container +#若要使用离线模型,请配置好模型路径,然后此 repo 挂载到 Container docker run --gpus all -d --name chatglm -p 7860:7860 -v ~/github/langchain-ChatGLM:/chatGLM chatglm-cuda:latest ``` - ## 开发部署 ### 软件需求 -本项目已在 Python 3.8 - 3.10,CUDA 11.7 环境下完成测试。已在 Windows、ARM 架构的 macOS、Linux 系统中完成测试。 +本项目已在 Python 3.8.1 - 3.10,CUDA 11.7 环境下完成测试。已在 Windows、ARM 架构的 macOS、Linux 系统中完成测试。 -vue前端需要node18环境 -### 从本地加载模型 +### 1. 开发环境准备 -请参考 [THUDM/ChatGLM-6B#从本地加载模型](https://github.com/THUDM/ChatGLM-6B#从本地加载模型) +参见 [开发环境准备](docs/INSTALL.md)。 -### 1. 安装环境 +### 2. 下载模型至本地 -参见 [安装指南](docs/INSTALL.md)。 +如需在本地或离线环境下运行本项目,需要首先将项目所需的模型下载至本地,通常开源 LLM 与 Embedding 模型可以从 [HuggingFace](https://huggingface.co/models) 下载。 -### 2. 设置模型默认参数 +以本项目中默认使用的 LLM 模型 [THUDM/chatglm2-6b](https://huggingface.co/THUDM/chatglm2-6b) 与 Embedding 模型 [moka-ai/m3e-base](https://huggingface.co/moka-ai/m3e-base) 为例: -在开始执行 Web UI 或命令行交互前,请先检查 [configs/model_config.py](configs/model_config.py) 中的各项模型参数设计是否符合需求。 +下载模型需要先[安装Git LFS](https://docs.github.com/zh/repositories/working-with-files/managing-large-files/installing-git-large-file-storage),然后运行 -### 3. 执行脚本体验 Web UI 或命令行交互 +```Shell +$ git clone https://huggingface.co/THUDM/chatglm2-6b -> 注:鉴于环境部署过程中可能遇到问题,建议首先测试命令行脚本。建议命令行脚本测试可正常运行后再运行 Web UI。 - -执行 [cli_demo.py](cli_demo.py) 脚本体验**命令行交互**: -```shell -$ python cli_demo.py +$ git clone https://huggingface.co/moka-ai/m3e-base ``` -或执行 [webui.py](webui.py) 脚本体验 **Web 交互** +### 3. 设置配置项 + +复制文件 [configs/model_config.py.example](configs/model_config.py.example) 存储至项目路径下 `./configs` 路径下,并重命名为 `model_config.py`。 + +在开始执行 Web UI 或命令行交互前,请先检查 `configs/model_config.py` 中的各项模型参数设计是否符合需求: + +- 请确认已下载至本地的 LLM 模型本地存储路径写在 `llm_model_dict` 对应模型的 `local_model_path` 属性中,如: + +```python +llm_model_dict={ + "chatglm2-6b": { + "local_model_path": "/Users/xxx/Downloads/chatglm2-6b", + "api_base_url": "http://localhost:8888/v1", # "name"修改为fastchat服务中的"api_base_url" + "api_key": "EMPTY" + }, + } +``` + +- 请确认已下载至本地的 Embedding 模型本地存储路径写在 `embedding_model_dict` 对应模型位置,如: + +```python +embedding_model_dict = { + "m3e-base": "/Users/xxx/Downloads/m3e-base", + } +``` + +### 4. 启动 API 服务或 Web UI + +#### 4.1 启动LLM服务 + +在项目根目录下,执行 [server/llm_api.py](server/llm_api.py) 脚本启动 **LLM 模型**服务: + +```shell +$ python server/llm_api.py +``` + +以如上方式启动LLM服务会以nohup命令在后台运行fastchat服务,如需停止服务,可以运行如下命令: + +```shell +$python server/llm_api_shutdown.py --serve all +``` + +亦可单独停止一个fastchat服务模块,可选[all, controller,model_worker,openai_api_server] + +#### 4.2 启动API服务 + +启动**LLM服务**后,执行 [server/api.py](server/api.py) 脚本启动 **API** 服务 + +```shell +$ python server/api.py +``` + +#### 4.3 启动Web UI服务 + +执行 [webui.py](webui.py) 启动 **Web UI** 服务 ```shell $ python webui.py ``` -或执行 [api.py](api.py) 利用 fastapi 部署 API -```shell -$ python api.py -``` -或成功部署 API 后,执行以下脚本体验基于 VUE 的前端页面 -```shell -$ cd views - -$ pnpm i - -$ npm run dev -``` - -执行后效果如下图所示: -1. `对话` Tab 界面 -![](img/webui_0510_0.png) -2. `知识库测试 Beta` Tab 界面 -![](img/webui_0510_1.png) -3. `模型配置` Tab 界面 -![](img/webui_0510_2.png) - -Web UI 可以实现如下功能: - -1. 运行前自动读取`configs/model_config.py`中`LLM`及`Embedding`模型枚举及默认模型设置运行模型,如需重新加载模型,可在 `模型配置` Tab 重新选择后点击 `重新加载模型` 进行模型加载; -2. 可手动调节保留对话历史长度、匹配知识库文段数量,可根据显存大小自行调节; -3. `对话` Tab 具备模式选择功能,可选择 `LLM对话` 与 `知识库问答` 模式进行对话,支持流式对话; -4. 添加 `配置知识库` 功能,支持选择已有知识库或新建知识库,并可向知识库中**新增**上传文件/文件夹,使用文件上传组件选择好文件后点击 `上传文件并加载知识库`,会将所选上传文档数据加载至知识库中,并基于更新后知识库进行问答; -5. 新增 `知识库测试 Beta` Tab,可用于测试不同文本切分方法与检索相关度阈值设置,暂不支持将测试参数作为 `对话` Tab 设置参数。 -6. 后续版本中将会增加对知识库的修改或删除,及知识库中已导入文件的查看。 - ### 常见问题 参见 [常见问题](docs/FAQ.md)。 -## Demo - -以问题`chatglm-6b 的局限性具体体现在哪里,如何实现改进`为例: - -未使用 langchain 接入本地文档时: - ->ChatGLM-6B 是一个基于 GPT-3 的聊天机器人,它的局限性主要体现在以下几个方面: ->1. 语言模型的精度:GPT-3 是一种大型的语言模型,它的精度比较高,但是仍然有一些局限性。例如,它可能无法准确地理解某些方言、口音或者复杂的句子结构。 ->2. 知识图谱的完整性:GPT-3 可以生成一些知识图谱,但是其完整性和准确性可能不够。例如,它可能无法准确地理解某些领域的专业知识和术语。 ->3. 对话生成的连贯性:GPT-3 可以生成一些连贯的对话,但是其生成的对话可能不够自然。例如,它可能会生成一些虚假的、不连贯的对话或者误解用户的意图。 -> ->为了改进 ChatGLM-6B,可以考虑以下几个方面: ->1. 提高语言模型的精度:可以通过增加训练数据、使用更好的语言模型架构、优化模型训练过程等方式来提高语言模型的精度。 ->2. 增强知识图谱的完整性:可以通过增加知识图谱的维度、使用更好的知识图谱构建方法、优化知识图谱的匹配算法等方式来增强知识图谱的完整性。 ->3. 提高对话生成的连贯性:可以通过增加对话生成模型的上下文信息、使用更好的上下文信息提取方法、优化对话生成模型的算法等方式来提高对话生成的连贯性。 ->4. 引入更多的人类反馈:人类反馈可以帮助 ChatGLM-6B 更好地理解用户的意图和需求,从而提高对话生成的质量和准确性。可以通过引入更多的人类客服、使用人类反馈收集器等方式来引入更多的人类反馈。 - -使用 langchain 接入 ChatGLM-6B 项目的 README.md 文件后 ChatGLM 的回答: ->ChatGLM-6B 是一个基于深度学习的自然语言处理模型,它在回答问题方面表现出色。但是,它也有一些局限性,具体如下: ->1. 语言理解有限:ChatGLM-6B 只学习了中文自然语言,对于其他语言可能会存在理解上的局限性。 ->2. 知识储备不足:ChatGLM-6B 的训练数据集只包含了中文语料,因此它可能无法回答一些非中文的问题或者某些特定领域的问题。 ->3. 数据量有限:ChatGLM-6B 的训练数据集只有几百万条记录,因此它可能无法回答一些非常具体或者复杂的问题。 -> ->为了改进 ChatGLM-6B,可以考虑以下几个方面: ->1. 学习更多的语言知识:可以学习其他语言的自然语言处理技术,扩大语言理解的范围。 ->2. 扩大知识储备:可以收集更多的中文语料,或者使用其他语言的数据集来扩充知识储备。 ->3. 增加数据量:可以使用更大的数据集来训练 ChatGLM-6B,提高模型的表现。 ->4. 引入更多的评估指标:可以引入更多的评估指标来评估模型的表现,从而发现 ChatGLM-6B 存在的不足和局限性。 ->5. 改进模型架构:可以改进 ChatGLM-6B 的模型架构,提高模型的性能和表现。例如,可以使用更大的神经网络或者改进的卷积神经网络结构。 - ## 路线图 -- [ ] Langchain 应用 - - [x] 接入非结构化文档(已支持 md、pdf、docx、txt 文件格式) - - [ ] 搜索引擎与本地网页接入 - - [ ] 结构化数据接入(如 csv、Excel、SQL 等) - - [ ] 知识图谱/图数据库接入 +- [X] Langchain 应用 + - [X] 本地数据接入 + - [X] 接入非结构化文档 + - [X] .md + - [X] .txt + - [X] .docx + - [ ] 结构化数据接入 + - [X] .csv + - [ ] .xlsx + - [ ] 本地网页接入 + - [ ] SQL 接入 + - [ ] 知识图谱/图数据库接入 + - [X] 搜索引擎接入 + - [X] Bing 搜索 + - [X] DuckDuckGo 搜索 - [ ] Agent 实现 -- [ ] 增加更多 LLM 模型支持 - - [x] [THUDM/chatglm-6b](https://huggingface.co/THUDM/chatglm-6b) - - [x] [THUDM/chatglm-6b-int8](https://huggingface.co/THUDM/chatglm-6b-int8) - - [x] [THUDM/chatglm-6b-int4](https://huggingface.co/THUDM/chatglm-6b-int4) - - [x] [THUDM/chatglm-6b-int4-qe](https://huggingface.co/THUDM/chatglm-6b-int4-qe) - - [x] [ClueAI/ChatYuan-large-v2](https://huggingface.co/ClueAI/ChatYuan-large-v2) - - [x] [fnlp/moss-moon-003-sft](https://huggingface.co/fnlp/moss-moon-003-sft) -- [ ] 增加更多 Embedding 模型支持 - - [x] [nghuyong/ernie-3.0-nano-zh](https://huggingface.co/nghuyong/ernie-3.0-nano-zh) - - [x] [nghuyong/ernie-3.0-base-zh](https://huggingface.co/nghuyong/ernie-3.0-base-zh) - - [x] [shibing624/text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) - - [x] [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese) -- [ ] Web UI - - [x] 利用 gradio 实现 Web UI DEMO - - [x] 添加输出内容及错误提示 - - [x] 引用标注 - - [ ] 增加知识库管理 - - [x] 选择知识库开始问答 - - [x] 上传文件/文件夹至知识库 - - [ ] 删除知识库中文件 - - [ ] 利用 streamlit 实现 Web UI Demo -- [ ] 增加 API 支持 - - [x] 利用 fastapi 实现 API 部署方式 - - [ ] 实现调用 API 的 Web UI Demo +- [X] LLM 模型接入 + - [X] 支持通过调用 [fastchat](https://github.com/lm-sys/FastChat) api 调用 llm +- [X] 增加更多 Embedding 模型支持 + - [X] [nghuyong/ernie-3.0-nano-zh](https://huggingface.co/nghuyong/ernie-3.0-nano-zh) + - [X] [nghuyong/ernie-3.0-base-zh](https://huggingface.co/nghuyong/ernie-3.0-base-zh) + - [X] [shibing624/text2vec-base-chinese](https://huggingface.co/shibing624/text2vec-base-chinese) + - [X] [GanymedeNil/text2vec-large-chinese](https://huggingface.co/GanymedeNil/text2vec-large-chinese) + - [X] [moka-ai/m3e-small](https://huggingface.co/moka-ai/m3e-small) + - [X] [moka-ai/m3e-base](https://huggingface.co/moka-ai/m3e-base) +- [X] 基于 FastAPI 的 API 方式调用 +- [X] Web UI + - [X] 基于 Streamlit 的 Web UI ## 项目交流群 -![二维码](img/qr_code_17.jpg) -🎉 langchain-ChatGLM 项目交流群,如果你也对本项目感兴趣,欢迎加入群聊参与讨论交流。 +二维码 + +🎉 langchain-ChatGLM 项目微信交流群,如果你也对本项目感兴趣,欢迎加入群聊参与讨论交流。 diff --git a/server/knowledge_base/utils.py b/server/knowledge_base/utils.py index cc10646..a6ff4b8 100644 --- a/server/knowledge_base/utils.py +++ b/server/knowledge_base/utils.py @@ -8,7 +8,7 @@ from configs.model_config import ( ZH_TITLE_ENHANCE ) from functools import lru_cache -import sys +import importlib from text_splitter import zh_title_enhance @@ -83,32 +83,36 @@ class KnowledgeFile: def file2text(self, using_zh_title_enhance=ZH_TITLE_ENHANCE): print(self.document_loader_name) try: - DocumentLoader = getattr(sys.modules['langchain.document_loaders'], self.document_loader_name) + document_loaders_module = importlib.import_module('langchain.document_loaders') + DocumentLoader = getattr(document_loaders_module, self.document_loader_name) except Exception as e: print(e) - DocumentLoader = getattr(sys.modules['langchain.document_loaders'], "UnstructuredFileLoader") + document_loaders_module = importlib.import_module('langchain.document_loaders') + DocumentLoader = getattr(document_loaders_module, "UnstructuredFileLoader") if self.document_loader_name == "UnstructuredFileLoader": loader = DocumentLoader(self.filepath, autodetect_encoding=True) else: loader = DocumentLoader(self.filepath) - # TODO: 增加依据文件格式匹配text_splitter try: if self.text_splitter_name is None: - TextSplitter = getattr(sys.modules['langchain.text_splitter'], "SpacyTextSplitter") + text_splitter_module = importlib.import_module('langchain.text_splitter') + TextSplitter = getattr(text_splitter_module, "SpacyTextSplitter") text_splitter = TextSplitter( pipeline="zh_core_web_sm", chunk_size=CHUNK_SIZE, chunk_overlap=OVERLAP_SIZE, ) else: - TextSplitter = getattr(sys.modules['langchain.text_splitter'], self.text_splitter_name) + text_splitter_module = importlib.import_module('langchain.text_splitter') + TextSplitter = getattr(text_splitter_module, self.text_splitter_name) text_splitter = TextSplitter( chunk_size=CHUNK_SIZE, - chunk_overlap=50) + chunk_overlap=OVERLAP_SIZE) except Exception as e: print(e) - TextSplitter = getattr(sys.modules['langchain.text_splitter'], "RecursiveCharacterTextSplitter") + text_splitter_module = importlib.import_module('langchain.text_splitter') + TextSplitter = getattr(text_splitter_module, "RecursiveCharacterTextSplitter") text_splitter = TextSplitter( chunk_size=CHUNK_SIZE, chunk_overlap=OVERLAP_SIZE, diff --git a/webui.py b/webui.py index 4b0c255..16d1191 100644 --- a/webui.py +++ b/webui.py @@ -41,5 +41,5 @@ if __name__ == "__main__": icons=[i["icon"] for i in pages.values()], menu_icon="chat-quote", default_index=0) - + print(f"root: {api.no_remote_api=}") pages[selected_page]["func"](api)