学习笔记13《代理(下):结构化工具对话、Self-Ask with Search以及Plan and execute代理》

189 阅读3分钟

LangChain 代理类型学习笔记

1. 结构化工具对话代理(Structured Tool Chat Agent)

  • 定义:允许代理调用包含一系列复杂工具的“结构化工具箱”,组合调用多个工具完成批次相关的任务集合。
  • 应用场景:文件管理、Web浏览器交互等。
  • PlayWright工具包:一个自动化框架,模拟用户操作网页,支持多种浏览器。

示例代码分析

from langchain.agents.agent_toolkits import PlayWrightBrowserToolkit
from langchain.tools.playwright.utils import create_async_playwright_browser

async_browser = create_async_playwright_browser()
toolkit = PlayWrightBrowserToolkit.from_browser(async_browser=async_browser)
tools = toolkit.get_tools()
print(tools)

from langchain.agents import initialize_agent, AgentType
from langchain.chat_models import ChatAnthropic, ChatOpenAI

llm = ChatOpenAI(temperature=0.5)  
agent_chain = initialize_agent(
    tools,
    llm,
    agent=AgentType.STRUCTURED_CHAT_ZERO_SHOT_REACT_DESCRIPTION,
    verbose=True,
)

async def main():
    response = await agent_chain.arun("What are the headers on python.langchain.com?")
    print(response)

import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
  • 分析:代理首先使用PlayWrightBrowserToolkit访问网页,然后使用get_elements工具获取标题。这个过程展示了代理如何通过组合工具完成复杂任务。

2. Self-Ask with Search 代理

  • 定义:利用“Follow-up Question”加“Intermediate Answer”技巧,辅助大模型寻找过渡性答案,引出最终答案。
  • 应用场景:解决多跳问题,即问题的答案依赖于另一个子问题的答案。

示例代码分析

from langchain import OpenAI, SerpAPIWrapper 
from langchain.agents import initialize_agent, Tool
from langchain.agents import AgentType

llm = OpenAI(temperature=0)
search = SerpAPIWrapper()
tools = [
    Tool(
        name="Intermediate Answer", 
        func=search.run,
        description="useful for when you need to ask with search",
    )
]

self_ask_with_search = initialize_agent(
    tools, llm, agent=AgentType.SELF_ASK_WITH_SEARCH, verbose=True
)
self_ask_with_search.run(
    "使用玫瑰作为国花的国家的首都是哪里?"  
)
  • 分析:代理通过两次追问和搜索,最终确定使用玫瑰作为国花的国家是英国,然后找到英国的首都是伦敦。

3. Plan and execute 代理

  • 定义:通过首先计划要做什么,然后执行子任务来实现目标。
  • 特点:计划由一个大语言模型代理完成,执行由另一个大语言模型代理完成。

示例代码分析

from langchain.chat_models import ChatOpenAI
from langchain_experimental.plan_and_execute import PlanAndExecute, load_agent_executor, load_chat_planner
from langchain.llms import OpenAI
from langchain import SerpAPIWrapper
from langchain.agents.tools import Tool
from langchain import LLMMathChain

search = SerpAPIWrapper()
llm = OpenAI(temperature=0)
llm_math_chain = LLMMathChain.from_llm(llm=llm, verbose=True)
tools = [
    Tool(
        name = "Search",
        func=search.run,
        description="useful for when you need to answer questions about current events"
    ),
    Tool(
        name="Calculator",
        func=llm_math_chain.run,
        description="useful for when you need to answer questions about math"
    ),
]
model = ChatOpenAI(temperature=0)
planner = load_chat_planner(model)
executor = load_agent_executor(model, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor, verbose=True)

agent.run("在纽约,100美元能买几束玫瑰?")
  • 分析:代理首先计划如何解决问题,然后执行子任务,如搜索和计算,最终给出答案。

思考题解答

  1. 打印PlayWrightBrowserToolkit中的所有具体工具名称的列表

    tools = toolkit.get_tools()
    print([t.name for t in tools])
    

    这将打印出PlayWrightBrowserToolkit中所有工具的名称列表。

  2. 分析PlanAndExecute、AgentExecutor和LLMMathChain链的调用流程以及代理的思考过程

    • PlanAndExecute:首先由planner制定计划,然后将计划传递给executor执行。
    • AgentExecutor:负责执行具体的工具调用,如搜索和计算。
    • LLMMathChain:处理数学计算相关的任务。
    • 思考过程:代理首先确定需要执行的任务,然后根据任务选择合适的工具,执行任务,并根据结果进行下一步思考或给出最终答案。