首页 > 其他分享 >浦语Camp3:基础4-Llamaindex RAG实践

浦语Camp3:基础4-Llamaindex RAG实践

时间:2024-08-25 15:55:07浏览次数:7  
标签:RAG index Llamaindex llamaindex cd llama st model Camp3

基础任务

  • 任务要求:基于 LlamaIndex 构建自己的 RAG 知识库,寻找一个问题 A 在使用 LlamaIndex 之前InternLM2-Chat-1.8B模型不会回答,借助 LlamaIndex 后 InternLM2-Chat-1.8B 模型具备回答 A 的能力,截图保存。

需要A100 30%内存!!!

安装环境:

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

环境激活后,命令行左边会显示当前(也就是 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

下载Sentence Transformer 模型

源词向量模型 Sentence Transformer:(这个模型是相对轻量、支持中文且效果较好的,也可自由尝试别的开源词向量模型) 运行以下指令,新建一个python文件

cd ~
mkdir llamaindex_demo
mkdir model
cd ~/llamaindex_demo
touch download_hf.py

在download_hf.py中输入以下代码

输入以下命令自动下载

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

LlamaIndex HuggingFaceLLM

用软链接复制 InternLM2 1.8B

cd ~/model
ln -s /root/share/new_models/Shanghai_AI_Laboratory/internlm2-chat-1_8b/ ./



cd ~/llamaindex_demo
touch llamaindex_internlm.py

打开llamaindex_internlm.py 贴入以下代码

运行 python llamaindex_internlm.py

可知现在InternLM2 1.8B还无法识别xtuner,之后我们借助RAG

LlamaIndex RAG

安装 LlamaIndex 词嵌入向量依赖

conda activate llamaindex
pip install llama-index-embeddings-huggingface==0.2.0 llama-index-embeddings-instructor==0.1.3

运行以下命令,获取知识库,将xtuner的README.md传给模型

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

#初始化一个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)

运行

可以成功解释xtuner是什么了!!

LlamaIndex web

运行之前首先安装依赖 pip install streamlit==1.36.0

运行以下指令,新建一个python文件

cd ~/llamaindex_demo
touch app.py

打开app.py贴入以下代码

import streamlit as st
from llama_index.core import VectorStoreIndex, SimpleDirectoryReader, Settings
from llama_index.embeddings.huggingface import HuggingFaceEmbedding
from llama_index.llms.huggingface import HuggingFaceLLM

st.set_page_config(page_title="llama_index_demo", page_icon="

标签:RAG,index,Llamaindex,llamaindex,cd,llama,st,model,Camp3
From: https://blog.csdn.net/weixin_46610879/article/details/141472317

相关文章

  • 浦语Camp3:入门3-Git基础
    任务等级任务内容必做任务破冰,提交一份自我介绍必做任务创建并提交一个项目1.破冰,提交一份自我介绍因此使用gitconfig--global命令来设置用户名和用户邮件执行:gitconfig--globaluser.name"YourName"#真实信息脱敏处理gitconfig--globaluser.email"your......
  • 浦语Camp3:入门2-Python基础
    任务类型任务内容闯关任务python实现wordcount闯关任务Vscode连接InternStudiodebug笔记1.python实现wordcount请实现一个wordcount函数,统计英文字符串中每个单词出现的次数。返回一个字典,key为单词,value为对应单词出现的次数text="""Gotthispandaplushtoyformyd......
  • 浦语Camp3:入门1-Linux基础
    任务等级任务内容闯关任务完成SSH连接与端口映射并运行hello_world.py可选任务1将Linux基础命令在开发机上完成一遍可选任务2使用VSCODE远程连接开发机并创建一个conda环境可选任务3创建并运行test.sh文件1.完成SSH连接与端口映射并运行hello_world.py新建hello_world.p......
  • RAG
    RAG,全称为“Retrieval-AugmentedGeneration”,是一种结合信息检索和生成式模型的方法。它主要用于提高生成式模型(如GPT、BERT等)的准确性和实用性,特别是在需要从大规模知识库或文档中提取相关信息的任务中。RAG的工作原理:信息检索(Retrieval):首先,从一个预定义的知识库或文档集合......
  • OpenCV(cv::FileStorage())
    目录1.功能2.方法和用法3.示例3.1写入数据3.2读取数据4.常见数据格式5.注意事项6.总结cv::FileStorage()是OpenCV提供的一个用于读写文件的类,主要用于读取和写入结构化数据(如XML、YAML或JSON文件)。它是用于在文件和内存之间存储和检索复杂数据结构的工具,例如矩......
  • vue3实现拖拽效果 (vuedraggable)
    效果图使用vuedraggable实现拖拽真的是特别丝滑和简单!!下载这里是vue3版本的对应vuedraggable版本4.1.0不要下错了!!!npminstallvuedraggable@4pnpmaddvuedraggable@4官网https://github.com/SortableJS/Vue.Draggable中文网https://www.itxst.com/vue-dragg......
  • 【LLM & RAG & text2sql】大模型在知识图谱问答上的核心算法详细思路及实践
    前言本文介绍了一个融合RAG(Retrieval-AugmentedGeneration)思路的KBQA(Knowledge-BasedQuestionAnswering)系统的核心算法及实现步骤。KBQA系统的目标是通过自然语言处理技术,从知识图谱中提取和生成精确的答案。系统的实现包括多个关键步骤:mention识别、实体链接及排序、属......
  • 使用Ollama本地离线体验SimpleRAG(手把手教程)
    Ollama介绍Ollama是一个开源项目,专注于开发和部署大语言模型,特别是像LLaMA这样的模型,用于生成高质量的文本和进行复杂的自然语言处理任务。Ollama的目标是让大语言模型的运行和使用变得更加容易和普及,而无需复杂的基础设施或深度的机器学习知识。GitHub地址:https://github.com/......
  • 拖拽神器:Pragmatic-drag-and-drop!
    前言在前端开发中,拖拽功能是一种常见的交互方式,它能够极大提升用户体验。今天,我们要介绍的是一个开源的前端拖拽组件—pragmatic-drag-and-drop,它以其轻量级、高性能和强大的兼容性,成为了前端开发者的新宠。什么是pragmatic-drag-and-drop?pragmatic-drag-and-drop是由A......
  • localStorage、sessionStorage、Cookie的区别以及特点
    文章目录一、localStorage、sessionStorage、Cookie是什么?二、各自特点以及之间的区别1.`window.localStorage`2.`sessionStorage`3.`Cookie`三、写一个html页面以用来测试这三者不同的区别四、开始测试测试`localStorage`:测试`sessionStorage`:测试`Cookie`:总......