【RAG】【vector_stores046】Lantern向量存储自动检索示例

张开发
2026/4/14 20:52:37 15 分钟阅读

分享文章

【RAG】【vector_stores046】Lantern向量存储自动检索示例
案例目标本案例演示如何使用Lantern向量存储与LlamaIndex框架结合实现自动检索功能。主要目标包括展示如何使用VectorIndexAutoRetriever实现智能元数据过滤演示如何让LLM根据自然语言查询自动推断元数据过滤器展示如何结合语义搜索和元数据过滤进行精确检索演示如何定义向量存储的元数据信息结构展示如何使用Lantern向量数据库构建高效的检索系统技术栈与核心依赖核心技术LlamaIndex: 用于构建文档索引和查询的框架Lantern: 基于PostgreSQL的向量扩展支持高效向量搜索OpenAI: 用于生成文本嵌入向量和推断查询PostgreSQL: 作为底层数据库存储向量数据核心依赖pip install llama-index-vector-stores-lanternpip install llama-index psycopg2-binary asyncpgpip install openai环境配置在开始之前需要进行以下环境配置1. 安装必要的依赖包%pip install llama-index-vector-stores-lantern !pip install llama-index psycopg2-binary asyncpg2. 配置OpenAI API密钥import os os.environ[OPENAI_API_KEY] your-api-key import openai openai.api_key os.environ[OPENAI_API_KEY]3. 配置PostgreSQL连接import psycopg2 from sqlalchemy import make_url connection_string postgresql://postgres:postgreslocalhost:5432 url make_url(connection_string) db_name postgres conn psycopg2.connect(connection_string) conn.autocommit True案例实现1. 导入必要的库import logging import sys from llama_index.core import VectorStoreIndex, StorageContext from llama_index.vector_stores.lantern import LanternVectorStore from llama_index.core.schema import TextNode from llama_index.core.retrievers import VectorIndexAutoRetriever from llama_index.core.vector_stores import MetadataInfo, VectorStoreInfo2. 创建示例数据节点nodes [ TextNode( text( Michael Jordan is a retired professional basketball player, widely regarded as one of the greatest basketball players of all time. ), metadata{ category: Sports, country: United States, }, ), TextNode( text( Angelina Jolie is an American actress, filmmaker, and humanitarian. She has received numerous awards for her acting and is known for her philanthropic work. ), metadata{ category: Entertainment, country: United States, }, ), TextNode( text( Elon Musk is a business magnate, industrial designer, and engineer. He is the founder, CEO, and lead designer of SpaceX, Tesla, Inc., Neuralink, and The Boring Company. ), metadata{ category: Business, country: United States, }, ), TextNode( text( Rihanna is a Barbadian singer, actress, and businesswoman. She has achieved significant success in the music industry and is known for her versatile musical style. ), metadata{ category: Music, country: Barbados, }, ), TextNode( text( Cristiano Ronaldo is a Portuguese professional footballer who is considered one of the greatest football players of all time. He has won numerous awards and set multiple records during his career. ), metadata{ category: Sports, country: Portugal, }, ), ]3. 创建Lantern向量存储vector_store LanternVectorStore.from_params( databasedb_name, hosturl.host, passwordurl.password, porturl.port, userurl.username, table_namefamous_people, embed_dim1536, # openai embedding dimension m16, # HNSW M parameter ef_construction128, # HNSW ef construction parameter ef64, # HNSW ef search parameter ) storage_context StorageContext.from_defaults(vector_storevector_store) index VectorStoreIndex(nodes, storage_contextstorage_context)4. 定义向量存储信息vector_store_info VectorStoreInfo( content_infobrief biography of celebrities, metadata_info[ MetadataInfo( namecategory, typestr, description( Category of the celebrity, one of [Sports, Entertainment, Business, Music] ), ), MetadataInfo( namecountry, typestr, description( Country of the celebrity, one of [United States, Barbados, Portugal] ), ), ], )5. 创建自动检索器retriever VectorIndexAutoRetriever( index, vector_store_infovector_store_info )6. 执行自动检索查询# 查询美国名人 retriever.retrieve(Tell me about two celebrities from United States)案例效果通过本案例的实现可以达到以下效果自动检索效果查询Tell me about two celebrities from United States自动推断的过滤器country United States结果返回Michael Jordan、Angelina Jolie和Elon Musk的传记信息查询Tell me about sports celebrities自动推断的过滤器category Sports结果返回Michael Jordan和Cristiano Ronaldo的传记信息技术效果LLM自动根据自然语言查询推断元数据过滤器结合语义搜索和元数据过滤进行精确检索支持多条件组合过滤提高检索相关性和准确性减少手动配置过滤器的需求案例实现思路本案例的实现思路如下环境准备安装必要的依赖库包括LlamaIndex、Lantern向量存储和PostgreSQL连接器数据库连接配置PostgreSQL连接参数建立与Lantern扩展的连接数据准备创建包含文本和元数据的节点数据用于演示自动检索功能向量存储初始化创建LanternVectorStore实例配置HNSW索引参数索引创建使用节点数据和存储上下文创建向量索引元数据信息定义定义向量存储的内容信息和元数据过滤器信息自动检索器创建使用VectorIndexAutoRetriever创建自动检索器查询执行执行自然语言查询让LLM自动推断元数据过滤器并检索相关内容关键技术点自动检索LLM根据自然语言查询自动推断元数据过滤器元数据信息结构通过VectorStoreInfo定义向量存储的元数据结构HNSW索引使用Lantern的HNSW索引实现高效的向量搜索组合过滤支持语义搜索和元数据过滤的组合使用扩展建议基于本案例可以考虑以下扩展方向功能扩展实现更复杂的元数据过滤逻辑添加时间范围过滤功能支持多语言查询和检索实现自定义检索排序策略添加检索结果解释功能应用场景扩展构建企业知识库智能检索系统实现电商产品智能搜索开发学术论文检索系统构建法律文档检索系统实现医疗文献智能检索性能优化建议调整HNSW索引参数(m, ef_construction, ef)以平衡索引构建速度和查询性能优化向量维度和嵌入模型以提高检索精度考虑使用PostgreSQL分区表处理大规模数据实现查询缓存机制提高重复查询性能总结本案例展示了如何使用LlamaIndex和Lantern向量存储实现自动检索功能。通过VectorIndexAutoRetriever系统能够根据自然语言查询自动推断元数据过滤器从而实现更精确的检索结果。与传统的固定过滤器相比自动检索功能具有更高的灵活性和智能性。用户无需手动指定过滤条件只需用自然语言描述查询需求系统就能自动理解并应用适当的过滤条件。Lantern作为基于PostgreSQL的向量扩展提供了高性能的向量搜索能力同时保持了与PostgreSQL生态系统的兼容性。这使得开发者可以利用PostgreSQL的强大功能如事务、并发控制和扩展性同时享受高效的向量搜索体验。总的来说LlamaIndex和Lantern的结合为构建智能检索系统提供了一个强大而灵活的解决方案适用于各种需要精确检索的应用场景。

更多文章