LangGraph: Multi-Agent Workflows

LangChain
23 Jan 202424:01

Summary

TLDRIn this video, Harrison from Lang introduces L Graph, a library for creating dynamic, multi-agent workflows as graphs. He explains the concept of using L Graph to define agent-like workflows with cycles, akin to state machines, and demonstrates three variants: multi-agent collaboration, agent supervision, and hierarchical agent teams. Each variant showcases different structures for running language models in loops, emphasizing the global state sharing and independent operation capabilities of agents within complex workflows.

Takeaways

  • 🌟 Lang graph is a new library released by Lang for creating dynamic, agent-like workflows as graphs.
  • 🔁 The concept of an agent-like workflow involves running a language model in a loop with various structures, allowing for the definition of cycles.
  • 🤖 L graph is useful for defining the structure of multi-agent workflows, where nodes represent agents and edges represent communication between them.
  • 📚 The video covers three variants of multi-agent workflows: multi-agent collaboration, agent supervisor, and hierarchical agent team.
  • 👥 In multi-agent collaboration, agents share the same state of messages and work together, possibly using shared tools or passing final responses.
  • 🛠️ The example given for multi-agent collaboration involves a researcher agent and a chart generator agent, each with different tools and a shared state.
  • 🔍 Lang Smith is a tool used for tracking and debugging multi-agent workflows, providing insights into the internal processes of the workflow.
  • 👮 The agent supervisor model routes between different independent agents, with each agent working on its own state before giving a final answer to the supervisor.
  • 🏢 Hierarchical agent teams consist of smaller teams of agents, each with their own set of tools and workflows, managed by a higher-level supervisor.
  • 📈 Lang Smith provides a detailed view of the workflow's execution, including the ability to see each agent's calls and responses, aiding in understanding complex tasks.
  • 🛑 The hierarchical approach allows for compartmentalization of functions, focusing each agent on specific tasks, which can be beneficial for complex workflows.

Q & A

  • What is Lang Graph and what does it enable users to do?

    -Lang Graph is a library recently released that allows users to dynamically create agent-like workflows as graphs. It enables the definition of cycles and is useful for defining the structure of workflows, including how different agents communicate with each other.

  • How is Lang Graph similar to a state machine?

    -Lang Graph is similar to a state machine in that both involve nodes and transitions. In the context of Lang Graph, nodes can represent states or agents, and the edges represent the transitions or communication between these states or agents.

  • What are the three variants of multi-agent workflows discussed in the script?

    -The three variants of multi-agent workflows discussed are multi-agent collaboration, agent supervisor, and hierarchical agent team. Each variant represents a different way of structuring the interaction and tasks among multiple agents.

  • What is the role of the researcher agent in the multi-agent collaboration example?

    -In the multi-agent collaboration example, the researcher agent is responsible for querying data and calling tools as needed. It works in conjunction with the chart generator agent, sharing state and messages to collaboratively complete a task.

  • How does the chart generator agent respond in the multi-agent collaboration workflow?

    -The chart generator agent responds by examining the accumulated state and messages. It takes action after receiving a normal message with no tool calls or final answer from the researcher agent, such as generating a chart based on the data provided.

  • What is Lang Smith and how is it used in the script?

    -Lang Smith is a tool used for tracking and debugging multi-agent workflows. It is used in the script to monitor the interactions between agents, providing a detailed view of the workflow's progress and helping to understand what each agent is doing at any given time.

  • What is the purpose of the supervisor agent in the agent supervisor variant?

    -The supervisor agent in the agent supervisor variant is responsible for routing tasks between different independent agents. It manages the conversation between the agents, deciding which agent to pass the task to based on the final responses from the agents.

  • How does the hierarchical agent team variant differ from the other multi-agent workflows?

    -The hierarchical agent team variant differs by having each agent node act as a supervisor agent setup, creating a more complex structure with multiple layers of supervision and task routing. This allows for a more intricate division of tasks and responsibilities among the agents.

  • What is the significance of the global state of messages in the multi-agent collaboration?

    -The global state of messages in multi-agent collaboration is significant because it allows each agent to see and build upon the steps taken by other agents. This shared context enables a high level of collaboration and coordination among the agents.

  • How does the hierarchical structure in the hierarchical agent team variant benefit complex tasks?

    -The hierarchical structure in the hierarchical agent team variant benefits complex tasks by compartmentalizing functions and focusing each agent on specific tasks. This organization can improve efficiency and clarity in managing complex workflows.

Outlines

00:00

🚀 Introduction to Lang Graph for Multi-Agent Workflows

Harrison introduces Lang graph, a library for creating dynamic, agent-like workflows as graphs. The library allows for the definition of cycles and is likened to state machines, with nodes representing agents and edges as communication pathways. The video will cover three variants of multi-agent workflows using Lang graph, starting with multi-agent collaboration where multiple agents share the same state of messages. The example given involves two agents: a researcher and a chart generator, each with specific tools and a structured interaction pattern. Lang Smith is mentioned as a tool for tracking and debugging multi-agent workflows.

05:00

🔍 Setting Up Multi-Agent Collaboration with Lang Graph

This section delves into the technical setup for multi-agent collaboration using Lang graph. It involves defining the state to track messages and the most recent sender, creating agent nodes with helper functions, and establishing tool nodes for executing tasks. The routing logic for the workflow is explained, which dictates the flow based on the content of messages. The setup culminates in the creation of a graph that incorporates agents, nodes, and conditional edges, with an example of invoking the graph to fetch and visualize UK GDP data.

10:02

🛠️ Exploring Agent Supervisor and Independent Agent Workflows

The script moves on to discuss the 'agent supervisor' model, where a supervisor agent routes tasks between independent agents, each with their own final output. This is contrasted with the collaborative model, highlighting the supervisor's oversight of separate agent tasks. The process involves creating tools, defining agents with their respective nodes, and constructing a graph that includes the supervisor and its decision logic. The supervisor's workflow is demonstrated with an example of generating a brief research report.

15:03

🏢 Constructing Hierarchical Agent Teams with Lang Graph

The script introduces the concept of hierarchical agent teams, where each agent node operates as a mini-supervisor within a larger team structure. This setup is more graph-oriented and less chain-oriented compared to previous examples. The process includes setting up tools for different teams, creating agents and nodes with specialized functions, and defining team supervisors. The construction of a complex graph that integrates research and document writing teams is outlined, with an example of generating a research report on North American sturgeon.

20:04

🌐 Conclusion on Multi-Agent Workflows with Lang Graph

In conclusion, Harrison emphasizes the benefits of using a multi-agent approach with Lang graph for compartmentalizing tasks and focusing each agent on specific functions. This method is presented as advantageous for complex tasks and as a useful mental model for system organization. The video ends with an expression of excitement for the potential applications and workflows that might emerge using Lang graph.

Mindmap

Keywords

💡Lang graph

Lang graph is a library introduced in the video for creating dynamic agent-like workflows as graphs. It is central to the video's theme as it provides a framework for structuring multi-agent interactions. The script describes it as a tool for defining cycles and transitions, akin to state machines, with nodes representing agents and edges as communication pathways.

💡Multi-agent workflows

Multi-agent workflows refer to processes involving multiple agents or entities working towards a common goal. In the context of the video, these workflows are created using Lang graph, where each agent has a specific role and interacts with others through defined communication channels, exemplified by the setup where a researcher and a chart generator collaborate.

💡State machines

State machines are a computational model used to represent a system's behavior as a sequence of states and transitions between them. The video relates state machines to Lang graph by drawing a parallel between nodes in a graph and states in a state machine, emphasizing the structured and predictable nature of the workflows.

💡Cycles

Cycles in the video refer to the iterative processes within a workflow where agents repeatedly perform tasks. Lang graph allows for the definition of such cycles, enabling continuous and looped interactions between agents, which is crucial for the dynamic creation of workflows.

💡Language model

A language model in the script represents an AI system that processes natural language input and generates relevant responses. It is used within agents to perform tasks such as research or data analysis, and its outputs are fed into the workflow through the Lang graph structure.

💡L graph

L graph is a term used in the video to describe the combination of Lang graph and L chain, which is another library. It seems to refer to the overall framework for creating and executing multi-agent workflows, as the script mentions using L graph syntax to define and track state over time.

💡L chain

L chain is mentioned as a library that works in conjunction with Lang graph. It is implied to be a part of the toolset used to create and manage the workflows, particularly in the context of defining and executing agent behaviors within the graph.

💡Agent collaboration

Agent collaboration is a specific type of multi-agent workflow where agents share state and work interdependently. The video script provides an example of this with two agents—a researcher and a chart generator—sharing information and contributing to a collective output.

💡Global state

The global state in the video refers to the shared context or information that all agents in a workflow can access and contribute to. It is a key aspect of multi-agent collaboration, allowing agents to stay informed about the overall progress and decisions made by others.

💡Lang Smith

Lang Smith is a tool mentioned for debugging and visualizing the execution of multi-agent workflows. It allows users to track the state and messages of agents over time, providing insights into the workflow's operation, as demonstrated when the video script discusses checking the results in Lang Smith.

💡Hierarchical agent team

A hierarchical agent team is a more complex structure within multi-agent workflows, where agents are organized in a hierarchy with supervisor agents overseeing sub-teams of worker agents. The video script describes this as a way to compartmentalize tasks and manage complex workflows effectively.

Highlights

Introduction to Lang graph, a new library for creating multi-agent workflows.

Explanation of Lang graph as a dynamic method to create agent-like workflows as graphs.

Definition of cycles in L graph, essential for loop creation in workflows.

Comparison of L graph to state machines and labeled directed graphs for workflow structuring.

Description of nodes as agents and edges as communication pathways in multi-agent workflows.

Introduction to three variants of multi-agent workflows using L graph.

Explanation of multi-agent collaboration with shared state and message passing.

Demonstration of setting up a multi-agent collaboration with two agents: a researcher and a chart generator.

Use of Lang Smith for tracking and debugging multi-agent workflows.

Creation of a graph with defined state, nodes, and conditional edges for multi-agent workflows.

Example of a multi-agent workflow to fetch UK's GDP data and generate a line graph.

Introduction to the concept of an agent supervisor routing between independent agents.

Description of hierarchical agent teams and their setup in L graph.

Example of creating a research team and document writing team as hierarchical agent nodes.

Demonstration of a complex task involving a research report and chart generation using hierarchical teams.

Lang Smith's utility in visualizing and understanding the process of complex multi-agent workflows.

Discussion on the benefits of compartmentalizing functions in multi-agent systems for complex tasks.

Conclusion emphasizing the flexibility and potential of L graph for creating various multi-agent workflows.

Transcripts

play00:00

hey everyone this is Harrison from Lang

play00:03

chain here I wanted to do a video on

play00:06

Lang graph which is a new library we

play00:09

released um pretty recently and how you

play00:12

can use l graph to create some

play00:15

multi-agent workflows so for those of

play00:18

you who aren't caught up on L graph we

play00:20

have a whole series of videos going

play00:21

through it and it's basically a way to

play00:24

dynamically

play00:26

create agent-like workflows as graphs so

play00:30

what do I mean by that by an agent-like

play00:31

workflow I I generally mean running a

play00:33

language model in a loop in a variety of

play00:36

ways um and there's different structure

play00:39

and how you might want to run that

play00:41

language model in a loop and so L graph

play00:43

is useful for defining that structure

play00:45

and specifically it allows for the

play00:47

definition of Cycles because this is a

play00:49

loop after all um another way to think

play00:52

about this um is thinking about it as a

play00:54

way to define kind of like State

play00:56

machines um and and so a state machine

play00:59

machine and a labeled directed graph are

play01:03

pretty similar and that you have

play01:04

different nodes and if if you think of

play01:06

this as a state machine then a node

play01:08

would be a state and if you think of it

play01:10

um as as uh uh kind of like in this

play01:12

multi-agent workflow then that node

play01:15

state is an agent um and then you have

play01:17

these edges between them and so in a

play01:20

graph that's an edge in a state machine

play01:22

that is uh you know transition

play01:24

probabilities and when you think of

play01:25

these multi-agent workflows then those

play01:27

are basically how do these agents

play01:29

communicate to each other so we'll think

play01:31

about building multi-agent workflows

play01:33

using L graph the nodes are the agents

play01:36

the edges are how they communicate um if

play01:38

you haven't already checked out L graph

play01:40

again highly recommend that you check

play01:41

out the this the video that we have at a

play01:43

high level the syntax looks like this

play01:45

it's very similar to network X you

play01:47

define a graph that tracks some State

play01:49

over time you then add nodes into that

play01:51

graph you then Define edges between

play01:53

those nodes and then you can use it um

play01:56

like you would uh any other um L chain

play02:00

chain so today I want to talk about

play02:03

three different variants of multi-agent

play02:05

workflows that we're going to use laying

play02:09

graph to create so the first one is what

play02:12

we're calling multi-agent

play02:14

collaboration and so this is largely

play02:17

defined as you have multiple agents

play02:19

working on the same state of messages so

play02:23

when you think about multiple agents

play02:25

collaborating they can either kind of

play02:26

like share State between them or they

play02:28

can be kind of like independent and work

play02:31

on their own and then maybe pass like

play02:33

final responses to the other and so in a

play02:35

multi-agent collaboration they share the

play02:37

state and so specifically this this

play02:39

example that we've set up it has two

play02:41

different agents and these are basically

play02:43

prompts plus llms so there's a prompts

play02:46

plus an llm for a researcher and there's

play02:48

a prompt plus an llm for a chart

play02:50

generator they each have a few different

play02:52

tools that they can call but basically

play02:53

the way that it works is we first call

play02:55

the researcher uh node or the researcher

play02:58

agent we get a message

play03:00

and then from there there's three

play03:01

different ways that it can go one if the

play03:04

researcher says to call a function then

play03:06

we call that tool and then we go back to

play03:08

the researcher two if the researcher

play03:11

says a message that that says final

play03:14

answer so we in the prompt we say like

play03:16

if you're done say final answer so if

play03:18

the researcher says final answer then it

play03:19

returns to the user and then three if it

play03:21

just sends a normal message there's no

play03:23

tool calls and there's no final answer

play03:26

then we let the chart generator take a

play03:28

look at the state that's accumulated

play03:30

um and kind of uh respond after that so

play03:34

we're going to we're going to walk

play03:35

through what this looks like um we're

play03:38

first going to set up everything we've

play03:40

need I've already done this um

play03:42

importantly we're going to be using Lang

play03:44

Smith here to kind of like track the the

play03:46

multi-agents and see what's going on for

play03:48

for a really good debugging experience

play03:51

um Lang Smith right now is in private

play03:52

beta if you don't have access shoot me a

play03:54

DM on LinkedIn or Twitter and we can get

play03:56

you access so the first thing we're

play03:59

going to do do is we're going to Define

play04:00

this helper function that creates an

play04:02

agent so basically we're going to create

play04:04

uh these two kind of like agents up here

play04:08

um and these agents are parameterized by

play04:10

an llm the tools they have and a system

play04:13

message and so then we're taking kind of

play04:15

like uh we're basically creating this

play04:18

mini chain this very simple mini chain

play04:21

that is a prompt which has uh the system

play04:23

message in it um and then it's a it's a

play04:26

call to the llm and it has access to

play04:29

some tool so let's define this helper

play04:31

function we're then going to define the

play04:33

tools that we want the agents to be able

play04:35

to call so we'll have one Search tool

play04:38

we'll use tavil search for that and then

play04:40

we'll have one python tool that can run

play04:42

python

play04:44

code um now we can start to create our

play04:46

graph so first we're going to define the

play04:49

state of what we want to track so we

play04:51

want to track the messages and so again

play04:52

like each agent will add a message to

play04:54

this messages property and then we also

play04:56

want to track who the most recent sender

play04:58

is and the reason reason we need this

play05:00

info is because after we call this tool

play05:03

we're going to check who the sender was

play05:05

and we're going to return to them so

play05:06

this is just some some State that's

play05:08

useful to track over

play05:10

time um so let's define this state um

play05:14

we're now going to Define some agent

play05:16

nodes so here we've created a little

play05:19

helper function to create an agent node

play05:21

and so specifically this agent node is

play05:23

going to uh take in state agent name

play05:26

it's going to call the agent on the

play05:28

state and it's then going to look at the

play05:30

uh uh result we need to do a little bit

play05:33

of kind of like converting here so the

play05:35

agent is going to respond with an AI

play05:37

message but when we add that to the

play05:39

messages States that's accumulated over

play05:40

time we want to represent that as a

play05:42

human message actually um because we

play05:45

basically want the next AI that sees

play05:48

this to kind of like work with that and

play05:51

so we're going to if it's a function

play05:53

message then we're going to represent it

play05:54

as a function message because it will

play05:56

just say a function message otherwise

play05:57

we're going to cast it to a human

play05:58

message

play06:00

and we're going to give it a specific

play06:01

name we're going to give it the name of

play06:02

the agent so that it knows basically is

play06:06

this message that it sees again because

play06:09

we're keeping track of This Global State

play06:10

and so in the global State when it sees

play06:12

a human message is it from this

play06:14

researcher or is it from this charge

play06:17

generator um and so we're going to do

play06:19

that and we're going to create two nodes

play06:20

we're going to create this research

play06:21

agent node um by basically partialing

play06:24

out the agent node function um and then

play06:26

we're going to create this chart

play06:27

generator

play06:28

node

play06:31

we're now going to define the tool node

play06:32

this is the node that just runs the

play06:33

tools it basically looks at the most

play06:35

recent message it loads the tool

play06:38

arguments it kind of it will call it it

play06:41

will call the tool executor the tool

play06:42

executor is just a simple wrapper around

play06:44

tools that makes it easy to call them

play06:46

and then it will convert the response

play06:47

into a function message and append that

play06:50

um to the messages

play06:53

property and now we need the edge logic

play06:55

so this is the this is the uh big router

play06:58

logic here um which basically defines uh

play07:02

some of the logic of where to go

play07:05

so we look at the messages we look at

play07:08

the last message if there's a function

play07:10

call in the last message then we go to

play07:13

the call tool or then we return call

play07:15

tool and we'll see what that leads to

play07:16

later on if there's final answer in the

play07:19

last message then we return end um

play07:21

otherwise we return continue and so

play07:23

let's see how we use this in the graph

play07:25

so first we create this graph let me run

play07:27

this m first first we create this graph

play07:30

with the agent State then we add the

play07:32

three nodes that we have the research

play07:33

node the chart node and the tool node um

play07:36

we then add this conditional Edge so the

play07:39

researcher after we call the researcher

play07:41

um it uses the router logic if if the

play07:44

router returns continue then we go to

play07:46

chart generator so if the researcher

play07:48

returns basically something that doesn't

play07:51

have a tool and something that isn't

play07:53

final answer then we return continue in

play07:55

which case we go to the chart generator

play07:57

if the router returns call tool then we

play07:59

call the tool and if it returns end then

play08:01

we end similar with the chart generator

play08:04

um and then we add this conditional Edge

play08:06

after call tool so after we call the

play08:08

tool we basically look at who the sender

play08:10

is and if the sender is the researcher

play08:12

then we go to the researcher if the

play08:14

sender is the charge generator then we

play08:15

go to the chart generator and we set the

play08:17

entry point to be the researcher

play08:19

node now we can invoke it um we'll use

play08:23

the stream method so that we can see

play08:24

things being printed out um we'll pass

play08:27

in this human message asking it to to

play08:29

fetch the UK's GDP over the past 5 years

play08:31

then draw a line graph of it um and then

play08:34

after we code it finish it um and we'll

play08:36

run this it'll take a little bit so I'll

play08:38

probably pause the video here or I'll

play08:40

kick off the I'll kick it off pause it

play08:42

come back when it's finished and then we

play08:43

can see what it looks like in Lang Smith

play08:45

as

play08:47

well all right so it's finished so it

play08:51

was streaming out so we can take a look

play08:53

at the whole stream of things in here if

play08:55

I can figure out how to expand it so we

play08:57

can see the researcher um you know the

play08:59

first thing it does is it queries UK GDP

play09:01

data for the past 5 years and then calls

play09:03

a tool um and then it kind of keeps on

play09:06

going um at some point there's this

play09:08

chart so you know it PL plots it over

play09:10

the past uh three years it looks like so

play09:12

it didn't quite get all the all the data

play09:14

it needs so you can see uh yeah you can

play09:17

see it says that the data for years 2018

play09:19

2019 is missing um and so it plots that

play09:22

and then this is just the final response

play09:23

so that's why it shows up so big if we

play09:25

want to see this in a little bit nicer

play09:27

view because this is a lot to digest we

play09:29

can go to Lang Smith um and so Lang

play09:31

Smith uh at the top of the notebook I

play09:33

sent I set it so that it would log to

play09:35

this specific project the multi-agent

play09:37

collaboration I can see that I've got

play09:38

one run can see some basic stats on it

play09:41

like it took over a minute it's got some

play09:42

tokens on it um if I click into it I can

play09:45

now see exactly what was going on under

play09:47

the hood so first it called the

play09:49

researcher and we can click into here

play09:51

and we can see the exact call that it

play09:52

made to open AI um and so there was this

play09:55

system prompt and then uh this was the

play09:57

user's first message and then output

play09:58

this

play09:59

it then called toil um which is the

play10:01

Search tool we can see the results there

play10:03

it then called the researcher again we

play10:05

can see that you know there's now more

play10:07

in it because we start to build up this

play10:08

message Bank we can see it calls toil

play10:11

again okay so now the researcher and it

play10:13

doesn't call toil okay interesting let's

play10:14

see what's going on here so at the

play10:17

bottom it basically says okay so now

play10:20

it's uh it's responding what the values

play10:22

are um and there's no tool call and

play10:25

there's no uh uh thing saying that it's

play10:27

finished so that means that it goes to

play10:29

the next one which is the chart

play10:30

generator so it calls the chart

play10:31

generator under the hood that calls open

play10:33

AI um we can see that down here we get a

play10:36

function call and this is running python

play10:38

code so it prints this out calls the

play10:40

python reppel

play10:42

um uh we can see that it runs this um

play10:46

the chart generator then goes um and the

play10:49

chart generator says uh you know here's

play10:52

the line graph see the chart above the

play10:54

researcher then goes and it says final

play10:58

answer so now that there's final answer

play11:00

it's finished and that's basically what

play11:02

happened um so that's the multi-agent

play11:05

collaboration bit the the kind of like

play11:09

specific thing about this that's

play11:10

interesting is it has This Global state

play11:12

of messages that each llm sees and a

play11:14

penss to so it keeps on adding to this

play11:16

over time and so on one hand this is

play11:19

good because it allows each agent to see

play11:21

kind of like exactly the other steps

play11:22

that the other one is doing and that's

play11:23

why we called it collaboration it's very

play11:25

collaborative the other version of this

play11:28

is that you have agents that have their

play11:29

own independent scratch pads um and so

play11:32

we'll take a look at the next

play11:33

multi-agent example that we have which

play11:35

is this agent

play11:36

supervisor so here we have the

play11:38

supervisor which basically routes

play11:40

between different independent agents so

play11:43

these agents will be Lang chain agents

play11:45

um so they'll basically be an llm that's

play11:48

run with the agent executor in a loop

play11:51

the llm decides what to do um the agent

play11:54

executive then calls the tool goes back

play11:56

to the llm calls another tool goes back

play11:58

to the llm and then then finishes and uh

play12:00

then there's basically the supervisor

play12:02

the supervisor has its own list of

play12:04

messages and essentially what it will do

play12:05

is it will call agent One agent one will

play12:08

go do its work but it will return only

play12:10

the final answer only that final answer

play12:11

is then added the supervisor only sees

play12:14

his final answer and then based on that

play12:15

it can call another agent or it can call

play12:17

another agent another way to think about

play12:19

this is that this is essentially um you

play12:21

know the supervisor an agent and it's

play12:23

got a bunch of tools and these tools

play12:25

themselves are agents so it's a slightly

play12:27

different framing than before it's not

play12:29

as collaborative they're not working on

play12:30

kind of like the same

play12:32

sections so let's get this started um

play12:36

again we're going to be uh I'm going to

play12:39

let me restart my kernel we're going to

play12:41

be using Lang Smith um so again uh hit

play12:44

me up if you don't have access to that

play12:47

um first thing we're going to do is

play12:48

we're going to create some tools we'll

play12:50

use the same two tools as before the

play12:51

tavil tool and the python

play12:54

tool we'll next Define some helpers

play12:57

again to create these agents um as the

play13:00

individual nodes so we have this create

play13:02

agent function similar to last time the

play13:04

difference is now that it's actually

play13:06

doing a little bit more work under the

play13:07

hood so it's not just a prompt plus a

play13:09

language model it's actually creating an

play13:11

open AI tool agent which is an agent

play13:13

class that we have in Lang chain from

play13:15

there it's creating an agent executor

play13:17

which is also in Lang chain and we're

play13:18

returning that that's the final

play13:20

agent so let's run that then what we're

play13:24

doing is we're also creating similar to

play13:26

last time this agent node thing which

play13:27

does this human message converion just

play13:29

like we did last time is taking the

play13:30

agent result um which is an AI message

play13:32

and converting it into human message

play13:34

with a name so we know kind of like

play13:35

where it's coming

play13:38

from now we create the agent supervisor

play13:41

so this is going to be the the you know

play13:44

system that's responsible for looking at

play13:46

the different agents that it has and

play13:48

deciding which one to to pass it on to

play13:51

so

play13:52

here you can see that we're defining a

play13:55

bunch of stuff we're defining system

play13:56

prompt your supervisor task with

play13:58

managing conversation a between the

play13:59

following workers and it's basically

play14:01

using this function definition to select

play14:03

the next Ro to send things to um and uh

play14:07

it's or it's selecting finish finish is

play14:09

another option we're creating this

play14:10

prompt template we're creating this llm

play14:12

and we're creating this supervisor chain

play14:15

um which basically has a prompt um it's

play14:17

got an llm with some functions that it

play14:19

can call specifically we're forcing it

play14:21

to call this route function where it

play14:23

selects the next best role and then

play14:24

we're pressing the the response in Json

play14:27

um and we're using that determine where

play14:29

to go to

play14:30

next now that we've got that we can

play14:32

create the graph so we've got this agent

play14:34

State um which is messages and then a

play14:37

next field um we've got this research

play14:39

agent so we create the agent we then

play14:40

create the node for it so we partial out

play14:42

agent node we then do the same for the

play14:44

code agent um and then we start creating

play14:46

the graph so we create the graph with

play14:48

the agent State and we add the three

play14:50

nodes we now connect the edges in the

play14:52

graph okay so for all the members um and

play14:56

again if we look at where members is

play14:58

defined members is defined up here so

play15:01

it's the researcher encoder it's two

play15:02

it's the two basically sub nodes or sub

play15:06

agents um so for those after we call

play15:08

those agents we go back to the

play15:11

supervisor um then we also have this

play15:15

conditional Edge so from the supervisor

play15:17

um after the supervisor um is run we

play15:20

look at what's in next and we look in

play15:22

the conditional map and the conditional

play15:23

map is basically a mapping between the

play15:25

the members so uh and and finish so so

play15:28

you know if if it says researcher then

play15:30

we go to research node if it says coder

play15:32

then we go to coder node if it says

play15:33

finish then we finish we set the entry

play15:35

point to the supervisor and then we run

play15:38

the

play15:39

graph now we can do the same thing we

play15:41

can invoke the team um we can uh see

play15:44

what happens um and see how it

play15:56

performs all right so it runs it um you

play16:00

can see that it calls the coder so here

play16:01

we're asking it to print out hello or to

play16:03

say hello world and print it to the

play16:05

terminal so it calls the coder and then

play16:06

it finishes here we can write a brief

play16:08

research report on pasas and it will do

play16:12

that while that's running um we can

play16:15

maybe look in Lang Smith um to see the

play16:18

previous ones so here we have the coding

play16:20

one um we can see that we first called

play16:22

the supervisor agent it's got this call

play16:24

to open AI um again it's got the system

play16:27

prompt and then the human um and then it

play16:29

decides what to do next and it's calling

play16:32

it's using this function call we can see

play16:33

it has this function definition of Route

play16:35

here based on that it goes to the coder

play16:37

so now there's the coder agent the coder

play16:40

agent does several things under the hood

play16:41

it calls open AI first um it does a

play16:44

function call it then calls the python

play16:46

reppel um and then it calls open AI

play16:49

again and so when we go back to the

play16:52

supervisor if we look at what the

play16:54

supervisor see says it just sees the

play16:58

response from the coder so there's a

play17:00

human message with the name coder and it

play17:02

gets this response it doesn't see any of

play17:04

these calls though or any of the calls

play17:06

to the python reppel and then we get a

play17:07

response and it says to finish so this

play17:10

is an example of where you know there's

play17:11

many calls but they're kind of hidden

play17:13

within one agent

play17:15

State we can also see that it finished

play17:17

writing the research report on pasas so

play17:20

if we go back here we can see that it's

play17:22

finished running and if we take a look

play17:23

it now calls the researcher agent under

play17:25

the hood and we can see here if you know

play17:27

this is interesting because this took a

play17:29

pretty long time 45 seconds we can

play17:30

quickly drill in and see that the the

play17:32

longest call was this call to open AI um

play17:35

inside the researcher agent where

play17:37

basically it uh uh

play17:41

has it has all these uh uh information

play17:44

from the internet and it writes this

play17:46

long research

play17:48

report so that's pretty much it for the

play17:50

agent supervisor now I want to talk

play17:52

about the third and final type of agent

play17:55

that we have an example of which is the

play17:57

hierarchical agent team

play17:59

so this is very similar to the previous

play18:00

concept it's just changing what's going

play18:02

on in the uh

play18:05

uh it's just changing what's going on in

play18:08

the sub things so now each agent node is

play18:12

itself a different kind of like

play18:14

supervisor agent setup and so what this

play18:17

means is that it's going to be a lot

play18:19

more kind of like a l graph um and a a a

play18:23

little bit less of L

play18:26

chain we can get started by setting up

play18:29

the environment similar to last time we

play18:31

now create the tools so there's two

play18:33

different sets of tools that we need to

play18:34

create one's for the research team it's

play18:35

the Searcher and web scraper the others

play18:37

for this document authoring team and

play18:39

it's these three tools here so we can do

play18:41

that we can load to villy we can create

play18:43

our scrape web pages tool and then we

play18:44

can start creating these uh other tools

play18:48

as well um and these are for the writer

play18:50

team so we create one that creates an

play18:51

outline read documents write documents

play18:53

edit documents um and then a python

play18:55

reppel tool as well we we're going to

play18:58

create a few helper utilities that make

play19:00

it easier to basically construct this

play19:02

pretty complicated graph so we have one

play19:03

that creates an agent we then have an

play19:05

agent node um and we then have this team

play19:08

supervisor idea and so these are kind of

play19:10

like taking a lot of the concepts that

play19:11

we had in the last notebook and

play19:13

extending them now we can Define these

play19:16

agent teams they're hierarchical teams

play19:19

so we have this research team This

play19:21

research team has a few different

play19:22

attributes on it the main things here

play19:24

are this messages um we create uh we

play19:27

initi in llm we create the search agent

play19:30

and then the search node the research

play19:32

agent and then the research node and now

play19:33

we have the supervisor agent as

play19:37

well we now put this all together um I'm

play19:40

not going to go over all the edges

play19:41

because it's very similar to some of the

play19:43

notebooks that we did before but the end

play19:45

result is that we have this uh uh graph

play19:50

that we create in order for this graph

play19:52

to be used in as a subgraph in another

play19:55

component we're going to add this enter

play19:56

chain function which basically just is

play19:58

just a converter to make it easier to

play20:00

use we can use this directly um so we

play20:03

can try out and again this is just one

play20:05

of the sub teams but we can try asking

play20:06

it a question um and we can see that it

play20:09

does a search it has a search to so

play20:12

it'll get a response from there and it

play20:13

will give us an

play20:16

answer we can see that we get back a

play20:18

response here and if we look at that in

play20:20

lsmith we can see that it's doing a

play20:23

pretty similar thing to before um it's

play20:25

got the supervisor agent it's calling

play20:26

search and then it responds with

play20:28

supervisor we're now going to put

play20:30

together another agent or another graph

play20:33

of Agents so it's this document writing

play20:34

team um so here we are defining a bunch

play20:38

of stuff we're defining the state that

play20:40

we're tracking it's largely these

play20:42

messages um there's then uh some logic

play20:45

that's run before each worker agent

play20:46

begins um so that's more aware of the

play20:48

current state of everything that exists

play20:50

this is basically with populating

play20:51

relevant context we then create a bunch

play20:54

of agents and a bunch of uh uh agent

play20:57

nodes um um and then we create the dock

play20:59

writing supervisor um now that that's

play21:02

done we can create the graph so we're

play21:03

adding a bunch of nodes we're adding a

play21:05

bunch of edges we've got some

play21:06

conditional routing edges um we set the

play21:09

entry point um and now that we have this

play21:12

we can do something simple like write an

play21:14

outline for a poem and write the poem to

play21:18

disk we can see that this is finished

play21:20

and if we look at this in Lang Smith we

play21:23

can see that it also looks fairly

play21:24

similar as to what we were doing before

play21:26

we've got the supervisor we do a bunch

play21:28

of note taking and then the supervisor

play21:31

finishes it doesn't see any of the

play21:33

internal things it just sees the final

play21:35

response and we can now put this

play21:37

together so we now have a layer on top

play21:39

of this we've got this team supervisor

play21:41

um and now we have this top level graph

play21:43

State it's just a list of messages we

play21:45

have some helper functions we start

play21:47

creating the graph we add the research

play21:49

team as its own node it's got a little

play21:51

bit of uh it's got a little bit of

play21:53

wrappers around the previous chain to

play21:55

like get the previous message and then p

play21:57

and then at the begin beginning pass it

play21:58

that into the chain and then call join

play22:01

graph to join it with the rest of the

play22:02

stuff same uh uh with the paper writing

play22:06

team we then add the supervisor we then

play22:07

add some nodes we compile it all right

play22:11

um and so now we can uh do a more

play22:14

complex thing where we call asket to

play22:17

write you know this brief research

play22:18

report on the North American sturgeon

play22:19

include a chart so let's kick this off

play22:22

and I'll come back when this is

play22:25

done all right so it's done took a while

play22:28

if we go to Lang Smith we can see that

play22:31

we have this most recent Trace took 400

play22:34

seconds if we click inside it we can see

play22:37

that there is a lot going on so a lot of

play22:41

calls um down the hood it looks like

play22:43

there's this big call to the paper

play22:45

writing team at one point that took a

play22:46

while there was also this big call to

play22:48

the research team to start that took a

play22:50

while I can click into any of these I

play22:52

can see like what exactly is going on at

play22:54

any point in time what exactly was

play22:55

happening um so this is pretty helpful

play22:57

for understanding what's going on um

play23:00

yeah wow that's a lot of tokens um

play23:05

anyways yeah that's pretty much it for

play23:07

the multi-agent stuff hope this was

play23:09

helpful I think uh you know the the

play23:12

benefits of thinking about things as a

play23:14

multi-agent way are really that it I

play23:17

think it helps compar compartmentalize

play23:20

what you expect different functions to

play23:23

be happening inside the system so you

play23:25

know each agent basically has its own

play23:27

prompt its own tools even its own llm at

play23:30

times and so you can really focus it on

play23:32

just doing specific things as opposed to

play23:33

having one General autonomous agent with

play23:35

lots of tools and so even if you you

play23:38

know even if you have all those same

play23:39

tools and you just organize it a bit

play23:41

more hierarchically that can often be

play23:43

helpful for for doing kind of like

play23:45

complex tasks as well as just a good

play23:47

mental model to how to think about

play23:48

things so hopefully this has been a good

play23:50

introduction to that these are again

play23:52

probably only a few of the ways that you

play23:55

can create multi-agent workflows with L

play23:57

graph really excited to see what other

play23:59

ones

play24:00

emerge

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
L-GraphAI WorkflowsMulti-AgentDynamic GraphsState MachinesLanguage ModelsAgent CollaborationWorkflow DesignAI ToolsLang Chain
¿Necesitas un resumen en inglés?