L1G4000 任务InternLM + LlamaIndex RAG 实践

一、启动开发机

启动30% A100机器。

进入开发机后,从官方环境复制运行 InternLM 的基础环境,命名为 llamaindex,在命令行模式下运行:

studio-conda -t llamaindex -o pytorch-2.1.2

 

复制完成后,在本地查看环境。

conda env list

结果如下所示。

# conda environments: 
# 
base * /root/.conda 
llamaindex /root/.conda/envs/llamaindex

运行 conda 命令,激活 llamaindex python 虚拟环境:

conda activate llamaindex 

  1. 安装 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.下载 Sentence Transformer 模型

源词向量模型 Sentence Transformer:(我们也可以选用别的开源词向量模型来进行 Embedding,目前选用这个模型是相对轻量、支持中文且效果较好的,同学们可以自由尝试别的开源词向量模型)

运行以下指令,新建一个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/data 目录下执行该脚本即可自动开始下载:

conda activate llamaindex 
python download_hf.py

更多关于镜像使用可以移步至 HF Mirror 查看。

3.下载 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

之后使用时服务器即会自动使用已有资源,无需再次下载。

4.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

结果为:

5.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

结果为:

任务

提问

xtuner是什么?

xtuner支持哪些模型?

结果:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值