【书生2.4】InternLM + LlamaIndex RAG 实践

【Intern Studio的gpu不足。本实验使用自有服务器】

1 环境安装

conda create -n llamaindex python=3.10
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==0.7.0 protobuf==5.26.1
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

#llama-index -rag相关
pip install llama-index-embeddings-huggingface==0.2.0 llama-index-embeddings-instructor==0.1.3

2 材料准备

  • internlm2-chat-1_8b模型下载
from modelscope.hub.snapshot_download import snapshot_download
 
model_dir = snapshot_download('Shanghai_AI_Laboratory/internlm2-chat-1_8b', cache_dir='./model_dir', revision='master')
  • embeding 模型下载
# embeding 模型下载;需要科学上网
cd /project
mkdir llamaindex_demo
mkdir model
cd /project/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')
  • 开源Embedding配套nltk
cd /project/llamaindex_demo
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
  • 设置软链接(可选,目的是简化路径)
cd /project/model
ln -s /project/models/model_dir/Shanghai_AI_Laboratory/internlm2-chat-1_8b/ ./
  • rag的知识库准备
cd /project/llamaindex_demo
mkdir data
cd data
git clone https://github.com/InternLM/xtuner.git
mv xtuner/README_zh-CN.md ./
  • 使用gpt生成一些独一无二的知识

    注:以下定义百度是没有的,是通过gpt生成的

cd /project/llamaindex_demo
mkdir data2
cd data2
vi k.txt
#k.txt内容如下
布鲁鲁尔(v.)- 指一种奇特的行为,即在吃冰淇淋时用鼻子呼出咖啡香气。这种行为被认为是一种超凡的美食体验,能够唤醒沉睡的味蕾。

炫烤器(n.)- 一种极为罕见且令人兴奋的厨房用具,据传说,它能将食物瞬间变成光亮的炫彩状态。使用炫烤器烹饪的食物会散发出奇幻的色彩和迷人的香气,令人难以置信。

智能笑话机器人(n.)- 这是一个不同寻常的机器人,其唯一任务是制作让人捧腹大笑的笑话。智能笑话机器人具备人工智能技术,可以根据不同的情境和听众的喜好创造个性化的笑话。它是喜剧表演的终极替代品。

量子思维帽(n.)- 一种奇妙的头戴设备,据说它能够扩展人类思维的边界。穿戴量子思维帽后,个体可以在瞬间获得超越常人的洞察力和创造力,从而解决世界上最复杂的问题。

反引力水晶(n.)- 一种传说中的神奇水晶,据说能够产生反引力效应。这种水晶被认为是引力定律的完全逆转者,当放置在物体下方时,它可以使物体向上漂浮,并且不受重力的影响。

3 还原实验

cd /project/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)

  • 运行代码
    python llamaindex_internlm.py
cd /project/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

#初始化一个HuggingFaceEmbedding对象,用于将文本转换为向量表示
embed_model = HuggingFaceEmbedding(
#指定了一个预训练的sentence-transformer模型的路径
    model_name="/root/model/sentence-transformer"
)
#将创建的嵌入模型赋值给全局设置的embed_model属性,
#这样在后续的索引构建过程中就会使用这个模型。
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}
)
#设置全局的llm属性,这样在索引查询时会使用这个模型。
Settings.llm = llm

#从指定目录读取所有文档,并加载数据到内存中
documents = SimpleDirectoryReader("/root/llamaindex_demo/data").load_data()
#创建一个VectorStoreIndex,并使用之前加载的文档来构建索引。
# 此索引将文档转换为向量,并存储这些向量以便于快速检索。
index = VectorStoreIndex.from_documents(documents)
# 创建一个查询引擎,这个引擎可以接收查询并返回相关文档的响应。
query_engine = index.as_query_engine()
response = query_engine.query("xtuner是什么?")

print(response)

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

4 更换问题验证rag

cp llamaindex_internlm.py llamaindex_internlm2.py
cp llamaindex_RAG.py llamaindex_RAG2.py
  • 修改问题为:反引力水晶是什么?

  • 原始回答

python llamaindex_internlm2.py

在这里插入图片描述

  • 结合知识库的rag回答
python llamaindex_internlm2.py

在这里插入图片描述

和第一步中准备的知识库k.txt中的内容一致

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

PythonJavaC++go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值