LangChain AI代理:高效网页浏览助手

2025年04月09日 由 alex 发表 850 0

本文介绍了一个很棒的项目,在这个项目中,一个基于 LangChain 的人工智能代理与PlayWright 相结合。


PlayWright

LangChain 的 Playwright 集成页面所概述的这项技术,展示了在通过 Python 实现对基于网络的内容进行自动化操作和交互方面的重大进展。


PlayWright 是一个浏览器自动化库,将该库与 LangChain 相结合,使开发人员能够以编程方式浏览、抓取和处理动态网页。


这增强了人工智能驱动的应用程序的数据检索能力。


这种结合实现了更复杂的工作流程,例如从网站中提取实时信息,以输入到语言模型中进行分析或内容生成。


我想最终这项技术填补了人工智能代理与动态网页交互之间的空白,从而助力打造具备情境感知能力的人工智能系统。


下面的图片展示了该架构的基本构成。在代码示例中,使用了Anthropic的大语言模型作为基础架构。  


9


下面的图片展示了不同组件之间的事件发生顺序。 


10


以下是一个简单的例子,我向LangChain人工智能代理提出了以下问题:What is the current ZAR USD exchange rate?


result = await agent_chain.arun("What is the current ZAR USD exchange rate?")
print(result)


请注意下面图片中,LangChain人工智能代理是如何经历一系列的“观察、思考、行动、观察……”过程的。直到出现名为“最终答案”的行动时,这一流程才结束,并给出最终答案。 


11


LangChain在突出展示如何在基于智能体的框架内利用这项技术方面做得很好,这是构建智能、自主系统的一个关键方面。


在这种情况下,人工智能智能体指的是一个人工智能实体——通常由语言模型驱动——它能够根据用户指令或预定义的目标来执行任务、做出决策并与自身所处环境进行交互。


通过将Playwright用作人工智能智能体中的一个智能体工具,该系统获得了主动与网络浏览器交互的能力,使其能够在网站上执行复杂的、多步骤的操作。


例如,一个人工智能智能体可能会被赋予在网上研究某个主题的任务,它可以浏览搜索结果、访问相关页面,并在无需人工干预的情况下提取最新信息。


PlayWright浏览器工具包

下面的Python代码可以原封不动地复制并粘贴到一个笔记本中运行。对这段代码唯一需要更改或更新的地方是添加你的Anthropic API密钥。 


%pip install --upgrade --quiet  playwright > /dev/null
%pip install --upgrade --quiet  lxml
###
# If this is your first time using playwright, you'll have to install a browser executable.
# Running `playwright install` by default installs a chromium browser executable.
# playwright install
from langchain_community.agent_toolkits import PlayWrightBrowserToolkit
###
from langchain_community.tools.playwright.utils import (
    create_async_playwright_browser,  # A synchronous browser is available, though it isn't compatible with jupyter.\n",   },
)
###
# This import is required only for jupyter notebooks, since they have their own eventloop
###
import nest_asyncio
###
nest_asyncio.apply()
###
!playwright install
###
!pip install langchain_anthropic
###
async_browser = create_async_playwright_browser()
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
tools = toolkit.get_tools()
tools
###
tools_by_name = {tool.name: tool for tool in tools}
navigate_tool = tools_by_name["navigate_browser"]
get_elements_tool = tools_by_name["get_elements"]
###
await navigate_tool.arun(
    {"url": "https://web.archive.org/web/20230428133211/https://cnn.com/world"}
)
###
# The browser is shared across tools, so the agent can interact in a stateful manner
await get_elements_tool.arun(
    {"selector": ".container__headline", "attributes": ["innerText"]}
)
###
# If the agent wants to remember the current webpage, it can use the `current_webpage` tool
await tools_by_name["current_webpage"].arun({})


在代理中使用

同样,下面的Python代码可以原封不动地复制并粘贴到笔记本中运行。对该代码唯一需要做出的更改或更新是添加你的Anthropic API密钥。 


import os
# Set the API key directly in the code
os.environ["ANTHROPIC_API_KEY"] = "<>"  # Replace with your actual API key
from langchain.agents import AgentType, initialize_agent
from langchain_anthropic import ChatAnthropic
# Get API key from environment variable, raise error if not found
anthropic_api_key = os.environ.get("ANTHROPIC_API_KEY")
if anthropic_api_key is None:
    raise ValueError(
        "ANTHROPIC_API_KEY environment variable not set. "
        "Please set it with your Anthropic API key."
    )
# Initialize ChatAnthropic with the API key
llm = ChatAnthropic(
    model_name="claude-3-haiku-20240307", 
    temperature=0, 
    anthropic_api_key=anthropic_api_key # Pass the API key here
)  
agent_chain = initialize_agent(
    tools,
    llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)
###
result = await agent_chain.arun("What are the headers on langchain.com?")
print(result)
###


这种设置使人工智能智能体能够将从网络获取的数据与LangChain的自然语言处理能力相结合,形成一个无缝的流程,在这个流程中,原始的网络信息被检索、处理,并转化为深刻见解或实际行动。


例如,一个商业智能人工智能智能体可以自主监控竞争对手的网站,提取价格数据,并生成一份比较报告。 

文章来源:https://medium.com/@cobusgreyling/langchain-web-browsing-ai-agent-e58852ade69e
欢迎关注ATYUN官方公众号
商务合作及内容投稿请联系邮箱:bd@atyun.com
评论 登录
写评论取消
回复取消