Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Plan-and-Execute Agents: Concepts, Implementation, and Usage Examples

Tech May 16 1

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:

  1. Basic principles of plan-and-execute agents
  2. Importing required libraries
  3. Defining and setting up tools
  4. Creating the planner, executor, and agent
  5. Running an example to demonstrate how the agent solves a complex problem

Extended Knowledge

  1. 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.

  2. Large Language Models (LLMs): Models like OpenAI's GPT series excel in natural language tasks, including text generation, question answering, and translation.

  3. Tool Usage: In this example, the agent uses search and calculator tools, allowing it to access external information and functionality, greatly expanding its capabilities.

  4. Temperature Parameter: When using language models, temperature controls output randomness. Lower temperatures (e.g., 0) produce more deterministic outputs, while higher temperatures increase diversity.

  5. API Wrappers: Wrappers like DuckDuckGoSearchAPIWrapper simplify integrating external services and APIs into AI systems.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.