LangGraph: Agent Executor

LangChain · Intermediate ·🤖 AI Agents & Automation ·2y ago

Key Takeaways

The video demonstrates how to recreate the LangChain AgentExecutor functionality in LangGraph, utilizing tools such as LangChain, LChain OpenAI, Toil Python, and Toil Search tool to enable agent execution and autonomous workflows. It covers the setup of the LangChain agent, definition of graph state, and the use of agent outcome, agent action, and agent finish to represent tool and final result.

Full Transcript

all right so in this video we're going to go over how to build the equivalent of the current Ling chain agent executive from scratch in Lane graph and we'll see how easy it is so in order to do this uh we're first going to set everything up we need to install a few packages we need to install a lang chain so we'll use this for to use the existing agent classes in Lang chain we can still easily use those agent classes in L graph we'll also install L chain open AI so that we can use the open AI package um and we'll use that to power our agent and then we'll also install toil python this will power the Search tool that we'll use as one of our tools for the agent um after we do that we'll set the open AI API key we'll set the tavil API key um and then we'll also set the link Smith uh API key and so uh here these two variables link chain tracing V2 and Link train API key will uh if you set those those will set things up so things get start getting logged to link Smith which is our observability platform and so you can see the uh the the instructions here and you'll also need to grab an Epi key from here if you don't have access to Lang Smith yet it's currently in a private beta just DM me on Twitter or LinkedIn and we can get you that going back to this notebook the first thing we're going to do is create the Lang chain agent and so this is the exact same code that we used in Lang chain so I'm not going to go over into too much detail if you want more information on that check out the link chain documentation but basically we'll create a tool which is this toil Search tool we'll get our prompt which we're pulling from The Hub we'll choose uh the llm that we want to use which is this uh which is this open AI LM and then we'll create this open AI functions agent which is a particular type of agent we will then um Define the graph state so this is the state that's going to be tracked throughout the graph over time and basically the reason that this is important is that once we establish the state each node can basically push updates to that state so we don't have to pass around this state from node to node all the time rather what we can do is we can just pass updates to that state as part of that we need to specify what type of updates push the that we're pushing to that state so by default the updates will basically override the existing attribute for that state that's useful in some cases but in other cases you want to actually add to the existing uh state that exists and we'll see an example of that here and so again default will override but often times you want to add to that to the to the attribute for that state and so if we look at the existing agent State we can see here that the first two things are basically inputs um so the input uh message to the conversation and then the chat history if if there happens to be any chat history and so those will be things that will pass in as we see later on the next two are things that the graph will add over time so agent outcome will be set by a few nodes or by one node in particular when after the agent is called and then that will be basically the uh basically tool that it should call or the final result that it should uh pass and so we have agent action and agent finish to basically represent that tool and then the final result we also have none um because that's what this is Will default to when it starts so this will be none to start finally we have a list of the steps that the agent has taken thus far so this is one of the ones that we don't want to override this but rather we want to append to this over time and keep on growing this and so here we've annotated this with the add operator and so this means that anytime a node writes to this attribute it will basically add it to the existing value rather than overwrite it and we type this as a list of tupal of agent actions and strings these are the these are how intermediate steps are represented in the current Lang chain agents so we can run that then then what we need to do is Define the nodes so here we uh and Define the nodes and Define the edges so first we really have a need for two nodes first the agent node which uses the agent to determine what action to take and then basically the node that invokes the tools so takes in uh the the agent decision calls that tool and then and then does something with that besides those nodes we also need to add some edges so there's two types of edges basically a condition Edge which will uh kind of it's basically one node leads to a fork in the road and there's two different directions or three different directions or four different directions but basically there's different edges that it could take conditional on kind of like the the result of the previous node um and so we'll see an example of adding that the example that we'll add is basically based on the agent outcome we either want to call a tool or we want to return to the user and so that'll be the forking decision that we have to make then there's a normal Edge and this is where this always happens um and so an example of that is after we call a tool we always want to return back to the agent to let it to decide what to do next so let's take a look at the nodes that we have here first we have this run agent node which calls the agent it takes in the data calls agent runnable invoke and then assigns this to the agent outcome so this will override the existing value of agent outcome and and put this new output there then we have this execute tools function which takes in the data gets the current agent outcome executes it with this tool executor which is a nice little helper function we've made to just easily basically say uh you know to run this tool with this function um and then it will return intermediate steps remember intermediate steps is the one that we're adding to so here we Define a list of this agent action and then the output cast to a string and this list will get appended to the existing list and then finally we add this function should continue and this is basically going to be used to create that that conditional um node um or conditional Edge so we look at this agent outcome and if it's a finish if it's agent finish then we return end and if otherwise we return continue and we'll see how we use these later on when we construct the graph which we'll do right now so first we Define a new graph so we import State graph from Lang graph. graph and we pass in agent state which is the state that we defined above we then add the two nodes so we add them by specifying the name of the node as a string and then the function and this can be a function or a runnable um either or um and so you can spe spe ify that here and the reason that we do this is so then that we can refer to this node uh down below we set the entry point as agent so this is using the same string that we set here and this is basically just saying this will be the first node that is called when input gets in we then add a conditional Edge so first we Define the uh start so this is saying after the agent uh is after the agent node is finished running what we'll do is we'll call this function the should continue function so this should continue function gets the output of uh anything after the agent node is called and then it will look at the data and return end or continue and so if we look down here the last thing we pass into this is this mapping of string to string and so basically we have this key this key should match the output of should continue and so if should continue returns continue then we call the action node which we defined above if it returns end then we call the special end node um which is a built-in node that denotes that we should end and return to the user we then add a normal Edge from action back to agent so this is after the tool is called we then return back to the agent and then finally we compile the graph and this basically converts this graph structure into a l chain runnable that we can then use so we can use invoke we can use stream and everything with it taking a look at what this looks like we can now call we can run this and now we can call uh it with some inputs so remember we need the input key and we need chat history as the as the as the two inputs um and we'll use the stream method after this and this will print out the the results of each node so we can see here that we get the agent outcome um and so this is the the first the the the output of the agent node we get back this uh thing that says use tool to V search results Json with this tool input query whether in San Francisco we then get back intermediate steps and so the intermediate steps are tupal of this of this uh tool um with the results of calling that tool and then we get back uh this a new agent outcome and this is uh this is calling agent finish so this is saying we're done with the agent and then this is the final thing that's returned which is just basically the whole state so it includes the input the chat history the current agent outcome and then any intermediate St steps we can see what this looks like in link Smith for perhaps a better example so if we click into Lane graph we can see that it starts um it then calls the agent and under the a under the hood the agent's calling open Ai and so we can see the exact prompt that goes in um and so we can see we get back this function call we can then see the function call to under in this action um and here we get back this result and we can see it goes back into agent after that calls open AI again it's now got this stuff in it and it says that that it gets some response back so that's basically how to use the uh or how to create an agent executor very similar to the existing agent executor in linkchain from scratch which lane graph um we'll go over a few additional things in future videos like one we'll do a deeper dive on the the the interface that this state graph exposes and then two we'll cover streaming more and different ways that you can stream results back

Original Description

In this video we will go over how to re-create the canonical LangChain "AgentExecutor" functionality in LangGraph. The benefits of doing it this way are that it will be much easier to modify parts of it to fit your own custom logic. Notebook: https://github.com/langchain-ai/langgraph/blob/main/examples/agent_executor/base.ipynb LangChain Agents: https://python.langchain.com/docs/modules/agents/
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 48 of 60

1 Chat With Your Documents Using LangChain + JavaScript
Chat With Your Documents Using LangChain + JavaScript
LangChain
2 LangChain SQL Webinar
LangChain SQL Webinar
LangChain
3 LangChain "OpenAI functions" Webinar
LangChain "OpenAI functions" Webinar
LangChain
4 LangSmith Launch
LangSmith Launch
LangChain
5 LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain x Pinecone: Supercharging Llama-2 with RAG
LangChain
6 LangChain Expression Language
LangChain Expression Language
LangChain
7 Building LLM applications with LangChain with Lance
Building LLM applications with LangChain with Lance
LangChain
8 Benchmarking Question/Answering Over CSV Data
Benchmarking Question/Answering Over CSV Data
LangChain
9 LangChain "RAG Evaluation" Webinar
LangChain "RAG Evaluation" Webinar
LangChain
10 Fine-tuning in Your Voice Webinar
Fine-tuning in Your Voice Webinar
LangChain
11 Tabular Data Retrieval
Tabular Data Retrieval
LangChain
12 Building an LLM Application with Audio by AssemblyAI
Building an LLM Application with Audio by AssemblyAI
LangChain
13 Superagent Deepdive Webinar
Superagent Deepdive Webinar
LangChain
14 Lessons from Deploying LLMs with LangSmith
Lessons from Deploying LLMs with LangSmith
LangChain
15 Shortwave Assistant Deepdive Webinar
Shortwave Assistant Deepdive Webinar
LangChain
16 Cognitive Architectures for Language Agents
Cognitive Architectures for Language Agents
LangChain
17 Effectively Building with LLMs in the Browser with Jacob
Effectively Building with LLMs in the Browser with Jacob
LangChain
18 Data Privacy for LLMs
Data Privacy for LLMs
LangChain
19 "Theory of Mind" Webinar with Plastic Labs
"Theory of Mind" Webinar with Plastic Labs
LangChain
20 LangChain Templates
LangChain Templates
LangChain
21 Using Natural Language to Query Postgres with Jacob
Using Natural Language to Query Postgres with Jacob
LangChain
22 Building a Research Assistant from Scratch
Building a Research Assistant from Scratch
LangChain
23 Benchmarking RAG over LangChain Docs
Benchmarking RAG over LangChain Docs
LangChain
24 Skeleton-of-Thought: Building a New Template from Scratch
Skeleton-of-Thought: Building a New Template from Scratch
LangChain
25 Benchmarking Methods for Semi-Structured RAG
Benchmarking Methods for Semi-Structured RAG
LangChain
26 LangSmith Highlights: Getting Started
LangSmith Highlights: Getting Started
LangChain
27 LangSmith Highlights: Debugging
LangSmith Highlights: Debugging
LangChain
28 LangSmith Highlights: Datasets
LangSmith Highlights: Datasets
LangChain
29 LangSmith Highlights: Evaluation
LangSmith Highlights: Evaluation
LangChain
30 LangSmith Highlights: Human Annotation
LangSmith Highlights: Human Annotation
LangChain
31 LangSmith Highlights: Monitoring
LangSmith Highlights: Monitoring
LangChain
32 LangSmith Highlights: Hub
LangSmith Highlights: Hub
LangChain
33 SQL Research Assistant
SQL Research Assistant
LangChain
34 Getting Started with Multi-Modal LLMs
Getting Started with Multi-Modal LLMs
LangChain
35 Build a Full Stack RAG App With TypeScript
Build a Full Stack RAG App With TypeScript
LangChain
36 Auto-Prompt Builder (with Hosted LangServe)
Auto-Prompt Builder (with Hosted LangServe)
LangChain
37 LangChain v0.1.0 Launch: Introduction
LangChain v0.1.0 Launch: Introduction
LangChain
38 LangChain v0.1.0 Launch: Observability
LangChain v0.1.0 Launch: Observability
LangChain
39 LangChain v0.1.0 Launch: Integrations
LangChain v0.1.0 Launch: Integrations
LangChain
40 LangChain v0.1.0 Launch: Composability
LangChain v0.1.0 Launch: Composability
LangChain
41 LangChain v0.1.0 Launch: Streaming
LangChain v0.1.0 Launch: Streaming
LangChain
42 LangChain v0.1.0 Launch: Output Parsing
LangChain v0.1.0 Launch: Output Parsing
LangChain
43 LangChain v0.1.0 Launch: Retrieval
LangChain v0.1.0 Launch: Retrieval
LangChain
44 LangChain v0.1.0 Launch: Agents
LangChain v0.1.0 Launch: Agents
LangChain
45 Build and Deploy a RAG app with Pinecone Serverless
Build and Deploy a RAG app with Pinecone Serverless
LangChain
46 Hosted LangServe + LangChain Templates
Hosted LangServe + LangChain Templates
LangChain
47 LangGraph: Intro
LangGraph: Intro
LangChain
LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
49 LangGraph: Chat Agent Executor
LangGraph: Chat Agent Executor
LangChain
50 LangGraph: Human-in-the-Loop
LangGraph: Human-in-the-Loop
LangChain
51 LangGraph: Dynamically Returning a Tool Output Directly
LangGraph: Dynamically Returning a Tool Output Directly
LangChain
52 LangGraph: Respond in a Specific Format
LangGraph: Respond in a Specific Format
LangChain
53 LangGraph: Managing Agent Steps
LangGraph: Managing Agent Steps
LangChain
54 LangGraph: Force-Calling a Tool
LangGraph: Force-Calling a Tool
LangChain
55 LangGraph: Multi-Agent Workflows
LangGraph: Multi-Agent Workflows
LangChain
56 Streaming Events: Introducing a new `stream_events` method
Streaming Events: Introducing a new `stream_events` method
LangChain
57 Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
Building a web RAG chatbot: using LangChain, Exa (prev. Metaphor), LangSmith, and Hosted Langserve
LangChain
58 OpenGPTs
OpenGPTs
LangChain
59 Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
Open Source RAG with Nomic's New Embedding Model (and ChromaDB and Ollama)
LangChain
60 LangGraph: Persistence
LangGraph: Persistence
LangChain

This video teaches how to recreate the LangChain AgentExecutor functionality in LangGraph, enabling the creation of custom agent execution environments and autonomous workflows. It covers the setup of LangChain agents, definition of graph state, and the use of condition edges and normal edges. By following this lesson, viewers can learn how to create complex multi-agent systems and workflows using LangGraph.

Key Takeaways
  1. Create the LangChain agent
  2. Set up OpenAI API key
  3. Set up Tavil API key
  4. Set up Link Smith API key
  5. Define the graph state
  6. Define the nodes and edges in the graph
  7. Add the agent node and the node that invokes the tools
  8. Add a condition edge based on the agent outcome
  9. Add a normal edge for actions that always happen
  10. Use the should continue function to create a conditional node or edge based on the agent outcome
💡 The LangGraph agent executor can be used to call tools, such as OpenAI, and stream results back, enabling the creation of complex multi-agent systems and autonomous workflows.

Related Reads

Up next
How to Clean Up Your Backlog with AI & MCP | Businessmap Quick Tips
Businessmap
Watch →