聚焦!LlamaIndex 的 VectorStoreIndex 索引结构深度解析

向量数据库大模型NoSQL数据库

picture.image

一、向量存储索引简介

向量存储(Vector Stores)是检索增强生成(RAG)系统的关键组件,因此在使用 LlamaIndex 构建应用时,几乎都会直接或间接地使用它们。

向量存储接受一系列 Node 对象,并从中构建索引。

VectorStoreIndex 是 LlamaIndex 提供的一种索引结构,专注于:

  • 构建索引:将文档转换为嵌入向量,并存储在 VectorStore 中。

  • 查询检索:根据查询向量,在 VectorStore 中进行相似度搜索,返回相关文档。

二、将数据加载到索引中

1、基本用法

最简单的使用方式是加载一组文档,并使用 from_documents 方法构建索引:

  
from llama_index.core import VectorStoreIndex,SimpleDirectoryReader  
from llama_index.embeddings.huggingface import HuggingFaceEmbedding  
  
# 定义Embedding模型  
embed_model = HuggingFaceEmbedding(model_name=r"D:\Test\LLMTrain\testllm\llm\BAAI\bge-m3")  
# 读取一批文件  
documents=SimpleDirectoryReader(r"D:\Test\LLMTrain\day22_rag_data\data2").load_data()  
  
#从文件创建索引  
vector_store_index= VectorStoreIndex.from_documents(documents,embed_model=embed_model)  
  
print("vector_store_index:",vector_store_index)  
  

代码说明:

  • 如果在命令行中使用 from_documents,可以传入 show_progress=True 参数,以在构建索引时显示进度条。

  • 使用 from_documents 方法时,文档会被拆分成多个块,并解析为 Node 对象。这些 Node 是对文本字符串的轻量级抽象,能够跟踪元数据和关系。

  • 默认情况下,VectorStoreIndex 会将所有内容存储在内存中。有关如何使用持久化的向量存储,请参阅下文的“使用向量存储”部分。

  • 默认情况下,VectorStoreIndex 会以每 2048 个节点为一批生成并插入向量。如果内存受限(或内存充裕),可以通过传入 insert_batch_size=2048 参数来修改批量大小。这在将数据插入远程托管的向量数据库时尤其有用。

2、直接创建和管理节点

如果希望完全控制索引,可以手动创建和定义节点,并将它们直接传递给索引构造函数:

  
from llama_index.core import VectorStoreIndex  
from  llama_index.core.schema import  TextNode  
from llama_index.embeddings.huggingface import HuggingFaceEmbedding  
  
# 定义Embedding模型  
embed_model = HuggingFaceEmbedding(model_name=r"D:\Test\LLMTrain\testllm\llm\BAAI\bge-m3")  
  
node1=TextNode(id="第一章第一句",text="“咱们的村子叫做神牛村,听爷爷的爷爷说,三百年前从天上掉下了一头神牛,好像是镇压什么妖怪,救了咱们村子,所以咱们村子为了感激那头神牛,特地改了名字叫做神牛村。")  
node2=TextNode(id="第一章第二句",text="“所以咱们村子感恩神牛的恩情,再也不吃牛肉了,人人都以牛姓为尊,所以俺爹给俺取了名字叫做牛大!")  
  
nodes=[node1,node2]  
index=VectorStoreIndex(nodes,embed_model=embed_model)  
  
print(index)

处理文档更新

在直接管理索引时,您可能需要处理随时间变化的数据源。Index 类提供了插入、删除、更新和刷新操作。

若需要将这些文档数据保存到向量数据库,则需要创建存储上线文StorageContext。示例代码如下:

  
from llama_index.core import VectorStoreIndex,StorageContext  
from  llama_index.core.schema import  TextNode  
from llama_index.embeddings.huggingface import HuggingFaceEmbedding  
from llama_index.vector_stores.chroma import ChromaVectorStore  
import chromadb  
  
# 定义Embedding模型  
embed_model = HuggingFaceEmbedding(model_name=r"D:\Test\LLMTrain\testllm\llm\BAAI\bge-m3")  
  
node1=TextNode(id="第一章第一句",text="“咱们的村子叫做神牛村,听爷爷的爷爷说,三百年前从天上掉下了一头神牛,好像是镇压什么妖怪,救了咱们村子,所以咱们村子为了感激那头神牛,特地改了名字叫做神牛村。")  
node2=TextNode(id="第一章第二句",text="“所以咱们村子感恩神牛的恩情,再也不吃牛肉了,人人都以牛姓为尊,所以俺爹给俺取了名字叫做牛大!")  
  
nodes=[node1,node2]  
  
# 创建chroma数据库客户端,和传关键集合collection  
chroma_client = chromadb.PersistentClient(path=r"D:\Test\LLMTrain\day22_rag_data\chroma_db3")  
chroma_collection = chroma_client.get_or_create_collection("quickstart")  
  
# 确保存储上下文正确初始化  
storage_context = StorageContext.from_defaults(  
    vector_store=ChromaVectorStore(chroma_collection=chroma_collection)  
)  
  
index=VectorStoreIndex(nodes, storage_context=storage_context,embed_model=embed_model)  
  
print(index)  

3、存储向量索引

LlamaIndex 支持多种向量存储。您可以通过传入 StorageContext 来指定使用哪一种向量存储,在其中指定 vector_store 参数,比如使用 chroma;也可以直接传vector_store,这种不需要传StorageContext。这两种写法如下:

(1)直接传vector_store

这种是可以从向量数据库里读取数据。

  
# import  
from llama_index.core import VectorStoreIndex  
from llama_index.vector_stores.chroma import ChromaVectorStore  
from llama_index.embeddings.huggingface import HuggingFaceEmbedding  
import chromadb  
  
  
# 创建chroma数据库客户端,和传关键集合collection  
chroma_client = chromadb.PersistentClient(path=r"D:\Test\LLMTrain\day22_rag_data\chroma_db2")  
chroma_collection = chroma_client.get_or_create_collection("quickstart")  
  
# 定义Embedding模型  
embed_model = HuggingFaceEmbedding(model_name=r"D:\Test\LLMTrain\testllm\llm\BAAI\bge-m3")  
  
# set up ChromaVectorStore  
vector_store = ChromaVectorStore(chroma_collection=chroma_collection)  
  
# 从chromadb中创建索引  
index = VectorStoreIndex.from_vector_store(  
    vector_store, embed_model=embed_model  
)  
  
print(index)

(2)传StroageContext

这种是将文档向量化后保存到向量数据库里的操作。

  
# import  
from llama_index.core import VectorStoreIndex, StorageContext,SimpleDirectoryReader  
from llama_index.vector_stores.chroma import ChromaVectorStore  
from llama_index.embeddings.huggingface import HuggingFaceEmbedding  
import chromadb  
  
# 创建chroma数据库客户端,和传关键集合collection  
chroma_client = chromadb.PersistentClient(path=r"D:\Test\LLMTrain\day22_rag_data\chroma_db2")  
chroma_collection = chroma_client.get_or_create_collection("quickstart")  
  
# 定义Embedding模型  
embed_model = HuggingFaceEmbedding(model_name=r"D:\Test\LLMTrain\testllm\llm\BAAI\bge-m3")  
  
# 存储上下文  
storage_context= StorageContext.from_defaults(vector_store=ChromaVectorStore(chroma_collection=chroma_collection))  
  
 # 加载文档  
documents = SimpleDirectoryReader(r"D:\Test\LLMTrain\day22_rag_data\data2").load_data()  
  
# 从文档中创建索引,并且序列化到chroma数据库  
index = VectorStoreIndex.from_documents(  
    documents,storage_context=storage_context, embed_model=embed_model  
)  
  
print(index)

三、查询检索

VectorStoreIndex(以及其他索引/检索器)能够检索通用对象,包括:

  • 对节点的引用
  • 查询引擎
  • 检索器
  • 查询管道

如果检索到这些对象,它们将使用提供的查询自动运行。

  
from llama_index.llms.huggingface import HuggingFaceLLM  
  
# 查询数据  
  
# LLM 大模型  
llm = HuggingFaceLLM(  
        model_name=r"D:\Test\LLMTrain\testllm\llm\Qwen\Qwen2___5-3B-Instruct",  
        tokenizer_name=r"D:\Test\LLMTrain\testllm\llm\Qwen\Qwen2___5-3B-Instruct",  
        model_kwargs={  
            "trust_remote_code": True,  
        },  
        tokenizer_kwargs={"trust_remote_code": True},  
        generate_kwargs={"temperature": 0.3}  
    )  
  
  
# 构建查询引擎  
query_engine = index.as_query_engine(llm=llm)  
  
# 查询,需要设置LLM大模型  
response = query_engine.query("陈平安是谁?")  
print(response)

picture.image

LlamaIndex入门指南和RAG原理

RAG进阶:Embedding Models嵌入式模型原理和选型指南

RAG 落地必备的 1 个开源 AI 原生向量数据库 —Chroma

RAG落地实战之文本切分4种策略全解析

速看!最新版 Dify 连接 Ollama 与 vLLM 全攻略

一文搞懂!RAGFlow 入门教程与安装部署全流程

0
0
0
0
关于作者
关于作者

文章

0

获赞

0

收藏

0

相关资源
CV 技术在视频创作中的应用
本次演讲将介绍在拍摄、编辑等场景,我们如何利用 AI 技术赋能创作者;以及基于这些场景,字节跳动积累的领先技术能力。
相关产品
评论
未登录
看完啦,登录分享一下感受吧~
暂无评论