Plan-and-Execute Agents: Concepts, Implementation, and Usage Examples
Plan-and-Execute Agents
Plan-and-Execute agents first plan what to do, then execute subtasks to accomplish the goal. This idea is primarily inspired by BabyAGI, followed by the "Plan-and-Solve" paper.
Planning is almost always performed by an LLM (Large Language Model).
Execution is typically carried out by a separate agent (equipped with tools).
Imports
from langchain.chains import LLMMathChain
from langchain_community.utilities import DuckDuckGoSearchAPIWrapper
from langchain_core.tools import Tool
from langchain_experimental.plan_and_execute import (
PlanAndExecute,
load_agent_executor,
load_chat_planner,
)
from langchain_openai import ChatOpenAI, OpenAI
# Import necessary libraries and modules
# LLMMathChain: chain for mathematical computations
# DuckDuckGoSearchAPIWrapper: tool for web searches
# Tool: class defining a tool
# PlanAndExecute, load_agent_executor, load_chat_planner: functions for creating plan-and-execute agents
# ChatOpenAI, OpenAI: OpenAI language models
Tools
search = DuckDuckGoSearchAPIWrapper()
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",
),
]
# Create search and calculator tools
# search: use DuckDuckGo search API
# llm: create an OpenAI language model instance with temperature=0 for deterministic output
# llm_math_chain: create a math chain
# tools: define two tools, one for search and one for math
Planner, Executor, and Agent
model = ChatOpenAI(temperature=0)
planner = load_chat_planner(model)
executor = load_agent_executor(model, tools, verbose=True)
agent = PlanAndExecute(planner=planner, executor=executor)
# Create planner, executor, and agent
# model: create a ChatOpenAI model instance
# planner: load a chat planner
# executor: load an agent executor equipped with previously defined tools
# agent: create a plan-and-execute agent instance
Running an Example
agent.run(
"Who is the current prime minister of the UK? What is their current age raised to the 0.43 power?"
)
# Run the agent to answer a question about the UK prime minister
# Question: Who is the current prime minister of the UK? What is their current age raised to the 0.43 power?
Summary
This document introduces the concept, implementation, and usage examples of plan-and-execute agents. Key points include:
- Basic principles of plan-and-execute agents
- Importing required libraries
- Defining and setting up tools
- Creating the planner, executor, and agent
- Running an example to demonstrate how the agent solves a complex problem
Extended Knowledge
-
Plan-and-Execute Agents: This AI method combines planning and execution phases. It first formulates a plan and then executes it step by step, enabling it to handle more complex tasks.
-
Large Language Models (LLMs): Models like OpenAI's GPT series excel in natural language tasks, including text generation, question answering, and translation.
-
Tool Usage: In this example, the agent uses search and calculator tools, allowing it to access external information and functionality, greatly expanding its capabilities.
-
Temperature Parameter: When using language models, temperature controls output randomness. Lower temperatures (e.g., 0) produce more deterministic outputs, while higher temperatures increase diversity.
-
API Wrappers: Wrappers like
DuckDuckGoSearchAPIWrappersimplify integrating external services and APIs into AI systems.