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