Skip to content
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

feat: add web search command using WebSurferAgent for enhanced resear… #3

Merged
merged 1 commit into from
Feb 11, 2025

Conversation

Mai0313
Copy link
Owner

@Mai0313 Mai0313 commented Feb 11, 2025

…ch capabilities

What does this PR do?

Fixes #<issue_number>

Before submitting

  • Did you make sure title is self-explanatory and the description concisely explains the PR?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you list all the breaking changes introduced by this pull request?
  • Did you test your PR locally with pytest command?
  • Did you run pre-commit hooks with pre-commit run -a command?

Did you have fun?

Make sure you had fun coding 🙃

Summary by CodeRabbit

  • 新功能

    • 新增了网页搜索命令,用户可通过 Discord 直接发起网页搜索并获取结果摘要。
  • Chores

    • 升级了运行环境,Python 版本更新至 3.11。
    • 调整了依赖项和预发行配置,优化了版本管理。

Copy link

coderabbitai bot commented Feb 11, 2025

Walkthrough

此次变更主要涉及三个方面:

  1. Python 版本升级:在 .python-version 文件中,Python 版本从 3.10.14 升级到 3.11
  2. 依赖与配置修改:在 pyproject.toml 中,将依赖 "autogen>=0.7.2" 替换为 "ag2[browser-use]>=0.7.2",同时将 prerelease 设置从 "if-necessary" 调整为 "if-necessary-or-explicit"
  3. 新增命令方法:在 src/cogs/gen_search.py 文件中,为 WebSearchCogs 类新增了异步方法 web,用于利用 WebSurferAgent 执行网页搜索,并将结果返回至 Discord 上的上下文。

Changes

文件路径 修改摘要
.python-version 升级 Python 版本从 3.10.143.11
pyproject.toml 修改依赖:"autogen>=0.7.2" 替换为 "ag2[browser-use]>=0.7.2";更新 prerelease 设置为 "if-necessary-or-explicit"
src/cogs/gen_search.py 新增 WebSearchCogs 类中的异步方法 web,用于调用 WebSurferAgent 执行网页搜索

Sequence Diagram(s)

sequenceDiagram
    participant U as 用户
    participant D as Discord Bot
    participant G as WebSearchCogs
    participant W as WebSurferAgent

    U->>D: 发送 "web" 指令与提示文本
    D->>G: 将指令和参数转发至 WebSearchCogs
    G->>W: 初始化 WebSurferAgent 并调用 run 方法
    W-->>G: 返回网页搜索的结果摘要
    G->>D: 将搜索结果发送回 Discord 频道
Loading

Poem

跳跳兔唱着欢歌,
代码新变如晨曦,
Python升腾迎朝阳,
依赖更新舞风起,
网络搜索显神奇,
快乐代码,飞跃天际! 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings (Beta)

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@Mai0313 Mai0313 merged commit 8bf0798 into master Feb 11, 2025
3 of 4 checks passed
@Mai0313 Mai0313 deleted the feat/add_browser_use branch February 11, 2025 13:17
@github-actions github-actions bot added enhancement New feature or request chore dependencies labels Feb 11, 2025
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
pyproject.toml (1)

69-69: 建议指定更严格的版本约束

当前的版本约束 "ag2[browser-use]>=0.7.2" 可能会在新版本发布时导致不兼容问题。建议使用更严格的版本约束。

建议修改为:

-    "ag2[browser-use]>=0.7.2",
+    "ag2[browser-use]>=0.7.2,<0.8.0",
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7739179 and 72068b8.

⛔ Files ignored due to path filters (1)
  • uv.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • .python-version (1 hunks)
  • pyproject.toml (2 hunks)
  • src/cogs/gen_search.py (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .python-version
🔇 Additional comments (1)
pyproject.toml (1)

198-198: 验证预发布设置的影响

更新后的 prerelease = "if-necessary-or-explicit" 设置可能会影响依赖项的安装稳定性。

✅ Verification successful

🌐 Web query:

uv package manager prerelease setting "if-necessary-or-explicit" vs "if-necessary" difference

💡 Result:

The prerelease setting in uv controls how pre-release package versions are handled during dependency resolution. Here's the key difference between if-necessary-or-explicit and if-necessary:

Prerelease Handling Strategies

1. if-necessary

  • Allows pre-releases only when:
    • All available versions of a package are pre-releases[1][6][9]
    • No stable releases exist for the package[1][6]

2. if-necessary-or-explicit (Default)
Adds an additional condition to if-necessary:

  • Allows pre-releases when:
    • All versions are pre-releases (same as if-necessary)
    • OR first-party requirements explicitly specify a pre-release version using markers like:
      • package==2.1.0rc1
      • package>=2.1.0a0[1][6][9]

Key Comparison

Strategy Accepts Pre-Releases When... Explicit Specifiers Required?
if-necessary Only when no stable versions exist No
if-necessary-or-explicit No stable versions OR explicit pre-release markers in requirements Yes for non-emergency cases

This means if-necessary-or-explicit provides more flexibility by allowing intentional adoption of pre-releases through explicit version specifiers, while still preventing accidental use of unstable versions[1][6]. The default setting helps balance stability with access to cutting-edge features when explicitly requested[9].

Citations:


确认预发布设置变更影响

根据 uv 官方文档及相关说明,if-necessary-or-explicitif-necessary 的主要区别在于:

  • if-necessary:仅当包的所有可用版本都是预发布版本(即没有稳定版本)时才允许安装预发布版本。
  • if-necessary-or-explicit:在上述条件成立的情况下,同样允许安装预发布版本,同时在依赖项中若显式指定了预发布版本(例如 package==2.1.0rc1package>=2.1.0a0),也会允许安装预发布版本。

因此,更新后的配置 prerelease = "if-necessary-or-explicit" 增加了对显式指定预发布依赖的支持,可能会在某些情况下引入预发布版本,从而影响依赖项安装的稳定性。请注意这一行为是否符合项目预期。

Comment on lines +20 to +36
@commands.command()
async def web(self, ctx: commands.Context, *, prompt: str) -> None:
llm_config = {"model": "gpt-4o-mini"}
browser_use_browser_config = {
"browser_config": {"headless": True},
"agent_kwargs": {"generate_gif": True},
}
web_researcher = WebSurferAgent(
name="researcher",
llm_config=llm_config,
human_input_mode="NEVER",
web_tool="browser_use",
web_tool_kwargs=browser_use_browser_config,
)
ag2_news_result = web_researcher.run(message=prompt, tools=web_researcher.tools, user_input=False)
ctx.send(content=ag2_news_result.summary)

Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

需要添加错误处理和改进实现

该实现存在以下问题:

  1. 缺少错误处理机制,可能导致未捕获的异常
  2. Discord消息发送方法使用错误,ctx.send() 需要使用 await
  3. 硬编码的模型名称 "gpt-4o-mini" 可能存在风险

建议按照以下方式修改代码:

@commands.command()
async def web(self, ctx: commands.Context, *, prompt: str) -> None:
-    llm_config = {"model": "gpt-4o-mini"}
+    llm_config = {"model": "gpt-3.5-turbo"}  # 使用稳定的模型
    browser_use_browser_config = {
        "browser_config": {"headless": True},
        "agent_kwargs": {"generate_gif": True},
    }
-    web_researcher = WebSurferAgent(
-        name="researcher",
-        llm_config=llm_config,
-        human_input_mode="NEVER",
-        web_tool="browser_use",
-        web_tool_kwargs=browser_use_browser_config,
-    )
-    ag2_news_result = web_researcher.run(message=prompt, tools=web_researcher.tools, user_input=False)
-    ctx.send(content=ag2_news_result.summary)
+    try:
+        web_researcher = WebSurferAgent(
+            name="researcher",
+            llm_config=llm_config,
+            human_input_mode="NEVER",
+            web_tool="browser_use",
+            web_tool_kwargs=browser_use_browser_config,
+        )
+        ag2_news_result = await web_researcher.run(
+            message=prompt,
+            tools=web_researcher.tools,
+            user_input=False
+        )
+        await ctx.send(content=ag2_news_result.summary)
+    except Exception as e:
+        await ctx.send(content=f"搜索时发生错误: {e!s}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
@commands.command()
async def web(self, ctx: commands.Context, *, prompt: str) -> None:
llm_config = {"model": "gpt-4o-mini"}
browser_use_browser_config = {
"browser_config": {"headless": True},
"agent_kwargs": {"generate_gif": True},
}
web_researcher = WebSurferAgent(
name="researcher",
llm_config=llm_config,
human_input_mode="NEVER",
web_tool="browser_use",
web_tool_kwargs=browser_use_browser_config,
)
ag2_news_result = web_researcher.run(message=prompt, tools=web_researcher.tools, user_input=False)
ctx.send(content=ag2_news_result.summary)
@commands.command()
async def web(self, ctx: commands.Context, *, prompt: str) -> None:
llm_config = {"model": "gpt-3.5-turbo"} # 使用稳定的模型
browser_use_browser_config = {
"browser_config": {"headless": True},
"agent_kwargs": {"generate_gif": True},
}
try:
web_researcher = WebSurferAgent(
name="researcher",
llm_config=llm_config,
human_input_mode="NEVER",
web_tool="browser_use",
web_tool_kwargs=browser_use_browser_config,
)
ag2_news_result = await web_researcher.run(
message=prompt,
tools=web_researcher.tools,
user_input=False
)
await ctx.send(content=ag2_news_result.summary)
except Exception as e:
await ctx.send(content=f"搜索时发生错误: {e!s}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant