用 Higress AI Search 插件为 DeepSeek 实现联网搜索:完整配置指南与源码原理

用 Higress AI Search 插件为 DeepSeek 实现联网搜索:完整配置指南与源码原理 用 Higress AI Search 插件为 DeepSeek 实现联网搜索完整配置指南与源码原理【免费下载链接】higress AI Gateway | AI Native API Gateway项目地址: https://gitcode.com/GitHub_Trending/hi/higress本篇技术指南以 Higress 开源仓库中的 ai-search 插件教程 为核心讲解如何基于 Higress 的ai-searchWasm 插件为 DeepSeek 等大模型补齐联网搜索能力通过 Google / Bing / 夸克 / Arxiv 等搜索引擎的实时结果增强回答的准确性与时效性。读完本文你将掌握从配置 DeepSeek API Key、创建服务来源、部署插件到深度调优的完整 Step-by-Step 流程并能从源码层面理解搜索重写、Prompt 注入与引用插入的底层机制。为什么需要给 DeepSeek 加联网搜索大模型的知识存在截止时间面对时效性强的问题如最新新闻、实时价格、新发布论文时回答往往滞后甚至错误。Higress 提供的ai-search插件解决了这一问题插件在请求转发到 LLM 供应商之前拦截请求调用搜索引擎获取实时结果将结果注入 Prompt 模板再交给 DeepSeek 生成带引用的回答。从 插件源码 可以看到插件注册了请求头、请求体、响应头、响应体以及流式响应等完整的处理回调覆盖了搜索触发、Prompt 改写、引用插入的完整链路。插件当前版本为 2.0.2见 VERSION。Step 0配置 DeepSeek 的 API Key安装 Higress 时如果没有填写 DeepSeek 的 API Key可在 Higress 控制台进行补充配置。这是后续搜索重写功能如果启用调用 LLM 服务的基础凭据。Step 1配置搜索引擎 API 域名ai-search 插件的所有搜索请求都经由 Higress 网关转发到后端搜索引擎因此需要在 Higress 控制台通过创建服务来源Service Source的方式配置各搜索引擎的域名。各引擎对应的 API 域名如下搜索引擎API 域名是否需要 API KeyGooglecustomsearch.googleapis.com是Bingapi.bing.microsoft.com是夸克Quarkcloud-iqs.aliyuncs.com是阿里云 IQSArxivexport.arxiv.org否免费配置服务来源时为每个域名创建一个服务来源例如命名为google.dns、bing.dns、quark.dns、arxiv.dns后续插件配置中的serviceName即引用这些服务来源名称格式为来源名.dns。API Key 的申请夸克搜索需要一个阿里云账号在阿里云控制台搜索 IQS智能开放搜索进入 IQS 控制台生成 API Key。从 quark 引擎实现 可以看到夸克搜索请求通过X-API-Key请求头发送密钥并遵循阿里云ACS3-HMAC-SHA256签名规范包含x-acs-action、x-acs-version、x-acs-content-sha256等签名头。Google / Bing分别通过 Google Custom Search JSON API 和 Azure AI Search 平台申请网上资料较多此处不再赘述。Arxiv免费开放无需 API Key但可通过arxivCategory指定论文类别如cs.AI、cs.CL缩小搜索范围。Step 2配置 AI Search 插件2.1 插件部署方式2.1.0 版本之前的 Higress需要通过自定义插件的方式导入 AI Search 插件。注意插件 OCI 镜像地址填写higress-registry.cn-hangzhou.cr.aliyuncs.com/plugins/ai-search:latestlatesttag 可以确保使用最新版本若希望使用稳定版本将 tag 改为1.0.0即可。执行阶段选择默认阶段执行优先级填写大于 100 的任意值。这样保证 AI Search 插件在请求转发到 LLM 供应商之前执行先完成对 Prompt 的修改。插件源码中声明的默认优先级为460见 README.md 运行属性部分同样属于默认阶段。2.2 完整插件配置示例添加完插件后进行相应配置。以下配置同时启用了夸克、Google、Bing、Arxiv 四个搜索引擎并开启了搜索重写searchRewrite以使用 DeepSeek 优化搜索查询needReference: true # 为 true 时会在结果中附带网页引用信息 promptTemplate: | # 可以不用配置模版使用内置的也可以 # The following content is based on search results from the user-submitted query: {search_results} In the search results I provide, each result is formatted as [webpage X begin]...[webpage X end], where X represents the index number of each article. Please cite the context at the end of the sentences where appropriate. Use a format of citation number in the answer for corresponding parts. If a sentence is derived from multiple contexts, list all relevant citation numbers, such as [3][5], and ensure not to cluster the citations at the end; instead, list them in the corresponding parts of the answer. When responding, please pay attention to the following: - Todays date in Beijing time is: {cur_date}. - Not all content from the search results is closely related to the users question. You need to discern and filter the search results based on the question. - For listing-type questions (e.g., listing all flight information), try to keep the answer to within 10 points and inform the user that they can check the search source for complete information. Prioritize providing the most comprehensive and relevant items; do not volunteer information missing from the search results unless necessary. - For creative questions (e.g., writing a paper), be sure to cite relevant references in the body paragraphs, such as [3][5], rather than only at the end of the article. You need to interpret and summarize the users topic requirements, choose the appropriate format, fully utilize search results, extract crucial information, and generate answers that meet user requirements, with deep thought, creativity, and professionalism. The length of your creation should be extended as much as possible, hypothesize the users intent for each point, providing as many angles as possible, ensuring substantial information, and detailed discussion. - If the response is lengthy, try to structure the summary into paragraphs. If responding with points, try to keep it within 5 points and consolidate related content. - For objective QA, if the answer is very short, you can appropriately add one or two related sentences to enrich the content. - You need to choose a suitable and aesthetically pleasing response format based on the users requirements and answer content to ensure high readability. - Your answers should synthesize multiple relevant web pages to respond and should not repeatedly quote a single web page. - Unless the user requests otherwise, respond in the same language the question was asked. # The users message is: {question} searchFrom: # 下面是配置一个搜索引擎选择列表可以仅配置你需要的引擎不用都配上 - type: quark apiKey: your-quark-api-key # 需要修改成你的 key serviceName: quark.dns servicePort: 443 - type: google apiKey: your-google-api-key # 需要修改成你的 key cx: your-search-engine-id # 需要修改成你的 engine id serviceName: google.dns servicePort: 443 - type: bing apiKey: bing-key # 需要修改成你的 key serviceName: bing.dns servicePort: 443 - type: arxiv serviceName: arxiv.dns servicePort: 443 searchRewrite: llmApiKey: your-deepseek-api-key # 需要修改成你的 key llmModelName: deepseek-chat llmServiceName: llm-deepseek.internal.dns llmServicePort: 443 llmUrl: https://api.deepseek.com/chat/completions2.3 配置字段详解以下字段表来自 插件 README是对插件能力的权威说明名称数据类型填写要求默认值描述defaultEnablebool选填true插件功能默认是否开启。设置为 false 时仅当请求中包含web_search_options字段时才启用插件功能needReferencebool选填false是否在回答中添加引用来源referenceFormatstring选填**References:**\n%s引用内容格式必须包含%s占位符referenceLocationstring选填head引用位置head 在回答开头tail 在回答结尾defaultLangstring选填-默认搜索语言代码如 zh-CN/en-USpromptTemplatestring选填内置模板提示模板必须包含{search_results}和{question}占位符searchFromarray of object必填-搜索引擎配置列表至少配置一个引擎searchRewriteobject选填-搜索重写配置用于使用 LLM 服务优化搜索查询搜索引擎通用配置名称数据类型填写要求默认值描述typestring必填-引擎类型google/bing/arxiv/elasticsearch/quarkserviceNamestring必填-后端服务名称servicePortnumber必填-后端服务端口apiKeystring必填-搜索引擎 API 密钥 / Aliyun AccessKeycountnumber选填10单次搜索返回结果数量startnumber选填0搜索结果偏移量从第 start1 条结果开始返回timeoutMillisecondnumber选填5000API 调用超时时间毫秒optionArgsmap选填-搜索引擎特定参数key-value 格式各引擎特定配置Googlecx必填——Google 自定义搜索引擎 ID用于指定搜索范围。从 google.go 可以看到请求会拼出customsearch.googleapis.com/customsearch/v1?cx...q...num...key...start...的 URL并校验count 10且start count 100。ArxivarxivCategory选填——搜索的论文类别如 cs.AI、cs.CL 等请求通过search_query...max_results...start...调用export.arxiv.org/api/query响应为 Atom XML 格式插件用 XML 解析提取标题、摘要、作者与发布时间见 arxiv.go。夸克contentMode选填默认 summary——summary 使用摘要snippetfull 使用正文优先markdownText为空则用mainText。Elasticsearch对接私有知识库index必填、contentField必填、semanticTextField必填以及linkField、titleField配置needReference时需要、username、password选填。混合搜索使用的 Reciprocal Rank Fusion (RRF) 查询要求 Elasticsearch 版本在 8.8 及以上文档向量化依赖 Elasticsearch 内置 Embedding 模型该功能需要企业版 License 或 30 天 Trial License。2.4 搜索重写searchRewrite说明搜索重写功能使用 LLM 服务对用户原始查询进行分析和优化具备四项能力判断是否需要搜索识别用户问题是否需要查询搜索引擎如果不需要直接跳过搜索增强逻辑关键词优化将用户自然语言查询转换为更适合搜索引擎的关键词组合Arxiv 论文类别识别自动识别相关论文类别并添加类别限定私有知识库查询拆分将长查询拆分成多个精准的关键词组合。强烈建议在使用 Arxiv 或 Elasticsearch 引擎时启用此功能。搜索重写配置字段如下名称数据类型填写要求默认值描述llmServiceNamestring必填-LLM 服务名称llmServicePortnumber必填-LLM 服务端口llmApiKeystring选填-LLM 服务 API 密钥llmUrlstring必填-LLM 服务 API 地址llmModelNamestring必填-LLM 模型名称timeoutMillisecondnumber选填30000API 调用超时时间毫秒maxCountnumber选填3搜索重写生成的最大查询次数2.5 进阶配置示例多搜索引擎 自定义 Prompt例如用不同的 Googlecx分别搜索 GitHub 和 NewsdefaultLang: en-US promptTemplate: | # Search Results: {search_results} # Please answer this question: {question} searchFrom: - type: google apiKey: google-key cx: github-search-id # 专门搜索GitHub内容的搜索引擎ID serviceName: google-svc.dns servicePort: 443 - type: google apiKey: google-key cx: news-search-id # 专门搜索Google News内容的搜索引擎ID serviceName: google-svc.dns servicePort: 443 - type: bing apiKey: bing-key serviceName: bing-svc.dns servicePort: 443 optionArgs: answerCount: 5并发查询获取更多结果由于搜索引擎限制单次查询返回条数如 Google 单次最多 100 条可配置较小count、通过start偏移并发发起多个查询searchFrom: - type: google apiKey: your-google-api-key cx: search-engine-id serviceName: google-svc.dns servicePort: 443 start: 0 count: 10 - type: google apiKey: your-google-api-key cx: search-engine-id serviceName: google-svc.dns servicePort: 443 start: 10 count: 10 - type: google apiKey: your-google-api-key cx: search-engine-id serviceName: google-svc.dns servicePort: 443 start: 20 count: 10注意过高的并发可能导致限流需要根据实际情况调整。Elasticsearch 私有知识库配置searchFrom: - type: elasticsearch serviceName: es-svc.static index: knowledge_base contentField: content semanticTextField: semantic_text # username: elastic # password: password自定义引用格式与位置needReference: true referenceFormat: ### 数据来源\n%s referenceLocation: tail # 在回答结尾添加引用而不是开头 searchFrom: - type: bing apiKey: your-bing-key serviceName: search-service.dns servicePort: 8080按需启用插件兼容 OpenAI 搜索协议配置defaultEnable: false后仅当请求中包含web_search_options字段时插件才被激活即使该字段是空对象web_search_options: {}defaultEnable: false searchFrom: - type: google apiKey: your-google-api-key cx: search-engine-id serviceName: google-svc.dns servicePort: 443动态搜索深度search_context_size通过在请求的web_search_options字段中添加search_context_size参数可动态调整搜索查询次数覆盖配置中的maxCount{ web_search_options: { search_context_size: medium } }三个级别low生成 1 个搜索查询适合简单问题、medium生成 3 个默认值、high生成 5 个适合复杂问题。Step 3直接请求进行测试配置完成后即可将 Higress 暴露的 LLM 网关地址接入任意 OpenAI 兼容客户端。社区常见的做法是使用 LobeChat 等对话客户端对接 Higress 地址发起提问后即可看到 DeepSeek 基于实时搜索结果生成的、带[1]、[2]编号引用的回答时效性问答如最新新闻、实时行情的准确性显著提升。源码级原理AI Search 插件的完整工作链路3.1 配置解析parseConfig插件通过wrapper.ParseConfig(parseConfig)注册配置解析逻辑见 main.go。解析时defaultEnable默认值为trueneedReference为 true 时referenceFormat默认**References:**\n%s且必须包含%sreferenceLocation只能是head或tailpromptTemplate为空时使用内置中文模板且强校验必须同时包含{search_results}和{question}占位符否则返回错误searchFrom按type分发到对应引擎构造函数bing/google/arxiv/elasticsearch/quark未知类型直接报错未配置任何引擎也会报错searchRewrite存在时llmServiceName、llmServicePort、llmUrl、llmModelName均为必填timeoutMillisecond默认 30000maxCount默认 3。3.2 请求处理与 Prompt 注入在onHttpRequestBody中插件从请求 JSON 中提取messages里最后一条 user 消息作为查询词。若配置了searchRewrite插件会先调用 LLM 服务改写查询请求体使用stream:false通过Authorization: Bearer apiKey鉴权改写响应中若包含none表示无需搜索直接放行原请求。改写结果按行解析为搜索引擎上下文engine.SearchContext支持internet:、private:以及 arxiv 论文类别三种前缀其中 arxiv 类别还会额外生成一个不带类别限定的备份查询以提升召回率。随后executeSearch并行调用所有需要执行的搜索引擎各引擎实现统一的 SearchEngine 接口包含NeedExectue、Client、CallArgs、ParseResult四个方法。全部完成后对结果按链接去重合并再格式化为[webpage X begin]...[webpage X end]结构needReference: true时结果带编号并生成[X] 标题格式的引用列表存入上下文之后用{search_results}、{question}、{cur_date}北京时间格式2006年1月2日填充 Prompt 模板并通过proxywasm.ReplaceHttpRequestBody替换请求体中 user 消息的 content。3.3 引用注入普通响应与流式响应普通响应onHttpResponseBody读取choices.0.message.content若内容以think开头DeepSeek 等推理模型引用插在/think之后否则默认插在回答开头referenceLocation: head配置tail时插在末尾。流式响应SSEonStreamingResponseBody逐块处理data:消息通过 30 字符的缓冲窗口BUFFER_SIZE识别think前缀与/think结束标记在合适的边界处插入引用并处理了/think标签被分块截断的边界情况见processSSEMessage。流式模式下referenceLocation: tail会等到finish_reason stop的最后一条消息再追加引用。3.4 内置搜索重写 Prompt 的自动选择搜索重写依赖的 LLM Prompt 按引擎组合自动选择prompts 目录同时配置私有知识库 互联网 Arxiv使用full.md互联网 Arxiv使用arxiv.md私有知识库 互联网使用private.md仅互联网使用internet.md其中仅配置夸克时使用中文优化的chinese-internet.md。以 internet.md 为例Prompt 要求 LLM 判断是否需要查询搜索引擎并在需要时输出以internet:开头的多行查询总次数受{max_count}控制同时约定搜索语言与用户提问语言保持一致。注意事项与最佳实践Prompt 模板约束必须包含{search_results}和{question}占位符可选使用{cur_date}插入当前日期北京时间格式默认模板已包含搜索结果处理指引与回答规范无特殊需求可直接使用默认模板。超时模型多个搜索引擎并行查询总超时时间 所有搜索引擎配置中最大timeoutMillisecond 处理时间配置多个引擎时建议统一超时以控制端到端延迟。Arxiv 免费Arxiv 搜索不需要 API Key但可通过arxivCategory指定论文类别缩小搜索范围配合搜索重写可自动识别论文所属领域并优化英文关键词。配额与限流并发查询可能触发搜索引擎限流Google 单次查询最多返回 100 条需通过count/start分页与多实例并发扩展。私有知识库使用 Elasticsearch 引擎时需注意版本RRF 需要 8.8与 Embedding 模型 License 限制适合用于构建 RAG 场景。结语通过本文的四个步骤你已经可以在 Higress 上为 DeepSeek 完整开启联网搜索能力配置 API Key、创建搜索引擎服务来源、部署并调优ai-search插件、接入 LobeChat 等客户端验证效果。进一步地理解插件在 main.go 与各 engine 实现 中的源码逻辑可以帮助你在多引擎组合、搜索重写、引用格式等维度做出更贴合业务场景的定制将 Higress 打造成具备实时知识检索能力的 AI 网关。【免费下载链接】higress AI Gateway | AI Native API Gateway项目地址: https://gitcode.com/GitHub_Trending/hi/higress创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考