ARTICLE DETAIL

资讯详情

深耕郑州网站建设与运营推广的一线实战洞察。

LlamaIndex AgentSearchReader 指南:集成 SciPhi AgentSearch 搜索 API 与 RAG 检索

LlamaIndex AgentSearchReader 指南:集成 SciPhi AgentSearch 搜索 API 与 RAG 检索 LlamaIndex AgentSearchReader 指南集成 SciPhi AgentSearch 搜索 API 与 RAG 检索【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index导读本文围绕 LlamaIndex 仓库中AgentSearchReader读取器的实现与使用展开介绍如何将 AgentSearch由 SciPhi 托管的 Agent 搜索平台作为数据源接入 LlamaIndex 的文档加载链路。读完本文你将掌握AgentSearchReader的安装方式、初始化参数、load_data查询流程query / search_provider / llm_model 三个核心参数理解其返回的Document结构并能基于源码了解其与BaseReader抽象、SciPhi客户端的底层调用关系。AgentSearchReader 是什么AgentSearchReader是 LlamaIndex 官方集成中的一名数据读取器Reader位于仓库的llama-index-integrations/readers/llama-index-readers-agent-search集成包中其 API 参考文档定义在 docs/api_reference/api_reference/readers/agent_search.md。它解决的核心问题是在查询query阶段而不是索引阶段动态获取搜索结果并生成 RAG 响应。与传统的“先加载语料、再建索引、最后检索”的流程不同AgentSearchReader 将“搜索引擎查询 检索增强生成”封装为一步给定一个查询字符串它调用 AgentSearch 托管服务返回由 RAG 专用大模型基于实时搜索结果生成的回答文本以及相关的查询建议、搜索结果等元数据最终封装成 LlamaIndex 的Document对象返回。从项目结构看该集成包的核心文件包括base.pyAgentSearchReader类的主实现init.py导出AgentSearchReader符号README.md安装与使用说明pyproject.toml包元数据与依赖声明要求 Python 3.10依赖llama-index-core0.13.0,0.15。安装在仓库中该集成以独立 Python 包形式维护包名为llama-index-readers-agent-search。安装命令如下pip install llama-index-readers-agent-search安装后运行时还需要agent-searchPython 包SciPhi 官方 SDK。该依赖在包的 requirements.txt 中声明为agent-search不过它并未被写入pyproject.toml的硬依赖列表而是在AgentSearchReader.__init__中通过延迟导入lazy import的方式按需加载详见下文源码分析。因此若环境中缺少该包初始化读取器时会抛出明确的ImportError提示。初始化与参数说明AgentSearchReader的构造函数签名见 base.pydef __init__( self, api_base: Optional[str] None, api_key: Optional[str] None, ):参数类型默认值说明api_baseOptional[str]NoneAgentSearch 托管服务的 API 基础地址不传时使用 SciPhi 默认端点api_keyOptional[str]None访问 AgentSearch 服务的 API 密钥也可通过环境变量SCIPHI_API_KEY提供见 README.md构造函数内部做两件事依赖检查尝试import agent_search若失败则抛出ImportError提示信息为agent-search package not found, please run pip install agent-search创建客户端从agent_search包中导入SciPhi类并以SciPhi(api_baseapi_base, api_keyapi_key)实例化内部客户端self._client。从源码结构可以推断AgentSearchReader是对 SciPhi SDK 的一层轻量封装所有与远端服务的通信都由SciPhi客户端完成读取器只负责参数透传与返回结果的类型转换。查询数据load_dataload_data是读取器的核心方法签名如下见 base.pydef load_data( self, query: str, search_provider: str bing, llm_model: str SciPhi/Sensei-7B-V1, ) - List[Document]:三个关键参数参数类型默认值说明querystr必填查询字符串例如latest newssearch_providerstrbing搜索引擎提供方目前支持bing与agent-search后者指 AgentSearch 自有数据集/检索服务llm_modelstrSciPhi/Sensei-7B-V1用于生成 RAG 回答的模型标识属于面向搜索场景优化的 RAG 专用模型调用流程调用self._client.get_search_rag_response(queryquery, search_providersearch_provider, llm_modelllm_model)向 AgentSearch 服务发起一次“搜索 RAG”组合请求将返回的字典rag_response拆分为两部分response字段作为Document.text即生成的回答正文其余字段整体作为Document.metadata保留。返回值是包含单个Document的列表。这一点与BaseReader抽象保持一致——LlamaIndex 所有读取器的统一入口约定都是load_data(...) - List[Document]见 llama-index-core/llama_index/core/readers/base.py因此AgentSearchReader可以直接作为数据源喂给后续的索引、查询引擎等下游组件。返回的 Document 结构README 中给出了返回结构的示意见 README.md# text The latest news encompasses ... and its consequences [2]. # metadata {related_queries: [Details on the..., ...], search_results : [...]}即document.textRAG 模型基于实时搜索结果生成的回答正文回答中带有[2]这类引用角标指向metadata中的搜索结果document.metadata包含related_queries相关查询建议列表、search_results本次搜索命中的结果条目等补充信息可用于溯源、追问或后续处理。使用示例基础用法来自 base.py 的官方示例示例日期为 2024-01-08可作参考from llama_index.readers.agent_search import AgentSearchReader reader AgentSearchReader() document reader.load_data( querylatest news, search_providerbing, )[0] print(fDocument:\n{document})运行后会打印类似如下的输出Document: Doc ID: 67a57dfe-8bd6-4c69-af9d-683e76177119 Text: The latest news encompasses a wide array of topics, reflecting the dynamic and complex nature of the world today. Notable events include the conviction of a man for killing his ex-wifes new partner, highlighting the ongoing issue of domestic violence and its legal consequences [2]. In the realm of international relations, the release of Jeffrey...Doc ID为 LlamaIndex 自动生成的文档唯一标识Text即 RAG 模型生成的回答。显式配置 API Key若服务需要鉴权可以显式传入或通过环境变量设置import os # 方式一环境变量 os.environ[SCIPHI_API_KEY] your-api-key # 方式二构造参数 reader AgentSearchReader( api_basehttps://api.agent-search.ai, # 按实际服务地址填写 api_keyyour-api-key, )切换搜索提供方与 RAG 模型reader AgentSearchReader() # 使用 AgentSearch 自有数据集作为检索源 doc reader.load_data( querywhat is agent search?, search_provideragent-search, )[0] # 显式指定 RAG 模型 doc reader.load_data( querylatest AI papers, search_providerbing, llm_modelSciPhi/Sensei-7B-V1, )[0]接入 LlamaIndex 索引链路由于load_data返回List[Document]返回值可直接用于构建索引或作为检索器结果消费from llama_index.core import VectorStoreIndex from llama_index.readers.agent_search import AgentSearchReader reader AgentSearchReader() documents reader.load_data(querylatest news, search_providerbing) index VectorStoreIndex.from_documents(documents) query_engine index.as_query_engine() response query_engine.query(What are the latest developments?) print(response)源码级原理拆解与 BaseReader 的关系AgentSearchReader继承自llama_index.core.readers.base.BaseReader。在 base.py 中from llama_index.core.readers.base import BaseReader from llama_index.core.schema import Document class AgentSearchReader(BaseReader): AgentSearch reader.BaseReader是 LlamaIndex 所有读取器的抽象基类定义了解析文本文件、向文档写入元数据等通用能力并统一约定load_data(*args, **load_kwargs) - List[Document]的方法签名见 llama-index-core/llama_index/core/readers/base.py。继承它意味着AgentSearchReader天然兼容 LlamaIndex 的 Reader 生态可用于SimpleDirectoryReader组合、download_loader动态加载、索引构建等场景。延迟导入设计构造函数中的try: import agent_search延迟导入是一种典型的设计取舍把重依赖SciPhi SDK的加载推迟到真正实例化读取器时从而保证包本身安装轻量、导入llama_index.readers.agent_search模块时不产生副作用。若缺少该依赖用户在实例化时立即得到明确的中文可读报错而不是深层调用栈。与 SciPhi 客户端的协作AgentSearchReader内部持有self._clientagent_search.SciPhi实例并将load_data的三个参数原样透传给get_search_rag_response。从代码结构看读取器本身不含任何 HTTP 请求逻辑所有网络交互均由 SciPhi SDK 完成——这是一层“薄封装”设计职责边界清晰读取器负责参数校验与结果归一化dict → DocumentSDK 负责协议与传输。命令行动态加载入口LlamaIndex 的 CLI 映射表 llama-index-core/llama_index/core/command_line/mappings.json 中登记了AgentSearchReader: llama_index.readers.agent_search这意味着除了常规的import导入方式还可以通过 LlamaIndex 的download_loader机制按名称动态下载并加载该读取器即文档示例开头的download_loader(AgentSearch)用法无需手动管理包依赖。注意事项与适用边界依赖外部服务AgentSearchReader依赖 SciPhi 托管的 AgentSearch 在线服务需要网络可达搜索质量、可用性与响应速度由远端服务决定不在仓库代码控制范围内。鉴权若服务端要求鉴权需通过api_key参数或SCIPHI_API_KEY环境变量提供密钥未配置时可能无法访问。返回单文档load_data返回的是单个DocumentRAG 聚合后的回答而非逐条搜索结果。若需要原始搜索结果列表可从document.metadata[search_results]中取得。参数约束search_provider的可选值bing、agent-search与llm_model的可用模型列表由 AgentSearch 服务端定义具体以 AgentSearch 官方文档为准。版本要求该集成包要求 Python 3.10且依赖llama-index-core0.13.0,0.15见 pyproject.toml使用时需保证环境满足约束。总结AgentSearchReader为 LlamaIndex 提供了一条极简的“搜索即读取”通路一次load_data调用即可同时完成搜索引擎检索与 RAG 回答生成并把结果无缝封装为 LlamaIndex 的Document。无论是希望快速把实时搜索结果接入索引还是需要为 Agent 应用补充带引用的检索式回答都可以从llama-index-readers-agent-search包入手结合本文的参数说明与源码原理快速落地一个可运行的搜索增强管线。【免费下载链接】llama_indexLlamaIndex is the leading document agent and OCR platform项目地址: https://gitcode.com/GitHub_Trending/ll/llama_index创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表