【RAG】【vector_stores047】Lantern向量存储索引示例
案例目标本案例演示如何使用PostgreSQL数据库和Lantern扩展与LlamaIndex框架结合实现高效的向量搜索和混合搜索功能。主要目标包括展示如何创建基于Lantern的向量索引演示如何使用HNSW索引参数优化搜索性能展示如何实现混合搜索向量搜索全文搜索演示如何从现有向量存储创建索引展示如何配置文本搜索语言参数技术栈与核心依赖核心技术LlamaIndex: 用于构建文档索引和查询的框架PostgreSQL: 关系型数据库作为向量存储的基础Lantern: PostgreSQL的向量扩展提供向量搜索功能OpenAI: 用于生成文本嵌入向量核心依赖pip install llama-index-vector-stores-lanternpip install llama-index-embeddings-openaipip install psycopg2-binarypip install asyncpg环境配置在开始之前需要进行以下环境配置1. 安装必要的依赖包%pip install llama-index-vector-stores-lantern %pip install llama-index-embeddings-openai !pip install psycopg2-binary llama-index asyncpg2. 配置OpenAI API密钥import os os.environ[OPENAI_API_KEY] your_key openai.api_key your_key3. 配置嵌入模型from llama_index.embeddings.openai import OpenAIEmbedding from llama_index.core import Settings # 设置全局嵌入模型 Settings.embed_model OpenAIEmbedding(modeltext-embedding-3-small)4. 创建PostgreSQL数据库import psycopg2 connection_string postgresql://postgres:postgreslocalhost:5432 db_name postgres conn psycopg2.connect(connection_string) conn.autocommit True with conn.cursor() as c: c.execute(fDROP DATABASE IF EXISTS {db_name}) c.execute(fCREATE DATABASE {db_name})案例实现1. 导入必要的库from llama_index.core import SimpleDirectoryReader, StorageContext from llama_index.core import VectorStoreIndex from llama_index.vector_stores.lantern import LanternVectorStore import textwrap import openai from sqlalchemy import make_url2. 加载文档数据# 创建目录并下载数据 !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)3. 创建Lantern向量存储和索引url make_url(connection_string) vector_store LanternVectorStore.from_params( databasedb_name, hosturl.host, passwordurl.password, porturl.port, userurl.username, table_namepaul_graham_essay, embed_dim1536, # openai embedding dimension ) storage_context StorageContext.from_defaults(vector_storevector_store) index VectorStoreIndex.from_documents( documents, storage_contextstorage_context, show_progressTrue ) query_engine index.as_query_engine()4. 执行查询# 查询作者做了什么 response query_engine.query(What did the author do?) print(textwrap.fill(str(response), 100)) # 查询1980年代中期发生了什么 response query_engine.query(What happened in the mid 1980s?) print(textwrap.fill(str(response), 100))5. 从现有向量存储创建索引vector_store LanternVectorStore.from_params( databasedb_name, hosturl.host, passwordurl.password, porturl.port, userurl.username, table_namepaul_graham_essay, embed_dim1536, # openai embedding dimension m16, # HNSW M parameter ef_construction128, # HNSW ef construction parameter ef64, # HNSW ef search parameter ) index VectorStoreIndex.from_vector_store(vector_storevector_store) query_engine index.as_query_engine() response query_engine.query(What did the author do?) print(textwrap.fill(str(response), 100))6. 实现混合搜索# 创建支持混合搜索的向量存储 hybrid_vector_store LanternVectorStore.from_params( databasedb_name, hosturl.host, passwordurl.password, porturl.port, userurl.username, table_namepaul_graham_essay_hybrid_search, embed_dim1536, # openai embedding dimension hybrid_searchTrue, text_search_configenglish, ) storage_context StorageContext.from_defaults( vector_storehybrid_vector_store ) hybrid_index VectorStoreIndex.from_documents( documents, storage_contextstorage_context ) # 创建混合查询引擎 hybrid_query_engine hybrid_index.as_query_engine( vector_store_query_modehybrid, sparse_top_k2 ) hybrid_response hybrid_query_engine.query( Who does Paul Graham think of with the word schtick ) print(hybrid_response)案例效果通过本案例的实现可以达到以下效果查询效果示例查询What did the author do?结果返回关于Paul Graham职业生涯和创业经历的详细信息查询What happened in the mid 1980s?结果返回关于1980年代中期AI发展的相关信息混合搜索查询Who does Paul Graham think of with the word schtick结果通过向量搜索和全文搜索的组合返回更精确的结果技术效果高效的向量搜索和检索支持HNSW索引优化搜索性能支持混合搜索向量全文可以从现有向量存储创建索引支持多种文本搜索语言配置案例实现思路本案例的实现思路如下环境准备安装必要的依赖库包括LlamaIndex、Lantern向量存储和PostgreSQL连接器数据库准备创建PostgreSQL数据库配置连接参数数据准备下载并加载Paul Graham的散文作为示例文档向量存储初始化创建LanternVectorStore实例配置嵌入维度索引创建使用加载的文档创建向量索引查询执行执行查询并获取结果展示基本向量搜索功能索引复用展示如何从现有向量存储创建索引并配置HNSW参数混合搜索实现创建支持混合搜索的向量存储结合向量搜索和全文搜索关键技术点HNSW索引使用分层可导航小世界图索引提高搜索性能混合搜索结合向量搜索和全文搜索提高搜索准确性向量存储复用从现有向量存储创建索引避免重复构建文本搜索配置支持多语言文本搜索配置扩展建议基于本案例可以考虑以下扩展方向功能扩展实现更复杂的元数据过滤功能添加自定义重排序策略实现多语言混合搜索添加向量相似度阈值过滤实现增量更新索引功能应用场景扩展构建企业知识库检索系统实现文档相似性分析工具开发智能问答系统构建学术论文检索平台实现多模态搜索系统性能优化建议调整HNSW索引参数(m, ef_construction, ef)以平衡索引构建速度和查询性能优化向量维度和嵌入模型以提高检索精度考虑使用PostgreSQL分区表处理大规模数据调整混合搜索中的sparse_top_k参数以优化全文搜索结果总结本案例展示了如何使用LlamaIndex和Lantern向量存储构建高效的向量搜索系统。通过PostgreSQL和Lantern扩展我们可以利用关系型数据库的强大功能和向量搜索的高效性实现高性能的向量检索。案例中介绍了两种主要的搜索方式纯向量搜索和混合搜索。纯向量搜索适用于语义相似性查询而混合搜索结合了向量搜索和全文搜索的优势能够提供更准确和全面的搜索结果。通过调整HNSW索引参数我们可以根据具体应用场景优化搜索性能。Lantern作为PostgreSQL的向量扩展不仅提供了高效的向量搜索能力还保持了与PostgreSQL生态系统的兼容性使得开发者可以利用PostgreSQL的丰富功能如事务、并发控制和扩展性。总的来说LlamaIndex和Lantern的结合为构建高性能的向量搜索应用提供了一个强大而灵活的解决方案适用于各种需要高效向量搜索的应用场景。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2519728.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!