1.前置知识
正式介绍检索增强生成(Retrieval Augmented Generation,RAG)技术以前,大家不妨想想为什么会出现这样一个技术。
给模型注入新知识的方式,可以简单分为两种方式,一种是内部的,即更新模型的权重,另一个就是外部的方式,给模型注入格外的上下文或者说外部信息,不改变它的的权重。
第一种方式,改变了模型的权重即进行模型训练,这是一件代价比较大的事情,大语言模型具体的训练过程,可以参考InternLM2技术报告。第二种方式,并不改变模型的权重,只是给模型引入格外的信息。类比人类编程的过程,第一种方式相当于你记住了某个函数的用法,第二种方式相当于你阅读函数文档然后短暂的记住了某个函数的用法。
对比两种注入知识方式,第二种更容易实现。RAG正是这种方式。它能够让基础模型实现非参数知识更新,无需训练就可以掌握新领域的知识。本次课程选用了LlamaIndex框架。LlamaIndex 是一个上下文增强的 LLM 框架,旨在通过将其与特定上下文数据集集成,增强大型语言模型(LLMs)的能力。它允许您构建应用程序,既利用 LLMs 的优势,又融入您的私有或领域特定信息。
RAG 效果比对
如图所示,由于xtuner
是一款比较新的框架, InternLM2-Chat-1.8B
训练数据库中并没有收录到它的相关信息。左图中问答均未给出准确的答案。右图未对 InternLM2-Chat-1.8B
进行任何增训的情况下,通过 RAG 技术实现的新增知识问答。
2.环境安装
进入开发机后,从官方环境复制运行 InternLM 的基础环境,命名为 llamaindex
,在命令行模式下运行:
studio-conda -t llamaindex -o pytorch-2.1.2
复制完成后,在本地查看环境。
conda env list
结果如下所示。
运行 conda
命令,激活 llamaindex
python 虚拟环境:
conda activate llamaindex
环境激活后,命令行左边会显示当前(也就是 llamaindex
)的环境名称,如下图所示:
安装 Llamaindex
安装 Llamaindex和相关的包
pip install llama-index==0.10.38 llama-index-llms-huggingface==0.2.0 "transformers[torch]==4.41.1" "huggingface_hub[inference]==0.23.1" huggingface_hub==0.23.1 sentence-transformers==2.7.0 sentencepiece==0.2.0 -i https://pypi.tuna.tsinghua.edu.cn/simple/
下载 Sentence Transformer 模型
运行以下指令,新建一个python文件
cd ~
mkdir llamaindex_demo
mkdir model
cd ~/llamaindex_demo
touch download_hf.py
打开download_hf.py
贴入以下代码
import os
# 设置环境变量
os.environ['HF_ENDPOINT'] = 'https://hf-mirror.com'
# 下载模型
os.system('huggingface-cli download --resume-download sentence-transformers/paraphrase-multilingual-MiniLM-L12-v2 --local-dir /root/model/sentence-transformer')
然后,在 /root/llamaindex_demo
目录下执行该脚本即可自动开始下载:
conda activate llamaindex
python download_hf.py
下载 NLTK 相关资源
我们在使用开源词向量模型构建开源词向量的时候,需要用到第三方库 nltk 的一些资源。正常情况下,其会自动从互联网上下载,但可能由于网络原因会导致下载中断,此处我们可以从国内仓库镜像地址下载相关资源,保存到服务器上。
我们用以下命令下载 nltk 资源并解压到服务器上:
cd /root
git clone https://gitee.com/yzy0612/nltk_data.git --branch gh-pages
cd nltk_data
mv packages/* ./
cd tokenizers
unzip punkt.zip
cd ../taggers
unzip averaged_perceptron_tagger.zip
3.LlamaIndex HuggingFaceLLM
运行以下指令,把 InternLM2 1.8B 软连接出来
cd ~/model
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b/ ./
运行以下指令,新建一个python文件
cd ~/llamaindex_demo
touch llamaindex_internlm.py
打开llamaindex_internlm.py
贴入以下代码
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core.llms import ChatMessage
llm = HuggingFaceLLM(
model_name="/root/model/internlm2-chat-1_8b",
tokenizer_name="/root/model/internlm2-chat-1_8b",
model_kwargs={"trust_remote_code":True},
tokenizer_kwargs={"trust_remote_code":True}
)
rsp = llm.chat(messages=[ChatMessage(content="xtuner是什么?")])
print(rsp)
之后运行
conda activate llamaindex
cd ~/llamaindex_demo/
python llamaindex_internlm.py
结果为:
回答的效果并不好,并不是我们想要的xtuner。
4.LlamaIndex RAG
安装 LlamaIndex 词嵌入向量依赖
conda activate llamaindex
pip install llama-index-embeddings-huggingface llama-index-embeddings-instructor
运行以下命令,获取知识库
cd ~/llamaindex_demo
mkdir data
cd data
git clone https://github.com/InternLM/xtuner.git
mv xtuner/README_zh-CN.md ./
运行以下指令,新建一个python文件
cd ~/llamaindex_demo
touch llamaindex_RAG.py
打开llamaindex_RAG.py
贴入以下代码
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLM
embed_model = HuggingFaceEmbedding(
model_name="/root/model/sentence-transformer"
)
Settings.embed_model = embed_model
llm = HuggingFaceLLM(
model_name="/root/model/internlm2-chat-1_8b",
tokenizer_name="/root/model/internlm2-chat-1_8b",
model_kwargs={"trust_remote_code":True},
tokenizer_kwargs={"trust_remote_code":True}
)
Settings.llm = llm
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
index = VectorStoreIndex.from_documents(documents)
query_engine = index.as_query_engine()
response = query_engine.query("xtuner是什么?")
print(response)
之后运行
conda activate llamaindex
cd ~/llamaindex_demo/
python llamaindex_RAG.py
结果为:
这个时候答案就准确多了,通过代码我们看到README_zh-CN.md 里面涉及到XTuner里面的内容进行文本向量化了。然后我们在问xtuner是什么?的时候模型先从向量模型数据库里面检索到相似的问题,然后在把答案发送给模型,模型通过推理总结归纳出已经检索到向量文本信息返回给客户这样的答案就清晰多了。
感兴趣的小伙伴可以上传其他文档内容对他进行向量化。这里主要是方便演示就拿xtuner README_zh-CN.md 中文文档来做介绍了。