Git Product home page Git Product logo

lph0729 / langchain-chatchat Goto Github PK

View Code? Open in Web Editor NEW

This project forked from chatchat-space/langchain-chatchat

0.0 0.0 0.0 137.57 MB

Langchain-Chatchat(原Langchain-ChatGLM, Qwen 与 Llama 等)基于 Langchain 与 ChatGLM 等语言模型的 RAG 与 Agent 应用 | Langchain-Chatchat (formerly langchain-ChatGLM), local knowledge based LLM (like ChatGLM, Qwen and Llama) RAG and Agent app with langchain

License: Apache License 2.0

Shell 0.05% JavaScript 0.26% Python 23.95% TypeScript 66.90% Makefile 0.12% Dockerfile 0.11% MDX 8.62%

langchain-chatchat's Introduction

chatchat-space%2FLangchain-Chatchat | Trendshift

🌍 READ THIS IN ENGLISH

📃 LangChain-Chatchat (原 Langchain-ChatGLM)

基于 ChatGLM 等大语言模型与 Langchain 等应用框架实现,开源、可离线部署的 RAG 与 Agent 应用项目。


目录

概述

🤖️ 一种利用 langchain **实现的基于本地知识库的问答应用,目标期望建立一套对中文场景与开源模型支持友好、可离线运行的知识库问答解决方案。

💡 受 GanymedeNil 的项目 document.aiAlexZhangji 创建的 ChatGLM-6B Pull Request 启发,建立了全流程可使用开源模型实现的本地知识库问答应用。本项目的最新版本中可使用 XinferenceOllama 等框架接入 GLM-4-ChatQwen2-InstructLlama3 等模型,依托于 langchain 框架支持通过基于 FastAPI 提供的 API 调用服务,或使用基于 Streamlit 的 WebUI 进行操作。

✅ 本项目支持市面上主流的开源 LLM、 Embedding 模型与向量数据库,可实现全部使用开源模型离线私有部署。与此同时,本项目也支持 OpenAI GPT API 的调用,并将在后续持续扩充对各类模型及模型 API 的接入。

⛓️ 本项目实现原理如下图所示,过程包括加载文件 -> 读取文本 -> 文本分割 -> 文本向量化 -> 问句向量化 -> 在文本向量中匹配出与问句向量最相似的 top k个 -> 匹配出的文本作为上下文和问题一起添加到 prompt中 -> 提交给 LLM生成回答。

📺 原理介绍视频

实现原理图

从文档处理角度来看,实现流程如下:

实现原理图2

🚩 本项目未涉及微调、训练过程,但可利用微调或训练对本项目效果进行优化。

🌐 AutoDL 镜像0.3.0 版本所使用代码已更新至本项目 v0.3.0 版本。

🐳 Docker 镜像将会在近期更新。

🧑‍💻 如果你想对本项目做出贡献,欢迎移步开发指南 获取更多开发部署相关信息。

Langchain-Chatchat 提供哪些功能

0.3.x 版本功能一览

功能 0.2.x 0.3.x
模型接入 本地:fastchat
在线:XXXModelWorker
本地:model_provider,支持大部分主流模型加载框架
在线:oneapi
所有模型接入均兼容openai sdk
Agent ❌不稳定 ✅针对ChatGLM3和QWen进行优化,Agent能力显著提升
LLM对话
知识库对话
搜索引擎对话
文件对话 ✅仅向量检索 ✅统一为File RAG功能,支持BM25+KNN等多种检索方式
数据库对话
ARXIV文献对话
Wolfram对话
文生图
本地知识库管理
WEBUI ✅更好的多会话支持,自定义系统提示词...

0.3.x 版本的核心功能由 Agent 实现,但用户也可以手动实现工具调用:

操作方式 实现的功能 适用场景
选中"启用Agent",选择多个工具 由LLM自动进行工具调用 使用ChatGLM3/Qwen或在线API等具备Agent能力的模型
选中"启用Agent",选择单个工具 LLM仅解析工具参数 使用的模型Agent能力一般,不能很好的选择工具
想手动选择功能
不选中"启用Agent",选择单个工具 不使用Agent功能的情况下,手动填入参数进行工具调用 使用的模型不具备Agent能力

更多功能和更新请实际部署体验.

已支持的模型部署框架与模型

本项目中已经支持市面上主流的如 GLM-4-ChatQwen2-Instruct 等新近开源大语言模型和 Embedding 模型,这些模型需要用户自行启动模型部署框架后,通过修改配置信息接入项目,本项目已支持的本地模型部署框架如下:

模型部署框架 Xinference LocalAI Ollama FastChat
OpenAI API 接口对齐
加速推理引擎 GPTQ, GGML, vLLM, TensorRT GPTQ, GGML, vLLM, TensorRT GGUF, GGML vLLM
接入模型类型 LLM, Embedding, Rerank, Text-to-Image, Vision, Audio LLM, Embedding, Rerank, Text-to-Image, Vision, Audio LLM, Text-to-Image, Vision LLM, Vision
Function Call /
更多平台支持(CPU, Metal)
异构 / /
集群 / /
操作文档链接 Xinference 文档 LocalAI 文档 Ollama 文档 FastChat 文档
可用模型 Xinference 已支持模型 LocalAI 已支持模型 Ollama 已支持模型 FastChat 已支持模型

除上述本地模型加载框架外,项目中也为可接入在线 API 的 One API 框架接入提供了支持,支持包括 OpenAI ChatGPTAzure OpenAI APIAnthropic Claude智谱请言百川 等常用在线 API 的接入使用。

Note

关于 Xinference 加载本地模型: Xinference 内置模型会自动下载,如果想让它加载本机下载好的模型,可以在启动 Xinference 服务后,到项目 tools/model_loaders 目录下执行 streamlit run xinference_manager.py,按照页面提示为指定模型设置本地路径即可.

快速上手

安装部署

0. 软硬件要求

💡 软件方面,本项目已支持在 Python 3.8-3.11 环境中进行使用,并已在 Windows、macOS、Linux 操作系统中进行测试。

💻 硬件方面,因 0.3.0 版本已修改为支持不同模型部署框架接入,因此可在 CPU、GPU、NPU、MPS 等不同硬件条件下使用。

1. 安装 Langchain-Chatchat

从 0.3.0 版本起,Langchain-Chatchat 提供以 Python 库形式的安装方式,具体安装请执行:

pip install langchain-chatchat -U

Important

为确保所使用的 Python 库为最新版,建议使用官方 Pypi 源或清华源。

Note

因模型部署框架 Xinference 接入 Langchain-Chatchat 时需要额外安装对应的 Python 依赖库,因此如需搭配 Xinference 框架使用时,建议使用如下安装方式:

pip install "langchain-chatchat[xinference]" -U

2. 模型推理框架并加载模型

从 0.3.0 版本起,Langchain-Chatchat 不再根据用户输入的本地模型路径直接进行模型加载,涉及到的模型种类包括 LLM、Embedding、Reranker 及后续会提供支持的多模态模型等,均改为支持市面常见的各大模型推理框架接入,如 XinferenceOllamaLocalAIFastChatOne API 等。

因此,请确认在启动 Langchain-Chatchat 项目前,首先进行模型推理框架的运行,并加载所需使用的模型。

这里以 Xinference 举例, 请参考 Xinference文档 进行框架部署与模型加载。

Warning

为避免依赖冲突,请将 Langchain-Chatchat 和模型部署框架如 Xinference 等放在不同的 Python 虚拟环境中, 比如 conda, venv, virtualenv 等。

3. 查看与修改 Langchain-Chatchat 配置

从 0.3.0 版本起,Langchain-Chatchat 不再使用本地文件的方式进行配置修改,改为使用命令行的方式,并会在后续版本中增加配置项修改页面。

以下从查看配置、修改配置两种操作类型进行介绍。

3.1 查看 chatchat-config 命令帮助

输入以下命令查看可选配置类型:

chatchat-config --help

这时会得到返回:

Usage: chatchat-config [OPTIONS] COMMAND [ARGS]...

  指令` chatchat-config` 工作空间配置

Options:
  --help  Show this message and exit.

Commands:
  basic   基础配置
  kb      知识库配置
  model   模型配置
  server  服务配置

可根据上述配置命令选择需要查看或修改的配置类型,以基础配置为例,想要进行基础配置查看或修改时可以输入以下命令获取帮助信息:

chatchat-config basic --help

这时会得到返回信息:

Usage: chatchat-config basic [OPTIONS]

  基础配置

Options:
  --verbose [true|false]  是否开启详细日志
  --data TEXT             初始化数据存放路径,注意:目录会清空重建
  --format TEXT           日志格式
  --clear                 清除配置
  --show                  显示配置
  --help                  Show this message and exit.
3.2 使用 chatchat-config 查看对应配置参数

基础配置为例,可根据上述命令帮助内容确认,需要查看基础配置的配置参数,可直接输入:

chatchat-config basic --show

在未进行配置项修改时,可得到默认配置内容如下:

{
    "log_verbose": false,
    "CHATCHAT_ROOT": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat",
    "DATA_PATH": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data",
    "IMG_DIR": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/img",
    "NLTK_DATA_PATH": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/nltk_data",
    "LOG_FORMAT": "%(asctime)s - %(filename)s[line:%(lineno)d] - %(levelname)s: %(message)s",
    "LOG_PATH": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/logs",
    "MEDIA_PATH": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/media",
    "BASE_TEMP_DIR": "/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/temp",
    "class_name": "ConfigBasic"
}
3.3 使用 chatchat-config 修改对应配置参数

以修改模型配置默认llm模型为例,可以执行以下命令行查看配置项名称:

chatchat-config model --help

这时会得到

Usage: chatchat-config model [OPTIONS]

  模型配置

Options:
  --default_llm_model TEXT        默认llm模型
  --default_embedding_model TEXT  默认embedding模型
  --agent_model TEXT              agent模型
  --history_len INTEGER           历史长度
  --max_tokens INTEGER            最大tokens
  --temperature FLOAT             温度
  --support_agent_models TEXT     支持的agent模型
  --set_model_platforms TEXT      模型平台配置 as a JSON string.
  --set_tool_config TEXT          工具配置项  as a JSON string.
  --clear                         清除配置
  --show                          显示配置
  --help                          Show this message and exit.

可首先查看当前模型配置的配置项:

chatchat-config model --show

这时会得到:

{
    "DEFAULT_LLM_MODEL": "glm4-chat",
    "DEFAULT_EMBEDDING_MODEL": "bge-large-zh-v1.5",
    "Agent_MODEL": null,
    "HISTORY_LEN": 3,
    "MAX_TOKENS": null,
    "TEMPERATURE": 0.7,
    ...
    "class_name": "ConfigModel"
}

需要修改默认llm模型qwen2-instruct时,可执行:

chatchat-config model --default_llm_model qwen2-instruct

更多配置项修改帮助请参考 README.md

4. 自定义模型接入配置

完成上述项目配置项查看与修改后,需要根据步骤2. 模型推理框架并加载模型 中选用的模型推理框架与加载的模型进行模型接入配置,其中模型推理框架包括 XinferenceOllamaLocalAIFastChatOne API 等,可以提供 GLM-4-ChatQwen2-Instruct 等中文最新开源模型的接入支持。 如果您已经有了一个openai endpoint的能力的地址,可以在MODEL_PLATFORMS这里直接配置

chatchat-config model --set_model_platforms TEXT      模型平台配置 as a JSON string.
  • platform_name 可以任意填写,不要重复即可
  • platform_type 以后可能根据平台类型做一些功能区分,与platform_name一致即可
  • 将框架部署的模型填写到对应列表即可。不同框架可以加载同名模型,项目会自动做负载均衡。
  • 设置模型
$ chatchat-config model --set_model_platforms "[{
    \"platform_name\": \"xinference\",
    \"platform_type\": \"xinference\",
    \"api_base_url\": \"http://127.0.0.1:9997/v1\",
    \"api_key\": \"EMPT\",
    \"api_concurrencies\": 5,
    \"llm_models\": [
        \"autodl-tmp-glm-4-9b-chat\"
    ],
    \"embed_models\": [
        \"bge-large-zh-v1.5\"
    ],
    \"image_models\": [],
    \"reranking_models\": [],
    \"speech2text_models\": [],
    \"tts_models\": []
}]"

5. 初始化知识库

Warning

进行知识库初始化前,请确保已经启动模型推理框架及对应 embedding 模型,且已按照上述步骤3步骤4完成模型接入配置。

chatchat-kb -r

指定 text-embedding 模型进行初始化(如有需要):

cd # 回到原始目录
chatchat-kb -r --embed-model=text-embedding-3-small

出现以下日志即为成功:


----------------------------------------------------------------------------------------------------
知识库名称      :samples
知识库类型      :faiss
向量模型:      :bge-large-zh-v1.5
知识库路径      :/root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/knowledge_base/samples
文件总数量      :47
入库文件数      :42
知识条目数      :740
用时            :0:02:29.701002
----------------------------------------------------------------------------------------------------

总计用时        :0:02:33.414425

知识库路径为 3.2 中 DATA_PATH 变量指向的路径下的 knowledge_base 目录中:

(chatchat) [root@VM-centos ~]# ls /root/anaconda3/envs/chatchat/lib/python3.11/site-packages/chatchat/data/knowledge_base/samples/vector_store
bge-large-zh-v1.5  text-embedding-3-small

6. 启动项目

chatchat -a

出现以下界面即为启动成功:

WebUI界面

Warning

由于 chatchat-config server 配置默认监听地址 DEFAULT_BIND_HOST 为 127.0.0.1, 所以无法通过其他 ip 进行访问。

如需修改请参考以下方式:

```shell chatchat-config server --show ```

这时会得到

{
    "HTTPX_DEFAULT_TIMEOUT": 300.0,
    "OPEN_CROSS_DOMAIN": true,
    "DEFAULT_BIND_HOST": "127.0.0.1",
    "WEBUI_SERVER_PORT": 8501,
    "API_SERVER_PORT": 7861,
    "WEBUI_SERVER": {
        "host": "127.0.0.1",
        "port": 8501
    },
    "API_SERVER": {
        "host": "127.0.0.1",
        "port": 7861
    },
    "class_name": "ConfigServer"
}

如需通过机器ip 进行访问(如 Linux 系统), 需要将监听地址修改为 0.0.0.0。

chatchat-config server --default_bind_host=0.0.0.0

这时会得到

{
    "HTTPX_DEFAULT_TIMEOUT": 300.0,
    "OPEN_CROSS_DOMAIN": true,
    "DEFAULT_BIND_HOST": "0.0.0.0",
    "WEBUI_SERVER_PORT": 8501,
    "API_SERVER_PORT": 7861,
    "WEBUI_SERVER": {
        "host": "0.0.0.0",
        "port": 8501
    },
    "API_SERVER": {
        "host": "0.0.0.0",
        "port": 7861
    },
    "class_name": "ConfigServer"
}

Docker 部署

docker pull chatimage/chatchat:0.3.0-2024-0624

Important

强烈建议: 使用 docker-compose 部署, 具体参考 README_docker

旧版本迁移

  • 0.3.x 结构改变很大,强烈建议您按照文档重新部署. 以下指南不保证100%兼容和成功. 记得提前备份重要数据!
  • 首先按照 安装部署 中的步骤配置运行环境
  • 配置 DATA 等选项
  • 将 0.2.x 项目的 knowledge_base 目录拷贝到配置的 DATA 目录下

项目里程碑

  • 2023年4月: Langchain-ChatGLM 0.1.0 发布,支持基于 ChatGLM-6B 模型的本地知识库问答。

  • 2023年8月: Langchain-ChatGLM 改名为 Langchain-Chatchat,发布 0.2.0 版本,使用 fastchat 作为模型加载方案,支持更多的模型和数据库。

  • 2023年10月: Langchain-Chatchat 0.2.5 发布,推出 Agent 内容,开源项目在Founder Park & Zhipu AI & Zilliz 举办的黑客马拉松获得三等奖。

  • 2023年12月: Langchain-Chatchat 开源项目获得超过 20K stars.

  • 2024年6月: Langchain-Chatchat 0.3.0 发布,带来全新项目架构。

  • 🔥 让我们一起期待未来 Chatchat 的故事 ···


协议

本项目代码遵循 Apache-2.0 协议。

联系我们

Telegram

Telegram

项目交流群

二维码

🎉 Langchain-Chatchat 项目微信交流群,如果你也对本项目感兴趣,欢迎加入群聊参与讨论交流。

公众号

二维码

🎉 Langchain-Chatchat 项目官方公众号,欢迎扫码关注。

引用

如果本项目有帮助到您的研究,请引用我们:

@software{langchain_chatchat,
    title        = {{langchain-chatchat}},
    author       = {Liu, Qian and Song, Jinke, and Huang, Zhiguo, and Zhang, Yuxuan, and glide-the, and liunux4odoo},
    year         = 2024,
    journal      = {GitHub repository},
    publisher    = {GitHub},
    howpublished = {\url{https://github.com/chatchat-space/Langchain-Chatchat}}
}

langchain-chatchat's People

Contributors

imclumsypanda avatar liunux4odoo avatar hzg0601 avatar zrzrzrzrzrzrzr avatar fxjhello avatar zqtgit avatar yuehua-s avatar zqt996 avatar calcitem avatar fengyunzaidushi avatar inksong avatar khazic avatar srszzw avatar keenzhu avatar sysalong avatar changxubo avatar eltociear avatar qqlww1987 avatar margox avatar showmecodett avatar kztao avatar qiankunli avatar liangtongt avatar bones-zhu avatar ykk648 avatar chinainfant avatar pearjelly avatar xldistance avatar yawudede avatar zhenkaivip avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.