【书生大模型实战营(暑假场)】进阶任务五 茴香豆:企业级知识库问答工具

进阶任务五 茴香豆:企业级知识库问答工具

1 茴香豆基本介绍

茴香豆 是由书生·浦语团队开发的一款开源、专门针对国内企业级使用场景设计并优化的知识问答工具。在基础 RAG 课程中我们了解到,RAG 可以有效的帮助提高 LLM 知识检索的相关性、实时性,同时避免 LLM 训练带来的巨大成本。在实际的生产和生活环境需求,对 RAG 系统的开发、部署和调优的挑战更大,如需要解决群应答、能够无关问题拒答、多渠道应答、更高的安全性挑战。因此,根据大量国内用户的实际需求,总结出了三阶段Pipeline的茴香豆知识问答助手架构,帮助企业级用户可以快速上手安装部署。

茴香豆特点:

  • 三阶段 Pipeline (前处理、拒答、响应),提高相应准确率和安全性
  • 打通微信和飞书群聊天,适合国内知识问答场景
  • 支持各种硬件配置安装,安装部署限制条件少
  • 适配性强,兼容多个 LLM 和 API
  • 傻瓜操作,安装和配置方便

在这里插入图片描述

2 茴香豆本地标准版搭建实战

任务
尝试将茴香豆从源码部署到本地服务器(以 InternlmLM Studio 为例),并开发一款简单的知识助手 Demo。

2.1 环境配置和源码准备

首先,准备 huixiangdou 专用 conda 环境,并安装相关依赖,完成环境配置;并且从官方码仓拉取仓库,并进行文件安装。

# 创建茴香豆专用 conda 环境
studio-conda -o internlm-base -t huixiangdou

# 激活环境
conda activate huixiangdou

# 从茴香豆仓库拉取代码到服务器
cd /root
# 克隆代码仓库并进入 huixiangdou 文件夹
git clone https://github.com/internlm/huixiangdou && cd huixiangdou
git checkout 79fa810

# 安装茴香豆所需依赖
# parsing `word` format requirements
apt update
apt install python-dev libxml2-dev libxslt1-dev antiword unrtf poppler-utils pstotext tesseract-ocr flac ffmpeg lame libmad0 libsox-fmt-mp3 sox libjpeg-dev swig libpulse-dev
# python requirements
pip install BCEmbedding==0.1.5 cmake==3.30.2 lit==18.1.8 sentencepiece==0.2.0 protobuf==5.27.3 accelerate==0.33.0
pip install -r requirements.txt
# python3.8 安装 faiss-gpu 而不是 faiss

2.2 模型文件配置

茴香豆默认可以根据配置文件自动下载对应模型文件。但模型已经预载在开发机中,可以通过软连接直接配置模型文件。在这里我们选择的大模型是internlm2-chat-7b 大模型。

# 创建模型文件夹
cd /root && mkdir models

# 复制BCE模型
ln -s /root/share/new_models/maidalun1020/bce-embedding-base_v1 /root/models/bce-embedding-base_v1
ln -s /root/share/new_models/maidalun1020/bce-reranker-base_v1 /root/models/bce-reranker-base_v1

# 复制大模型参数(下面的模型,根据作业进度和任务进行**选择一个**就行)
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-7b /root/models/internlm2-chat-7b

在这里插入图片描述

2.3 更改配置文件

茴香豆的功能管理和模型切换可通过 config.ini 文件进行修改,默认参数如下:

[feature_store]
# `feature_store.py` use this throttle to distinct `good_questions` and `bad_questions`
reject_throttle = -1.0
# text2vec model, support local relative path, huggingface repo and URL.
# for example:
#  "maidalun1020/bce-embedding-base_v1"
#  "BAAI/bge-m3"
#  "https://api.siliconflow.cn/v1/embeddings"
embedding_model_path = "maidalun1020/bce-embedding-base_v1"

# reranker model, support list:
#  "maidalun1020/bce-reranker-base_v1"
#  "BAAI/bge-reranker-v2-minicpm-layerwise"
#  "https://api.siliconflow.cn/v1/rerank"
reranker_model_path = "maidalun1020/bce-reranker-base_v1"

# if using `siliconcloud` API as `embedding_model_path` or `reranker_model_path`, give the token
api_token = ""
api_rpm = 1000
work_dir = "workdir"

[web_search]
engine = "serper"
# web search engine support ddgs and serper
# For ddgs, see https://pypi.org/project/duckduckgo-search
# For serper, check https://serper.dev/api-key to get a free API key
serper_x_api_key = "YOUR-API-KEY-HERE"
domain_partial_order = ["arxiv.org", "openai.com", "pytorch.org", "readthedocs.io", "nvidia.com", "stackoverflow.com", "juejin.cn", "zhuanlan.zhihu.com", "www.cnblogs.com"]
save_dir = "logs/web_search_result"

[llm]
enable_local = 1
enable_remote = 0
# hybrid llm service address
client_url = "http://127.0.0.1:8888/inference"

[llm.server]
# local LLM configuration
# support "internlm/internlm2-chat-7b", "internlm2_5-7b-chat" and "qwen/qwen-7b-chat-int8"
# support local path, for example
# local_llm_path = "/path/to/your/internlm2_5"

local_llm_path = "internlm/internlm2_5-7b-chat"
local_llm_max_text_length = 3000
# llm server listen port
local_llm_bind_port = 8888

# remote LLM service configuration
# support "gpt", "kimi", "deepseek", "zhipuai", "step", "internlm", "xi-api" and "alles-apin"
# support "siliconcloud", see https://siliconflow.cn/zh-cn/siliconcloud
# xi-api and alles-apin is chinese gpt proxy
# for internlm, see https://internlm.intern-ai.org.cn/api/document

remote_type = "kimi"
remote_api_key = "YOUR-API-KEY-HERE"
# max text length for remote LLM.
# use 128000 for kimi, 192000 for gpt/xi-api, 16000 for deepseek, 128000 for zhipuai, 40000 for internlm2
remote_llm_max_text_length = 128000
# openai API model type, support model list:
# "auto" for kimi. To save money, we auto select model name by prompt length.
# "auto" for step to save money, see https://platform.stepfun.com/
# "gpt-4-0613" for gpt/xi-api,
# "deepseek-chat" for deepseek,
# "glm-4" for zhipuai,
# "gpt-4-1106-preview" for alles-apin or OpenAOE
# "internlm2-latest" for internlm
# for example "alibaba/Qwen1.5-110B-Chat", see https://siliconflow.readme.io/reference/chat-completions-1
remote_llm_model = "auto"
# request per minute
rpm = 500

在终端执行以下命令更改配置文件,让茴香豆使用本地模型:

sed -i '9s#.*#embedding_model_path = "/root/models/bce-embedding-base_v1"#' /root/huixiangdou/config.ini
sed -i '15s#.*#reranker_model_path = "/root/models/bce-reranker-base_v1"#' /root/huixiangdou/config.ini
sed -i '43s#.*#local_llm_path = "/root/models/internlm2-chat-7b"#' /root/huixiangdou/config.ini

修改后,配置文件部分改变,茴香豆将使用本地模型:
在这里插入图片描述

注意!配置文件默认的模型和下载好的模型相同。如果不修改地址为本地模型地址,茴香豆将自动从 huggingface hub 拉取模型。如果选择拉取模型的方式,需要提前在命令行中运行 huggingface-cli login 命令,验证 huggingface 权限。

2.4 知识库创建

完成配置文件后,搭建知识库,除了茴香豆和 MMPose 的文档,这里还选取多个 github 上开源的机器学习,深度学习,大模型相关的开源资料。我们在 huixiangdou 文件加下创建 repodir 文件夹,用来储存知识库的原始文档。再创建一个文件夹 workdir 用来存放原始文档特征提取到的向量知识库。运行以下指令将完成知识库的构建以及从原始文档提取为向量知识库。我们期望可以得到一个了解茴香豆,MMPose,机器学习,深度学习和大模型知识的问答助手。

conda activate huixiangdou

cd /root/huixiangdou && mkdir repodir

git clone https://github.com/fengdu78/Coursera-ML-AndrewNg-Notes.git --depth=1 repodir/Coursera-ML-AndrewNg-Notes
git clone https://github.com/d2l-ai/d2l-zh.git --depth=1 repodir/d2l-zh
git clone https://github.com/amusi/Deep-Learning-Interview-Book.git --depth=1 repodir/Deep-Learning-Interview-Book
git clone https://github.com/unclestrong/DeepLearning_LHY21_Notes.git --depth=1 repodir/DeepLearning_LHY21_Notes
git clone https://github.com/datawhalechina/leedl-tutorial.git --depth=1 repodir/leedl-tutorial
git clone https://github.com/datawhalechina/so-large-lm.git --depth=1 repodir/so-large-lm
git clone https://github.com/datawhalechina/self-llm.git --depth=1 repodir/self-llm

# Save the features of repodir to workdir, and update the positive and negative example thresholds into `config.ini`
mkdir workdir
python3 -m huixiangdou.service.feature_store

在这里插入图片描述

可以看到,知识库创建过程中进行了一系列小测试,检验问题拒答和响应效果。对于在知识库内的问题,进行了有依据的回答;不在知识库内的问题则选择了拒答,一定程度上提高了茴香豆的稳定性。

本地板也可以编辑正例和反例来调整茴香豆的拒答和响应,正反例位置如:

在这里插入图片描述

在应用中需要注意的是,每次更新只是文档和正反例,都必须重新运行 python3 -m huixiangdou.service.feature_store 命令,以进行向量知识库的重新创建和应答阈值的更新。更新也会直接反映在配置文件中:
在这里插入图片描述

2.5 茴香豆知识助手测试

2.5.1 在终端命令行部署茴香豆

运行以下指令,可以在终端测试目前的茴香豆知识助手。这种基于命令行的方式可以看到对话的结果及中间过程,便于确认知识库是否覆盖需求,正反例设计是否合理。

conda activate huixiangdou
cd /root/huixiangdou
python3 -m huixiangdou.main --standalone

在询问对话中,可以发现茴香豆在知识库中检索出了类相关的知识内容,并根据这些知识文档进行了回复,命令行中的输出也可以看出茴香豆向知识库的搜索:
在这里插入图片描述
在这里插入图片描述

2.5.2 Gradio 部署茴香豆

茴香豆也用 Gradio 搭建了一个 Web UI 的交互界面,可以支持本地茴香豆助手的测试效果。我们运行以下指令,然后进行端口映射,便可以与在 Web 界面的 Gradio 支持的茴香豆助手进行交互:

conda activate huixiangdou
cd /root/huixiangdou
python3 -m huixiangdou.gradio

这里有一个小插曲,即按照运行后指定的 7860 端口去访问,始终无法显示 Web UI:

在这里插入图片描述
在这里插入图片描述
我们根据社区大佬的解决方案

把开发机的7860端口映射到本地的7870端口试一试能否在笔记本的7870访问。还不行的话,就建议所有终端都kill了,然后重新启动huixiangdou的gradio界面

我们使用以下指令进行映射后,成功在本地的 7870 端口进行了访问:

ssh -CNg -L 7870:127.0.0.1:7860 root@ssh.intern-ai.org.cn -p <开发机端口号>

然后我们便可以在 Gradio 界面与大模型进行对话,发现在禁用网页搜索时,茴香豆也能在回复时很好的抽取知识库对应的内容,给出可靠的回答:
在这里插入图片描述

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值