-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(websearch): 新增Exa搜索提供商,支持 Tavily/Exa API Base URL 可配置,补充搜索工具相关文档 #7359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
piexian
wants to merge
6
commits into
AstrBotDevs:master
Choose a base branch
from
piexian:feat/exa-search-provider
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,234
−221
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9f8f419
feat(websearch): 新增 Exa 搜索提供商,支持 Tavily/Exa API Base URL 可配置
piexian f0edbb9
Apply suggestions from code review
piexian 479c58e
fix(websearch): 修复全局 HEADERS 污染、密钥索引越界等问题,提取共享工具函数消除重复代码
piexian 22e2c8b
fix(websearch): 统一前后端网页搜索引用提取逻辑,增加前端 refs 降级获取
piexian 370167f
fix(websearch): 修复 UUID 生成逻辑,确保唯一性;更新 API Base URL 错误提示信息;新增消息引用缓存机制
piexian 96e15f7
fix(websearch): 放宽 API Base URL 校验,增强 Tavily/Exa 请求报错提示
piexian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| import json | ||
| import re | ||
| from typing import Any | ||
| from urllib.parse import urlparse | ||
|
|
||
| WEB_SEARCH_REFERENCE_TOOLS = ( | ||
| "web_search_tavily", | ||
| "web_search_bocha", | ||
| "web_search_exa", | ||
| "exa_find_similar", | ||
| ) | ||
|
|
||
|
|
||
| def normalize_web_search_base_url( | ||
| base_url: str | None, | ||
| *, | ||
| default: str, | ||
| provider_name: str, | ||
| ) -> str: | ||
| normalized = (base_url or "").strip() | ||
| if not normalized: | ||
| normalized = default | ||
| normalized = normalized.rstrip("/") | ||
|
|
||
| parsed = urlparse(normalized) | ||
| if parsed.scheme not in {"http", "https"} or not parsed.netloc: | ||
| raise ValueError( | ||
| f"Error: {provider_name} API Base URL must start with http:// or " | ||
| f"https://. Proxy base paths are allowed. Received: {normalized!r}.", | ||
| ) | ||
| return normalized | ||
|
|
||
|
|
||
| def _iter_web_search_result_items( | ||
| accumulated_parts: list[dict[str, Any]], | ||
| ): | ||
| for part in accumulated_parts: | ||
| if part.get("type") != "tool_call" or not part.get("tool_calls"): | ||
| continue | ||
|
|
||
| for tool_call in part["tool_calls"]: | ||
| if tool_call.get( | ||
| "name" | ||
| ) not in WEB_SEARCH_REFERENCE_TOOLS or not tool_call.get("result"): | ||
| continue | ||
|
|
||
| result = tool_call["result"] | ||
| try: | ||
| result_data = json.loads(result) if isinstance(result, str) else result | ||
| except json.JSONDecodeError: | ||
| continue | ||
|
|
||
| if not isinstance(result_data, dict): | ||
| continue | ||
|
|
||
| for item in result_data.get("results", []): | ||
| if isinstance(item, dict): | ||
| yield item | ||
|
|
||
|
|
||
| def _extract_ref_indices(accumulated_text: str) -> list[str]: | ||
| ref_indices: list[str] = [] | ||
| seen_indices: set[str] = set() | ||
|
|
||
| for match in re.finditer(r"<ref>(.*?)</ref>", accumulated_text): | ||
| ref_index = match.group(1).strip() | ||
| if not ref_index or ref_index in seen_indices: | ||
| continue | ||
| ref_indices.append(ref_index) | ||
| seen_indices.add(ref_index) | ||
|
|
||
| return ref_indices | ||
|
|
||
|
|
||
| def collect_web_search_ref_items( | ||
| accumulated_parts: list[dict[str, Any]], | ||
| favicon_cache: dict[str, str] | None = None, | ||
| ) -> list[dict[str, Any]]: | ||
| web_search_refs: list[dict[str, Any]] = [] | ||
| seen_indices: set[str] = set() | ||
|
|
||
| for item in _iter_web_search_result_items(accumulated_parts): | ||
| ref_index = item.get("index") | ||
| if not ref_index or ref_index in seen_indices: | ||
| continue | ||
|
|
||
| payload = { | ||
| "index": ref_index, | ||
| "url": item.get("url"), | ||
| "title": item.get("title"), | ||
| "snippet": item.get("snippet"), | ||
| } | ||
| if favicon_cache and payload["url"] in favicon_cache: | ||
| payload["favicon"] = favicon_cache[payload["url"]] | ||
|
|
||
| web_search_refs.append(payload) | ||
| seen_indices.add(ref_index) | ||
|
|
||
| return web_search_refs | ||
|
|
||
|
|
||
| def build_web_search_refs( | ||
| accumulated_text: str, | ||
| accumulated_parts: list[dict[str, Any]], | ||
| favicon_cache: dict[str, str] | None = None, | ||
| ) -> dict: | ||
| ordered_refs = collect_web_search_ref_items(accumulated_parts, favicon_cache) | ||
| if not ordered_refs: | ||
| return {} | ||
|
|
||
| refs_by_index = {ref["index"]: ref for ref in ordered_refs} | ||
| ref_indices = _extract_ref_indices(accumulated_text) | ||
| used_refs = [refs_by_index[idx] for idx in ref_indices if idx in refs_by_index] | ||
|
|
||
| if not used_refs: | ||
| used_refs = ordered_refs | ||
|
|
||
| return {"used": used_refs} | ||
|
|
||
|
|
||
| def collect_web_search_results(accumulated_parts: list[dict[str, Any]]) -> dict: | ||
| web_search_results = {} | ||
|
|
||
| for ref in collect_web_search_ref_items(accumulated_parts): | ||
| web_search_results[ref["index"]] = { | ||
| "url": ref.get("url"), | ||
| "title": ref.get("title"), | ||
| "snippet": ref.get("snippet"), | ||
| } | ||
|
|
||
| return web_search_results | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.