【RAG】【vector_stores045】LanceDB向量存储示例
案例目标本案例演示如何使用LanceDB向量存储与LlamaIndex框架结合实现高效的向量搜索和检索功能。主要目标包括展示如何创建基于LanceDB的向量索引演示如何使用混合搜索功能结合向量搜索和全文搜索展示如何使用元数据过滤功能精确筛选搜索结果演示如何向现有索引添加新数据展示如何从现有表创建索引技术栈与核心依赖核心技术LlamaIndex: 用于构建文档索引和查询的框架LanceDB: 高性能向量数据库支持混合搜索OpenAI: 用于生成文本嵌入向量核心依赖pip install llama-indexpip install llama-index-vector-stores-lancedbpip install lancedb0.6.13pip install torch transformers tantivy环境配置在开始之前需要进行以下环境配置1. 安装必要的依赖包%pip install llama-index llama-index-vector-stores-lancedb %pip install lancedb0.6.13 %pip install -U torch transformers tantivygithttps://github.com/quickwit-oss/tantivy-py#164adc87e1a033117001cf70e38c82a53014d9852. 配置OpenAI API密钥import openai openai.api_key sk-3. 清理旧的向量存储可选! rm -rf ./lancedb案例实现1. 导入必要的库import logging import sys from llama_index.core import SimpleDirectoryReader, Document, StorageContext from llama_index.core import VectorStoreIndex from llama_index.vector_stores.lancedb import LanceDBVectorStore import textwrap2. 加载文档数据# 创建目录并下载数据 !mkdir -p data/paul_graham/ !wget https://raw.githubusercontent.com/run-llama/llama_index/main/docs/examples/data/paul_graham/paul_graham_essay.txt -O data/paul_graham/paul_graham_essay.txt # 加载文档 documents SimpleDirectoryReader(./data/paul_graham/).load_data() print(Document ID:, documents[0].doc_id, Document Hash:, documents[0].hash)3. 创建LanceDB向量存储和索引# 创建向量存储 vector_store LanceDBVectorStore( uri./lancedb, modeoverwrite, query_typehybrid ) storage_context StorageContext.from_defaults(vector_storevector_store) # 创建索引 index VectorStoreIndex.from_documents( documents, storage_contextstorage_context )4. 设置元数据过滤器from llama_index.core.vector_stores import ( MetadataFilters, FilterOperator, FilterCondition, MetadataFilter, ) from datetime import datetime query_filters MetadataFilters( filters[ MetadataFilter( keycreation_date, operatorFilterOperator.EQ, valuedatetime.now().strftime(%Y-%m-%d), ), MetadataFilter( keyfile_size, value75040, operatorFilterOperator.GT ), ], conditionFilterCondition.AND, )5. 配置混合搜索from lancedb.rerankers import ColbertReranker reranker ColbertReranker() vector_store._add_reranker(reranker) query_engine index.as_query_engine(filtersquery_filters) response query_engine.query(How much did Viaweb charge per month?)6. 使用Lance原生过滤lance_filter metadata.file_name paul_graham_essay.txt retriever index.as_retriever(vector_store_kwargs{where: lance_filter}) response retriever.retrieve(What did the author do growing up?)7. 添加新数据到索引# 从检索结果获取节点 nodes [node.node for node in response] # 创建新索引并插入节点 index VectorStoreIndex.from_documents( [Document(textThe sky is purple in Portland, Maine)], uri/tmp/new_dataset, ) index.insert_nodes(nodes)8. 从现有表创建索引vec_store LanceDBVectorStore.from_table(vector_store._table) index VectorStoreIndex.from_vector_store(vec_store)案例效果通过本案例的实现可以达到以下效果查询效果示例查询How much did Viaweb charge per month?结果Viaweb charged $100 a month for a small store and $300 a month for a big one.查询Where is the sky purple?结果Portland, Maine技术效果高效的向量搜索和检索支持混合搜索向量全文灵活的元数据过滤功能支持动态添加数据可以从现有表创建索引案例实现思路本案例的实现思路如下环境准备安装必要的依赖库包括LlamaIndex、LanceDB和相关组件数据准备下载并加载Paul Graham的散文作为示例文档向量存储初始化创建LanceDB向量存储实例配置混合搜索模式索引创建使用加载的文档创建向量索引查询配置设置元数据过滤器和混合搜索重排序器查询执行执行查询并获取结果展示不同查询方式的效果数据操作演示如何向现有索引添加新数据索引复用展示如何从现有表创建新的索引实例关键技术点混合搜索结合向量搜索和全文搜索提高搜索准确性元数据过滤使用元数据过滤器精确筛选搜索结果重排序使用ColbertReranker对搜索结果进行重排序动态更新支持向现有索引动态添加新数据扩展建议基于本案例可以考虑以下扩展方向功能扩展实现更复杂的元数据过滤逻辑集成其他重排序器如CohereReranker添加文档分块策略优化实现增量更新索引功能添加多语言支持应用场景扩展构建企业知识库检索系统实现多模态搜索文本图像开发智能问答系统构建推荐系统实现文档相似性分析性能优化建议调整LanceDB的nprobes参数以平衡搜索速度和准确性使用refine_factor参数优化搜索结果考虑使用LanceDB云服务以提高可扩展性优化文档分块大小以平衡检索精度和性能总结本案例展示了如何使用LlamaIndex和LanceDB构建高效的向量搜索和检索系统。通过LanceDB的混合搜索功能我们可以结合向量搜索和全文搜索的优势获得更准确的搜索结果。同时LanceDB的元数据过滤功能允许我们精确筛选搜索结果提高检索的相关性。本案例还演示了如何动态添加数据和从现有表创建索引这些功能使得LanceDB成为一个灵活且强大的向量存储解决方案。通过调整各种参数如nprobes和refine_factor我们可以根据具体应用场景优化搜索性能。总的来说LlamaIndex和LanceDB的结合为构建高性能的向量搜索应用提供了一个强大而灵活的框架适用于各种应用场景从简单的文档检索到复杂的企业知识库系统。