MultiOnTool

描述

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