跳转到主要内容

概述

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 智能体中的强大方式。通过使智能体能够通过自然语言指令与网站互动,它为基于网络的任务开辟了广泛的可能性,从数据收集和研究到与网络服务的自动化互动。