1.前置知识
RAG,Retrieval Augmented Generation 检索增强生成技术。RAG是一种给模型注入新知识的方式,并不改变模型的权重,只是给模型引入额外的信息。RAG能够让基础模型实现非参数知识更新,无需训练就可以掌握新领域的知识。
LlamaIndex是一个上下文增强的LLM框架,旨在通过将其与特定上下文数据集成,增强大型语言模型的能力。
我们这次使用的大模型是InternLM2-Chat-1.8B
2.环境、模型准备
2.1 配置基础环境
本次使用30% A100,使用cuda 11.7镜像。
创建一个名为llamaindex的虚拟环境,安装相关基础以来python虚拟环境:
conda activate llamaindex
conda install pytorch==2.0.1 torchvision==0.15.2 torchaudio==2.0.2 pytorch-cuda=11.7 -c pytorch -c nvidia
pip install einops
pip install protobuf
2.2 安装Llamaindex
安装llamaindex相关的包
conda activate 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
2.3下载Sentence Transformer模型
使用轻量级源词-向量模型sentence transofrmer
进行embedding。新建脚本downlaod_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')
执行该脚本可以开始自动下载sentence transformer
代码使用huggingface-cli进行下载,可以参考HF Mirror
2.4 下载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/ ./
新建脚本llamaindex_intern.py,贴入代码:
from llama_index.llms.huggingface import HuggingFaceLLM
from llama_index.core.llms