LangGraph: Chat Agent Executor

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

Key Takeaways

This video demonstrates the LangGraph Chat Agent Executor, a simpler agent runtime for models that support chat messages and function calling, using tools like LangGraph, open AI, and Toil search.

Full Transcript

in this video we're going to go over the chat agent executor that we've added to L graph so the chat agent executor is basically an agent executor that works solely on a list of input messages and then basically adds messages to that list to keep track of the agent State over time this is useful and interesting because a lot of the newer models are chat based models that represent function calling and function responses as messages and so we can just append it to this list of messages and keep track of it that way compared to the other video on using L chain agents this will actually use less L chain Concepts so it will just use the open AI model um it can use any model that supports function calling but we'll use the open AI model for this for this video and it will use tools from Lang chain but it won't use the the Lang chain agent abstractions it will it will be more kind of like Bare Bones so let's take a look at setting it up we're going to require L chain package we're going to require L chain open AI package to use the open AI model and we're going to require toil package um which will be the Search tool that we'll use we'll set the API keys for that and then we'll also set up uh Lang chain uh tracing from Lang Smith so this isn't required but this is a really good way to observe what's going on under the hood with your agents so first we're going to set up the tools uh we can do that pretty easily we're going to be using toil search here and then we're just going to set up the tool executor which is a helper method to basically invoke these tools after that we're going to set up the model so what we're going to do is we're going to import the chat open AI model from the Lang chain integration um we're going to initialize it we're going to set streaming equals to true and we'll see why this is important later on but basically we can stream back tokens this way um and then what we're going to do is we're going to basically attach the functions that we want the model to have the ability to call to this object um so we can call this format tool to open AI function method which takes in the Lang chain tools and converts it to the format that the open AI functions calling expects once we do that we can Define the agent state so the agent state is the thing that is passed around and all nodes in the graph will basically update this state over time so here the state's really simple it's just going to require it's going to be a dictionary and there's going to be one key in it it's going to be this list of messages this list of messages we want to append to over time so there's two different ways that you can update the state of of you can update the state over time you can either overwrite the individual attributes or you can add to it here we want to add to it so we're going to use annotated and then this operator. add toe that any updates from nodes to this messages thing are going to add to it over time and this is nice so we just so we can just return only the new messages and not have to worry about returning the old messages plus the new messages after this we're going to define the nodes and the edges so the nodes are things that do work and then edges are basically things that connect them so we're going to want a few things here we're going we going to want an agent node that basically calls the language model and uh and and gets back a response and then we're going to want a action node which will take the the the response or the list of messages see if there's any tools that should be called call those tools and then append them to the list of messages we're also going to need a way to determine whether we want to go from the agent to the tool calling thing or to finish so remember the agent doesn't always have to call tool when it's fin when it's called as many tools as it wants to and it wants to finish it can just return directly and so we need to have a function that determines which of those paths to go down and that's called a conditional Edge we'll see that later on so here we'll first Define this function that determines which branch to go down it's going to look at the at the current list of messages if there's a function call in the last uh message then we're uh going to or sorry if there's not a function call in the last message then we're going to end if there is a function call then we're going to continue and so we'll use this to create that conditional Edge later on then we have this function that calls the model here we just get the messages pass it into the model and then append that response this response will be a single message and we'll use a list here because this is getting added to the current messages value so we need it to be something that can be added with a list and then finally we'll have this call tool node and this will take in the messages it will get the last message it will create this tool invocation um which basically just has the tool and then the tool input and we'll we'll load those from the function calling that's returned from open a function calline we will then call the tool executor with this uh with this tool invocation uh we'll create a function message and then we'll append this function message to the list of messages by returning this function message as its own list once that's done we can now Define the graph so we create a graph by passing in the agent State we defined above we then create two nodes the agent node and the action node we then set the entry point to be the agent node and so this is basically saying as soon as the input gets in the first node that we're going to send it to is the agent node we're then going to add a conditional Edge after the agent is called so after the agent is called we either want to go to the action node or we want to finish and we want to do that based on the logic that's up here in the should continue function so we're going to P we're going to add conditional edges from the agent node it's going to use continue as the function and then basically we're going to pass in a mapping from string to some other node name so continue and end these are the two values that are returned from the should continue function and basically if continue is called then we're going to go to the action node that we defined above if end is called then we're going to go to this end node and this is a special node that denotes the end of agent now we can add an edge from the action node to the agent node and we're going to do this because we always want to go to the agent node after we call an action and then we can compile this graph into something that we can use uh like a l chain runnable and so we'll expose a lot of similar interfaces as Lang chain runnables do and we'll we'll use these below so in order to use it we need to create our input our input is going to be a dictionary it's going to have a messages key and that messages key is just going to be a list of messages here I just have one human message but I can easily add a system message um a system message a human message an AI message and then another human message anything there so I can have full control over the list of inputs it's just a list of messages once I call it I need to run the above cell once I call it um it will take a little bit because it's doing a bunch of calls under the hood um but it will eventually return an answer and it will return uh an updated thing with this messages and this messages will be the list it has in the human message that we passed in uh and then uh uh the AI message which is the first call that it made a function message which is the result and then an AI message which is the fin final result if we want to see what's going on under the hood one way that we can do that is with L Smith so we can go in here and we can see that there's a few things that are happening under the hood first we're calling open AI the inputs this list of messages which is just this human message it gets back this function call thing um we then call this uh we then go to the action node and we call toil search and so we get back this list of results and then we have another call to the agent where we have this list of messages as input and we have this output so this shows what's going on under the hood as you may have noticed it took a little bit to get this and so one of the things that's really nice about L graph is it also has a few different streaming capabilities and that's what we'll cover in the next video

Original Description

This is a version of an agent executor specifically designed for chat models. It represents state as a list of messages and passes that around. This is a simpler agent runtime for models that support chat messages and function calling. Notebook: https://github.com/langchain-ai/langgraph/blob/main/examples/chat_agent_executor_with_function_calling/base.ipynb
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from LangChain · LangChain · 49 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
48 LangGraph: Agent Executor
LangGraph: Agent Executor
LangChain
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 use LangGraph to build a chat agent executor, which can be used to create autonomous workflows and multi-agent systems. It covers the basics of chat agents and how to use open AI and Toil search for chat models.

Key Takeaways
  1. Define a function to determine which path to go down based on the current list of messages
  2. Create a function that calls the model and appends the response to the list of messages
  3. Create a call tool node that takes in the messages, gets the last message, creates a tool invocation, and calls the tool executor
  4. Define the graph by creating nodes, setting the entry point, and adding conditional edges based on the logic of the should continue function
  5. Compile the graph into a chain runnable and expose interfaces similar to Lang chain runnables
💡 LangGraph provides a simpler agent runtime for models that support chat messages and function calling, making it easier to build autonomous workflows and multi-agent systems.

Related Reads

📰
A Builder’s Map of Open-Source Tools for Shipping AI Agents
Learn to navigate open-source tools for building AI agents with a 9-layer capability map
Medium · LLM
📰
Building a Production AI Agent in Node.js: Tool Calling, the ReAct Loop, and Error Handling
Learn to build a production-ready Node.js AI agent with robust features like tool calling, ReAct loop, and error handling to handle real-world traffic
Dev.to · ZyVOP
📰
HarnessX: When the Harness Starts Learning From Its Own Runs
Learn about HarnessX, a new approach to harness engineering that enables learning from its own runs, and its potential to revolutionize AI development
Medium · AI
📰
🔬 The Coolest Diffusion Research Isn't in LLMs — Evan Feinberg & Sergey Edunov, Genesis Molecular AI
Discover how diffusion research in AI is being applied to drug discovery, enabling breakthroughs in molecular binding and structure prediction, which can revolutionize the field of medicine
Latent Space
Up next
Is your company truly AI-native or just dabbling? The answer changes everything.
AI InterConnect
Watch →