尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

基于 Fleet Context 与 Pinecone 为 LlamaIndex 构建混合检索(dense + sparse)引擎

基于 Fleet Context 与 Pinecone 为 LlamaIndex 构建混合检索(dense + sparse)引擎 基于 Fleet Context 与 Pinecone 为 LlamaIndex 构建混合检索dense sparse引擎【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index本文以 Fleet Context 官方集成指南为主体完整演示如何下载 LlamaIndex 全量文档的向量化数据约 1.2 万 chunk、约 100MB 内容并将其写入 Pinecone最终基于 LlamaIndex 的PineconeVectorStore搭建支持稠密向量dense与稀疏向量sparse的混合检索查询引擎。读完本文你将掌握fleet-context的用法、Pinecone 混合索引的建库要点、批量 upsert 的工程技巧以及如何在 LlamaIndex 中一键切换hybrid查询模式。技术背景为什么用 Fleet Context 预生成的 EmbeddingsFleet Contextfleet-context包为开源社区提供了面向 1220 多个知名开源库的预计算 Embeddings 下载能力。它内部维护了一条完整的 Embeddings 流水线与自行对文档做load → split → embed相比Fleet 的流水线保留了大量对检索与生成至关重要的信息包括页面内位置position on page可用于后续重排序re-rankingChunk 类型类class、函数function、属性attribute等代码结构类型标注父级章节parent section保留文档层级上下文。这些元数据与文本一起被编码进 Embeddings 数据集使得下游检索器不仅能命中长得像的文本还能理解代码文档的结构语义。这正是本文下载现成 Embeddings而非本地重新生成的核心动机。前置准备首先安装依赖!pip install llama-index !pip install --upgrade fleet-context然后配置 OpenAI API Key。Fleet Context 生成的稠密向量来自 OpenAI 的text-embedding-ada-002模型维度为 1536import os import openai os.environ[OPENAI_API_KEY] sk-... # add your API key here! openai.api_key os.environ[OPENAI_API_KEY]说明当前仓库中llama-index-vector-stores-pinecone集成包的官方文档示例见 base.py 的 docstring同样基于 1536 维稠密向量与dotproduct度量与本文流程完全对应。从 Fleet Context 下载 LlamaIndex 文档 Embeddings调用download_embeddings并传入库名即可from context import download_embeddings df download_embeddings(llamaindex)下载过程会显示进度条示例中约 83.7M速度约 27.4MiB/s100%|██████████| 83.7M/83.7M [00:0300:00, 27.4MiB/s] id \ 0 e268e2a1-9193-4e7b-bb9b-7a4cb88fc735 1 e495514b-1378-4696-aaf9-44af948de1a1 2 e804f616-7db0-4455-9a06-49dd275f3139 3 eb85c854-78f1-4116-ae08-53b2a2a9fa41 4 edfc116e-cf58-4118-bad4-c4bc0ca1495e返回的 DataFrame 每一行对应一个文档 Chunk包含id、values稠密向量、metadata、sparse_values稀疏向量等字段。可以通过下标查看具体某条记录的元数据与文本# Show some examples of the metadata df[metadata][0] display(Markdown(f{df[metadata][8000][text]}))输出示例第 8000 条记录展示的是某个类的 API 文档片段classmethod from_dict(data: Dict[str, Any], kwargs: Any) → Self classmethod from_json(data_str: str, kwargs: Any) → Self classmethod from_orm(obj: Any) → Model json(, include: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] None, exclude: Optional[Union[AbstractSetIntStr, MappingIntStrAny]] None, by_alias: bool False, skip_defaults: Optional[bool] None, exclude_unset: bool False, exclude_defaults: bool False, exclude_none: bool False, encoder: Optional[Callable[[Any], Any]] None, models_as_dict: bool True*, dumps_kwargs: Any) → unicode Generate a JSON representation of the model, include and exclude arguments as per dict().可以看到 Fleet 的 Chunk 保留了类方法签名、参数说明等结构化文档信息——这正是其 Embeddings 流水线的价值所在。创建 Pinecone 混合搜索索引Pinecone 支持在同一个索引中同时存储稠密向量与稀疏向量从而支撑混合检索。混合检索要求使用dotproduct 相似度而非 cosine因此在建索引时必须指定metricdotproduct。先配置日志与客户端import logging import sys logging.basicConfig(streamsys.stdout, levellogging.INFO) logging.getLogger().handlers [] logging.getLogger().addHandler(logging.StreamHandler(streamsys.stdout))import pinecone api_key ... # Add your Pinecone API key here pinecone.init( api_keyapi_key, environmentus-east-1-aws ) # Add your db region here创建索引维度必须与text-embedding-ada-002对齐即 1536# Fleet Context uses the text-embedding-ada-002 model from OpenAI with 1536 dimensions. # NOTE: Pinecone requires dotproduct similarity for hybrid search pinecone.create_index( quickstart-fleet-context, dimension1536, metricdotproduct, pod_typep1, ) pinecone.describe_index( quickstart-fleet-context ) # Make sure you create an index in pinecone版本提示上例使用的是 Pinecone 经典客户端 APIpinecone.init/pinecone.create_index。当前仓库中PineconeVectorStore的实现已适配新版客户端官方示例使用Pinecone(api_key...).create_index(...)并配合ServerlessSpec(cloudaws, regionus-west-2)创建 Serverless 索引详见 base.py 的 docstring。两种方式二选一即可关键在于dimension1536与metricdotproduct这两个混合检索的硬性要求。在 LlamaIndex 中接入 Pinecone 向量存储将 Pinecone 索引包装成 LlamaIndex 的PineconeVectorStore其中add_sparse_vectorTrue是关键开关——它让 LlamaIndex 在写入与查询时同时处理稠密和稀疏两个通道from llama_index.vector_stores.pinecone import PineconeVectorStore pinecone_index pinecone.Index(quickstart-fleet-context) vector_store PineconeVectorStore(pinecone_index, add_sparse_vectorTrue)从源码看该开关背后有完整的实现支撑当add_sparse_vectorTrue时PineconeVectorStore.__init__会自动实例化一个稀疏 Embedding 模型见 base.py默认的稀疏模型DefaultPineconeSparseEmbedding使用BertTokenizerFastbert-base-uncased做分词再以词频term frequency构造{token_id: 频率}形式的稀疏向量实现细节见 utils.py该开关也允许你通过tokenizer或sparse_embedding_model参数自定义稀疏向量生成方式base.py。批量 Upsert 向量到 PineconePinecone 官方推荐每次 upsert 100 条向量。下面用生成器将 DataFrame 逐行转换为 Pinecone 所需的(id, values, metadata, sparse_values)结构并按 100 条一批写入import random import itertools def chunks(iterable, batch_size100): A helper function to break an iterable into chunks of size batch_size. it iter(iterable) chunk tuple(itertools.islice(it, batch_size)) while chunk: yield chunk chunk tuple(itertools.islice(it, batch_size)) # generator that generates many (id, vector, metadata, sparse_values) pairs data_generator map( lambda row: { id: row[1][id], values: row[1][values], metadata: row[1][metadata], sparse_values: row[1][sparse_values], }, df.iterrows(), ) # Upsert data with 1000 vectors per upsert request for ids_vectors_chunk in chunks(data_generator, batch_size100): print(fUpserting {len(ids_vectors_chunk)} vectors...) pinecone_index.upsert(vectorsids_vectors_chunk)补充说明PineconeVectorStore内部同样以批处理方式工作其DEFAULT_BATCH_SIZE 100见 base.py并在add()时按此批量执行upsert这一默认值与本指南的批量写入策略一致。基于向量存储构建 LlamaIndex 索引数据写入完成后直接用已有的vector_store构建检索索引无需重新加载文档from llama_index.core import VectorStoreIndex from IPython.display import Markdown, displayindex VectorStoreIndex.from_vector_store(vector_storevector_store)from_vector_store是 LlamaIndex 面向外部向量库已就绪场景的标准入口它不再关心文档与 Embedding 的产生过程只把向量库抽象为可查询的数据源。以 Hybrid 模式查询索引这是整个流程的收官一步。将查询引擎的vector_store_query_mode设为hybrid即可让 LlamaIndex 同时利用稠密向量语义匹配与稀疏向量关键词匹配并取两者综合结果query_engine index.as_query_engine( vector_store_query_modehybrid, similarity_top_k8 ) response query_engine.query(How do I use llama_index SimpleDirectoryReader)display(Markdown(fb{response}/b))输出示例bTo use the SimpleDirectoryReader in llama_index, you need to import it from the llama_index library. Once imported, you can create an instance of the SimpleDirectoryReader class by providing the directory path as an argument. Then, you can use the load_data() method on the SimpleDirectoryReader instance to load the documents from the specified directory./bHybrid 模式的底层机制vector_store_query_mode映射到核心层的VectorStoreQueryMode枚举见 types.py其中与本文相关的取值包括模式含义default仅稠密向量检索sparse仅稀疏向量关键词检索hybrid稠密 稀疏混合检索当模式为sparse或hybrid时PineconeVectorStore.query()会先用默认稀疏模型对查询串生成稀疏向量再连同稠密向量一起提交给 Pinecone见 base.py。这里有两个值得注意的细节alpha权重参数核心层的VectorStoreQuery.alpha注释明确为 0 for bm25稀疏, 1 for vector search稠密见 types.py。在 Pinecone 实现中若指定alpha稠密向量按alpha缩放、稀疏向量按1 - alpha缩放从而调节两种检索的贡献比例base.py查询串必填sparse与hybrid模式要求必须提供query_str否则会抛出ValueErrorbase.py。元数据回传与结果组装查询返回后PineconeVectorStore会把 Pinecone 每条 match 中的 metadata 反序列化回 LlamaIndex 的TextNode优先走新版metadata_dict_to_node失败时回退到legacy_metadata_dict_to_node兼容旧数据并组装出nodes、similarities、ids三要素的VectorStoreQueryResultbase.py。这也意味着 Fleet Context 预置在 metadata 里的位置、类型、章节等信息在检索链路中全程保留可继续用于下游的重排序或过滤。集成包的验证与扩展仓库为PineconeVectorStore提供了完整的集成测试见 test_vector_stores_pinecone.py覆盖了向量写入test_add_upserts_vectors_by_keyword、基于 mock 的索引行为校验等场景可作为你自行验证混合检索链路的参考模板。此外PineconeVectorStore还支持以下能力可作为本文方案的进阶扩展命名空间隔离通过namespace参数在同一个 Pinecone 索引中隔离不同数据集批量/过滤删除delete_nodes支持按node_ids或元数据过滤器删除二者互斥base.py元数据过滤标准MetadataFilters会被自动转换为 Pinecone 的$eq/$ne/$gt/$lt/$in等过滤语法base.py可在查询时叠加filter约束。小结本文完整走通了Fleet Context 下载 Embeddings → Pinecone 混合索引建库 → 批量 upsert → LlamaIndex hybrid 查询的端到端链路。核心要点可归纳为四点直接复用 Fleet Context 的预计算 Embeddings省去本地文档解析与向量化流水线同时获得位置、类型、章节等丰富元数据混合检索要求metricdotproduct与 1536 维建索引时不可省略add_sparse_vectorTrue是让PineconeVectorStore启用稀疏通道的关键参数查询时指定vector_store_query_modehybrid并按需通过alpha调节稠密/稀疏检索权重。这套方案不仅适用于 LlamaIndex 自身文档任何 Fleet Context 支持的 1220 余个开源库都可复用同一套代码流程快速搭建语义 关键词双通道的混合检索问答系统。【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表