1. 案例目标本案例展示了如何使用Pinecone向量数据库的混合搜索功能结合稠密向量和稀疏向量实现更精确的文档检索。主要目标包括演示如何创建支持混合搜索的Pinecone索引展示如何配置PineconeVectorStore以启用稀疏向量计算演示如何使用默认稀疏嵌入模型进行混合搜索展示如何使用自定义稀疏嵌入模型FastEmbed提高搜索质量演示如何使用混合搜索模式查询索引2. 技术栈与核心依赖本案例使用以下技术栈和依赖LlamaIndex- 用于构建向量索引和查询引擎的核心框架Pinecone- 云原生向量数据库支持混合搜索OpenAI- 提供嵌入模型和语言模型FastEmbed- 提供高效的稀疏嵌入模型Transformers- 提供预训练模型和工具核心Python依赖包llama-index-vector-stores-pinecone llama-index-sparse-embeddings-fastembed transformers[torch] openai pinecone-client3. 环境配置要运行本案例需要进行以下环境配置安装依赖%pip install llama-index-vector-stores-pinecone transformers[torch]设置API密钥import os os.environ[PINECONE_API_KEY] your-pinecone-api-key os.environ[OPENAI_API_KEY] sk-your-openai-api-key api_key os.environ[PINECONE_API_KEY] pc Pinecone(api_keyapi_key)创建Pinecone索引from pinecone import Pinecone, ServerlessSpec # 删除现有索引如果需要 pc.delete_index(quickstart) # 创建新索引使用dotproduct度量以支持混合搜索 pc.create_index( namequickstart, dimension1536, # text-embedding-ada-002的维度 metricdotproduct, # 混合搜索需要使用dotproduct specServerlessSpec(cloudaws, regionus-east-1), ) # 获取索引引用 pinecone_index pc.Index(quickstart)准备数据下载Paul Graham的文章作为示例数据!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.txt4. 案例实现本案例的实现主要包括以下几个步骤4.1 加载文档并构建向量索引默认稀疏嵌入首先加载文档并构建向量索引启用稀疏向量计算from llama_index.core import VectorStoreIndex, SimpleDirectoryReader from llama_index.vector_stores.pinecone import PineconeVectorStore from llama_index.core import StorageContext # 加载文档 documents SimpleDirectoryReader(./data/paul_graham/).load_data() # 创建Pinecone向量存储启用稀疏向量计算 vector_store PineconeVectorStore( pinecone_indexpinecone_index, add_sparse_vectorTrue, # 启用稀疏向量计算 ) # 创建存储上下文 storage_context StorageContext.from_defaults(vector_storevector_store) # 构建向量索引 index VectorStoreIndex.from_documents( documents, storage_contextstorage_context )4.2 使用混合搜索模式查询创建查询引擎并使用混合搜索模式执行查询from IPython.display import Markdown, display # 创建查询引擎使用混合搜索模式 query_engine index.as_query_engine(vector_store_query_modehybrid) # 执行查询 response query_engine.query(What happened at Viaweb?) # 显示结果 display(Markdown(f{response}))4.3 使用自定义稀疏嵌入模型安装FastEmbed并使用自定义稀疏嵌入模型# 安装FastEmbed %pip install llama-index-sparse-embeddings-fastembed # 清空向量存储 vector_store.clear() # 创建自定义稀疏嵌入模型 from llama_index.sparse_embeddings.fastembed import FastEmbedSparseEmbedding sparse_embedding_model FastEmbedSparseEmbedding( model_nameprithivida/Splade_PP_en_v1 ) # 使用自定义稀疏嵌入模型创建向量存储 vector_store PineconeVectorStore( pinecone_indexpinecone_index, add_sparse_vectorTrue, sparse_embedding_modelsparse_embedding_model, )4.4 重建索引并查询使用自定义稀疏嵌入模型重建索引并执行查询# 重建索引 index VectorStoreIndex.from_documents( documents, storage_contextstorage_context ) # 等待索引上传完成 # 执行查询 response query_engine.query(What happened at Viaweb?) display(Markdown(f{response}))5. 案例效果本案例实现了以下效果混合搜索结合稠密向量和稀疏向量实现更精确的文档检索自定义稀疏嵌入支持使用自定义稀疏嵌入模型提高搜索质量高效检索利用Pinecone的高性能向量检索能力实现快速查询查询示例效果查询: What happened at Viaweb? 回答默认稀疏嵌入: Paul Graham started Viaweb because he needed money. As the company grew, he realized he didnt want to run a big company and decided to build a subset of the vision as an open source project. Eventually, Viaweb was bought by Yahoo in the summer of 1998, which was a huge relief for Paul Graham. 回答自定义稀疏嵌入: Paul Graham started Viaweb because he needed money. He recruited a team to work on building software and services, with a focus on creating an application builder and network infrastructure. However, halfway through the summer, Paul realized he didnt want to run a big company and decided to shift his focus to building a subset of the project as an open source project. This led to the development of a new dialect of Lisp called Arc. Ultimately, Viaweb was sold to Yahoo in the summer of 1998, providing relief to Paul Graham and allowing him to transition to a new phase in his life.6. 案例实现思路本案例的实现思路如下混合搜索原理混合搜索结合了稠密向量捕捉语义相似性和稀疏向量捕捉关键词匹配提高搜索准确性索引配置通过设置metricdotproduct和add_sparse_vectorTrue启用混合搜索功能稀疏嵌入模型默认使用简单的词频统计作为稀疏嵌入也可以使用更高级的模型如FastEmbed查询模式通过vector_store_query_modehybrid指定使用混合搜索模式模型切换支持动态切换稀疏嵌入模型通过clear()方法清空存储后重建索引提示混合搜索特别适合需要同时考虑语义相似性和关键词匹配的场景。例如在搜索技术文档时用户可能希望找到包含特定术语且在语义上相关的文档。7. 扩展建议基于本案例可以考虑以下扩展方向多语言支持实现多语言混合搜索支持不同语言的文档检索权重调整实现稠密向量和稀疏向量的权重动态调整根据查询类型优化搜索结果高级稀疏嵌入尝试更多高级稀疏嵌入模型如BM25、TF-IDF等查询分析添加查询分析功能自动判断是否需要使用混合搜索性能优化优化索引构建和查询性能支持大规模数据处理多模态支持扩展支持图像、音频等多模态数据的混合搜索实时更新实现索引的实时更新支持动态数据变化8. 总结本案例详细展示了如何使用Pinecone向量数据库的混合搜索功能结合稠密向量和稀疏向量实现更精确的文档检索。通过PineconeVectorStore的add_sparse_vector参数和vector_store_query_modehybrid配置我们可以轻松实现混合搜索功能。关键要点包括混合搜索结合了稠密向量和稀疏向量的优势提高搜索准确性PineconeVectorStore支持启用稀疏向量计算实现混合搜索支持自定义稀疏嵌入模型如FastEmbed提高搜索质量混合搜索特别适合需要同时考虑语义相似性和关键词匹配的场景通过clear()方法可以清空存储并重建索引实现模型切换Pinecone混合搜索为RAG应用提供了更强大的检索能力特别适合处理需要精确匹配关键词和语义理解的复杂查询场景。通过结合稠密向量和稀疏向量可以在保持语义理解能力的同时提高关键词匹配的准确性从而提供更全面的搜索结果。