{ "cells": [ { "cell_type": "markdown", "id": "79b5811c-1074-495f-9722-8325b5e717d3", "metadata": {}, "source": [ "# Plan-and-Execute\n", "\n", "This notebook shows how to create a \"plan-and-execute\" style agent. This is heavily inspired by the [Plan-and-Solve](https://arxiv.org/abs/2305.04091) paper as well as the [Baby-AGI](https://github.com/yoheinakajima/babyagi) project.\n", "\n", "The core idea is to first come up with a multi-step plan, and then go through that plan one item at a time.\n", "After accomplishing a particular task, you can then revisit the plan and modify as appropriate.\n", "\n", "\n", "The general computational graph looks like the following:\n", "\n", "\n", "\n", "\n", "\n", "This compares to a typical [ReAct](https://arxiv.org/abs/2210.03629) style agent where you think one step at a time.\n", "The advantages of this \"plan-and-execute\" style agent are:\n", "\n", "1. Explicit long term planning (which even really strong LLMs can struggle with)\n", "2. Ability to use smaller/weaker models for the execution step, only using larger/better models for the planning step\n", "\n", "\n", "The following walkthrough demonstrates how to do so in LangGraph. The resulting agent will leave a trace like the following example: ([link](https://smith.langchain.com/public/d46e24d3-dda6-44d5-9550-b618fca4e0d4/r))." ] }, { "cell_type": "markdown", "id": "a44a72d6-7e0c-4478-9d20-4c09000420a8", "metadata": {}, "source": [ "## Setup\n", "\n", "First, we need to install the packages required." ] }, { "cell_type": "code", "execution_count": 1, "id": "b451b58a-89bd-424f-8c06-0d9fe325e01b", "metadata": {}, "outputs": [], "source": [ "%%capture --no-stderr\n", "%pip install --quiet -U langgraph langchain-community langchain-openai tavily-python" ] }, { "cell_type": "markdown", "id": "35f267b0-98db-4a59-8b2c-a23f795576ff", "metadata": {}, "source": [ "Next, we need to set API keys for OpenAI (the LLM we will use) and Tavily (the search tool we will use)" ] }, { "cell_type": "code", "execution_count": 1, "id": "ce438281-08d5-4804-afe7-e4089f7b016b", "metadata": {}, "outputs": [], "source": [ "import getpass\n", "import os\n", "\n", "\n", "def _set_env(var: str):\n", " if not os.environ.get(var):\n", " os.environ[var] = getpass.getpass(f\"{var}: \")\n", "\n", "\n", "_set_env(\"OPENAI_API_KEY\")\n", "_set_env(\"TAVILY_API_KEY\")" ] }, { "cell_type": "markdown", "id": "be2d7981-3737-4134-8bef-d00d18d4e91d", "metadata": {}, "source": [ "
Set up LangSmith for LangGraph development
\n", "\n", " Sign up for LangSmith to quickly spot issues and improve the performance of your LangGraph projects. LangSmith lets you use trace data to debug, test, and monitor your LLM apps built with LangGraph — read more about how to get started here. \n", "
\n", "