Skip to content

Added New web search tool: Brave Search#6847

Open
daniel5u wants to merge 4 commits intoAstrBotDevs:masterfrom
daniel5u:feat/brave-search
Open

Added New web search tool: Brave Search#6847
daniel5u wants to merge 4 commits intoAstrBotDevs:masterfrom
daniel5u:feat/brave-search

Conversation

@daniel5u
Copy link
Contributor

@daniel5u daniel5u commented Mar 23, 2026

Modifications / 改动点

Added the web_search tool for Brave Search, keeping the code style same as the existed.

  • This is NOT a breaking change. / 这不是一个破坏性变更。

Screenshots or Test Results / 运行截图或测试结果

image

Checklist / 检查清单

  • 😊 If there are new features added in the PR, I have discussed it with the authors through issues/emails, etc.
    / 如果 PR 中有新加入的功能,已经通过 Issue / 邮件等方式和作者讨论过。

  • 👀 My changes have been well-tested, and "Verification Steps" and "Screenshots" have been provided above.
    / 我的更改经过了良好的测试,并已在上方提供了“验证步骤”和“运行截图”

  • 🤓 I have ensured that no new dependencies are introduced, OR if new dependencies are introduced, they have been added to the appropriate locations in requirements.txt and pyproject.toml.
    / 我确保没有引入新依赖库,或者引入了新依赖库的同时将其添加到 requirements.txtpyproject.toml 文件相应位置。

  • 😮 My changes do not introduce malicious code.
    / 我的更改没有引入恶意代码。

Summary by Sourcery

Add Brave Search as a configurable web search provider alongside existing engines and integrate it across backend search tooling, configuration, and dashboard handling.

New Features:

  • Introduce a new web_search_brave LLM tool that performs web search via the Brave Search API with API key rotation support.
  • Add brave as a selectable web search provider with corresponding Brave API key list in the AstrBot configuration schema and defaults.

Enhancements:

  • Extend web search reference extraction and display logic in the dashboard and agent hooks to recognize Brave-based search results alongside existing providers.
  • Align docker compose configuration for AstrBot with updated port mapping and simplified environment definition.

Documentation:

  • Update English and Chinese AstrBot configuration docs to describe the new Brave web search provider and its API key settings.

@dosubot dosubot bot added the size:L This PR changes 100-499 lines, ignoring generated files. label Mar 23, 2026
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces Brave Search as a new web search tool within the AstrBot ecosystem. It includes the necessary code modifications to integrate the Brave Search API, along with corresponding configuration options and updates to ensure compatibility with existing features. The changes also incorporate UI updates to reflect the new search option.

Highlights

  • New Feature: Introduced Brave Search as a new web search tool, integrating it into the existing web search functionality.
  • Configuration: Added configuration options for Brave Search API keys, allowing multiple keys for rotation.
  • Tool Integration: Ensured seamless integration of Brave Search by updating relevant files and components to include the new search option.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 3 issues, and left some high level feedback:

  • The changes to compose.yml (port mapping adjustment and environment syntax change) seem unrelated to adding the Brave search tool and may surprise existing deployments; consider reverting these or moving them to a separate PR focused on deployment configuration.
  • Error messages for Brave search are mixed between Chinese and English (e.g., in _get_brave_key vs search_from_brave); consider standardizing the language and style of these messages to match the rest of the web search tools for consistency.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The changes to compose.yml (port mapping adjustment and environment syntax change) seem unrelated to adding the Brave search tool and may surprise existing deployments; consider reverting these or moving them to a separate PR focused on deployment configuration.
- Error messages for Brave search are mixed between Chinese and English (e.g., in `_get_brave_key` vs `search_from_brave`); consider standardizing the language and style of these messages to match the rest of the web search tools for consistency.

## Individual Comments

### Comment 1
<location path="astrbot/builtin_stars/web_searcher/main.py" line_range="619-620" />
<code_context>
+        """
+        logger.info(f"web_searcher - search_from_brave: {query}")
+        cfg = self.context.get_config(umo=event.unified_msg_origin)
+        if not cfg.get("provider_settings", {}).get("websearch_brave_key", []):
+            raise ValueError("Error: Brave API key is not configured in AstrBot.")
+
+        if count < 1:
</code_context>
<issue_to_address>
**suggestion:** Avoid duplicated configuration checks and inconsistent error messages for missing Brave key.

This case is handled both here and in `_get_brave_key`, with different error messages. Please choose a single place to perform the validation and define the message—either centralize it in `_get_brave_key` and let `search_from_brave` rely on that, or have `_get_brave_key` assume a non-empty list and keep the check only here. A single validation path will keep behavior consistent and easier to maintain.

Suggested implementation:

```python
        logger.info(f"web_searcher - search_from_brave: {query}")

        if count < 1:

```

These changes assume that `_get_brave_key`:
1. Is the single source of truth for accessing the Brave API key.
2. Performs its own validation and raises a clear, consistent error if the key is missing or invalid.

If `_get_brave_key` does not yet validate the key or has an undesired error message, you should:
- Add the non-empty-key validation there.
- Standardize the error message in `_get_brave_key` to whatever you want to expose to callers like `search_from_brave`.
</issue_to_address>

### Comment 2
<location path="compose.yml" line_range="8-9" />
<code_context>
-    ports: # mappings description: https://github.com/AstrBotDevs/AstrBot/issues/497
-      - "6185:6185" # 必选,AstrBot WebUI 端口
-      - "6199:6199" # 可选, QQ 个人号 WebSocket 端口
+    ports:  # mappings description: https://github.com/AstrBotDevs/AstrBot/issues/497
+      - 6184:6185  # 必选,AstrBot WebUI 端口
       # - "6195:6195" # 可选, 企业微信 Webhook 端口
       # - "6196:6196" # 可选, QQ 官方接口 Webhook 端口
</code_context>
<issue_to_address>
**issue (bug_risk):** Host port 6184 vs. comment stating the AstrBot WebUI port may confuse users or break existing setups.

The host port used to be `6185:6185`, and the comment explicitly documents 6185 as the WebUI port. Changing the host port to 6184 while leaving the comment (and any existing docs/scripts) referring to 6185 will likely break existing setups. If this change is intentional, please update the comment and any relevant docs to 6184; otherwise, keep the host port as 6185 for backward compatibility.
</issue_to_address>

### Comment 3
<location path="docs/zh/dev/astrbot-config.md" line_range="293" />
<code_context>
-网页搜索提供商类型。默认为 `default`。目前支持 `default``tavily`。
+网页搜索提供商类型。默认为 `default`。目前支持 `default``tavily``bocha``baidu_ai_search``brave`- `default`:能访问 Google 时效果最佳。如果 Google 访问失败,程序会依次访问 Bing, Sogo 搜索引擎。

 - `tavily`:使用 Tavily 搜索引擎。
</code_context>
<issue_to_address>
**issue (typo):** Possible typo: "Sogo" should likely be "Sogou" (or its Chinese name).

This likely refers to the search engine “Sogou” (搜狗). Please change “Sogo” to “Sogou” or its Chinese brand name for correctness.

```suggestion
- `default`:能访问 Google 时效果最佳。如果 Google 访问失败,程序会依次访问 Bing, Sogou 搜索引擎。
```
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Comment on lines +619 to +620
if not cfg.get("provider_settings", {}).get("websearch_brave_key", []):
raise ValueError("Error: Brave API key is not configured in AstrBot.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Avoid duplicated configuration checks and inconsistent error messages for missing Brave key.

This case is handled both here and in _get_brave_key, with different error messages. Please choose a single place to perform the validation and define the message—either centralize it in _get_brave_key and let search_from_brave rely on that, or have _get_brave_key assume a non-empty list and keep the check only here. A single validation path will keep behavior consistent and easier to maintain.

Suggested implementation:

        logger.info(f"web_searcher - search_from_brave: {query}")

        if count < 1:

These changes assume that _get_brave_key:

  1. Is the single source of truth for accessing the Brave API key.
  2. Performs its own validation and raises a clear, consistent error if the key is missing or invalid.

If _get_brave_key does not yet validate the key or has an undesired error message, you should:

  • Add the non-empty-key validation there.
  • Standardize the error message in _get_brave_key to whatever you want to expose to callers like search_from_brave.

网页搜索提供商类型。默认为 `default`。目前支持 `default``tavily`。
网页搜索提供商类型。默认为 `default`。目前支持 `default``tavily`、`bocha`、`baidu_ai_search`、`brave`。

- `default`:能访问 Google 时效果最佳。如果 Google 访问失败,程序会依次访问 Bing, Sogo 搜索引擎。
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue (typo): Possible typo: "Sogo" should likely be "Sogou" (or its Chinese name).

This likely refers to the search engine “Sogou” (搜狗). Please change “Sogo” to “Sogou” or its Chinese brand name for correctness.

Suggested change
- `default`:能访问 Google 时效果最佳。如果 Google 访问失败,程序会依次访问 Bing, Sogo 搜索引擎。
- `default`:能访问 Google 时效果最佳。如果 Google 访问失败,程序会依次访问 Bing, Sogou 搜索引擎。

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds Brave Search as a new web search provider. The implementation is well-integrated into the existing system, including the backend logic for API key rotation and making search requests, configuration updates, and frontend changes for displaying search results. The changes are consistent with the existing codebase structure. I have a couple of suggestions: one to remove a redundant check in the new search logic, and another regarding unrelated changes in the compose.yml file that should ideally be in a separate pull request.

Comment on lines +619 to +620
if not cfg.get("provider_settings", {}).get("websearch_brave_key", []):
raise ValueError("Error: Brave API key is not configured in AstrBot.")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This check for the Brave API key is redundant. The _get_brave_key method, which is called on line 636, already performs this check and raises a ValueError if no keys are configured. You can remove these lines to avoid duplicate logic.

Additionally, the error message here is in English, while the one in _get_brave_key is in Chinese, which is inconsistent. Centralizing the check will resolve this.

compose.yml Outdated
- "6185:6185" # 必选,AstrBot WebUI 端口
- "6199:6199" # 可选, QQ 个人号 WebSocket 端口
ports: # mappings description: https://github.com/AstrBotDevs/AstrBot/issues/497
- 6184:6185 # 必选,AstrBot WebUI 端口
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The changes in this compose.yml file, such as altering the host port from 6185 to 6184, removing the 6199 port mapping, and changing the environment syntax, seem unrelated to adding the Brave Search feature. Committing unrelated changes in a feature PR can be confusing and might introduce unintended side effects for other users. It's recommended to revert the changes in this file or move them to a separate PR if they are still needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:L This PR changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant