跳转到主要内容

概述

MultiOnTool 旨在封装 MultiOn 的网页浏览功能,使 CrewAI 代理能够使用自然语言指令控制网页浏览器。此工具方便了无缝的网页浏览,使其成为需要动态网页数据交互和网页任务自动化的项目的必备资产。

安装

要使用此工具,您需要安装 MultiOn 包。
uv add multion
您还需要安装 MultiOn 浏览器扩展并启用 API 使用。

开始步骤

要有效使用 MultiOnTool,请遵循以下步骤:
  1. 安装 CrewAI:确保您的 Python 环境中安装了 crewai[tools] 包。
  2. 安装并使用 MultiOn:请遵循 MultiOn 文档 安装 MultiOn 浏览器扩展。
  3. 启用 API 使用:在浏览器的扩展文件夹中点击 MultiOn 扩展(而不是网页上悬浮的 MultiOn 图标)以打开扩展配置。点击“API Enabled”开关以启用 API。

示例

以下示例演示了如何初始化工具并执行网页浏览任务。
代码
from crewai import Agent, Task, Crew
from crewai_tools import MultiOnTool

# Initialize the tool
multion_tool = MultiOnTool(api_key="YOUR_MULTION_API_KEY", local=False)

# Define an agent that uses the tool
browser_agent = Agent(
    role="Browser Agent",
    goal="Control web browsers using natural language",
    backstory="An expert browsing agent.",
    tools=[multion_tool],
    verbose=True,
)

# Example task to search and summarize news
browse_task = Task(
    description="Summarize the top 3 trending AI News headlines",
    expected_output="A summary of the top 3 trending AI News headlines",
    agent=browser_agent,
)

# Create and run the crew
crew = Crew(agents=[browser_agent], tasks=[browse_task])
result = crew.kickoff()

参数

MultiOnTool 在初始化时接受以下参数:
  • api_key:可选。指定 MultiOn API 密钥。如果未提供,它将查找 MULTION_API_KEY 环境变量。
  • local:可选。设置为 True 以在您的浏览器上本地运行代理。请确保 MultiOn 浏览器扩展已安装并且“API Enabled”已勾选。默认为 False
  • max_steps:可选。设置 MultiOn 代理执行命令的最大步数。默认为 3

用法

使用 MultiOnTool 时,代理将提供自然语言指令,工具会将其转换为网页浏览操作。工具会返回浏览会话的结果以及状态。
代码
# Example of using the tool with an agent
browser_agent = Agent(
    role="Web Browser Agent",
    goal="Search for and summarize information from the web",
    backstory="An expert at finding and extracting information from websites.",
    tools=[multion_tool],
    verbose=True,
)

# Create a task for the agent
search_task = Task(
    description="Search for the latest AI news on TechCrunch and summarize the top 3 headlines",
    expected_output="A summary of the top 3 AI news headlines from TechCrunch",
    agent=browser_agent,
)

# Run the task
crew = Crew(agents=[browser_agent], tasks=[search_task])
result = crew.kickoff()
如果返回的状态是 CONTINUE,则应指示代理重新发布相同的指令以继续执行。

实现细节

MultiOnTool 作为 CrewAI 中 BaseTool 的子类实现。它封装了 MultiOn 客户端以提供网页浏览功能。
代码
class MultiOnTool(BaseTool):
    """Tool to wrap MultiOn Browse Capabilities."""

    name: str = "Multion Browse Tool"
    description: str = """Multion gives the ability for LLMs to control web browsers using natural language instructions.
            If the status is 'CONTINUE', reissue the same instruction to continue execution
        """
    
    # Implementation details...
    
    def _run(self, cmd: str, *args: Any, **kwargs: Any) -> str:
        """
        Run the Multion client with the given command.
        
        Args:
            cmd (str): The detailed and specific natural language instruction for web browsing
            *args (Any): Additional arguments to pass to the Multion client
            **kwargs (Any): Additional keyword arguments to pass to the Multion client
        """
        # Implementation details...

结论

MultiOnTool 提供了一种强大的方式,可以将网页浏览功能集成到 CrewAI 代理中。通过使代理能够通过自然语言指令与网站交互,它为基于网络的任务开辟了广泛的可能性,从数据收集和研究到与网络服务的自动化交互。