让浏览器AI起来:基于大模型Agent的浏览器自动化工具

最近有个非常火的项目,利用大模型Agent驱动浏览器完成各种操作,如网页搜索、爬虫分析、机票酒店预定、股票监控等,号称全面替代所有在浏览器上的操作,试用方式还是比较简单的,以下将进行简单介绍。

快速开始

通过pip安装:

pip install browser-use

安装web自动化框架:

playwright install

Playwright微软开源的一个浏览器自动化框架,主要用于浏览器自动化执行web测试,提供强大的 API 集,适用于所有现代浏览器。

设置好大模型API KEY,填入.env文件:

OPENAI_API_KEY=

创建智能体,指定命令:

from langchain_openai import ChatOpenAI
from browser_use import Agent
import asyncio
from dotenv import load_dotenv
load_dotenv()

async def main():
    agent = Agent(
        task="Go to Reddit, search for 'browser-use', click on the first post and return the first comment.",
        llm=ChatOpenAI(model="gpt-4o"),
    )
    result = await agent.run()
    print(result)

asyncio.run(main())

模型支持

API

所有适配LangChain chat接口并且支持工具调用的模型理论上都支持,但小模型在解析过程中错误率不可忽视。GPT-4o和DeepSeek-V3这类模型比较合适,推荐DeepSeek-V3,价格更加便宜。

from langchain_openai import ChatOpenAI
from browser_use import Agent
from pydantic import SecretStr

# Initialize the model
llm=ChatOpenAI(base_url='https://api.deepseek.com/v1', model='deepseek-chat', api_key=SecretStr(api_key))

# Create agent with the model
agent = Agent(
    task="Your task here",
    llm=llm,
    use_vision=False
)

本地模型

本地大模型可以使用Ollama,需要从Ollama官网选择支持工具调用的大模型。
让浏览器AI起来:基于大模型Agent的浏览器自动化工具_第1张图片

调用方式同样非常简单,使用langchain_ollama加载模型即可:

from langchain_ollama import ChatOllama
from browser_use import Agent
from pydantic import SecretStr


# Initialize the model
llm=ChatOllama(model="qwen2.5", num_ctx=32000)

# Create agent with the model
agent = Agent(
    task

你可能感兴趣的:(优质项目,大语言模型,计算机杂谈,人工智能,自动化,语言模型,开源)