Hello guys!
In this video we are going to create an amazing end to end multi AI agent application using land graph
and astrolabe.
Now if you don't know about land graph, Landgraaf actually helps you to create AI agents in such a
way that you'll be able to manage their state.
The communication between the agents.
It provides you the entire workflow to probably create this amazing AI applications.
So in this video also, we are going to probably discuss about an end to end project.
First of all, we will go ahead and discuss about the architecture, what we are actually trying to
implement.
And then step by step, we'll go ahead and implement this with the help of land graph.
So here the architecture of the project looks like this.
Let's say that I'm trying to create a chatbot application which has this multi AI agent feature which
will be able to communicate with each other and it will be able to do multiple task.
So whenever a user query comes, there will be a start node in land graph.
And this start node will further send it to router.
Router basically means whenever I'm probably putting up a user query.
We need to route it whether this user query can be fulfilled after requesting from a vector database,
or whether it can be fulfilled from an external tool like wiki search.
Now here, just for an example, I'm considering wiki search.
But uh, in real world application you can even implement different different external tools you like,
right?
So wiki search is one.
You can also use serp API, you know, to probably do Google search itself.
Right.
So in this example I'm just going to consider one.
Uh, and similarly you can implement with respect to other third party external tools.
Then uh, if I probably consider this side, you will be able to see that it will do a vector DB search.
Now this vector db is basically getting created by Astra db.
We will try to create this entire vector database in the cloud.
Okay.
In the cloud.
And initially inside this vector database we will be putting some information.
The information will be a website page from which we can specifically query right.
We will take some URLs and we will read the entire data inside that particular website.
And we will store that all in in the form of vectors in the vector database.
That is Astra DB.
Right.
And this will be in the cloud platform.
Um, basically it is deployed in the cloud.
And I will try to show you the entire configuration and how powerful and amazing Astra DB is.
Okay, after we do this specific thing, then the next step will be that we will get the response from
this, and then we can integrate along with LLM along with some prompt engineering like what we really
want to do with this particular response.
And finally we should be able to get our response.
And the final node is basically n.
So this is the entire architecture of this project.
In terms of line graph.
We are going to be we are we are going to use the line graph where we are going to implement this entire
thing.
Right.
So without wasting any time, let's go ahead and implement this entire thing.
Okay.
So first of all you really need to know about data stacks.
So data stacks actually provides you the access of Astra DB.
So in order to access them, uh, you know, you can actually do vector search for real world journey
applications.
And it is really, really powerful.
Okay.
And already I have made some videos related to Astra in my previous tutorials also.
So first of all, just go ahead and sign in.
So once you probably sign in then you will be able to see this specific page okay.
Um if you have not signed in.
So first of all you need to sign up over there.
Here you will be able to probably create databases.
Okay.
So in order to create the databases you can actually see over here I will just go ahead and click on
Create Serverless database.
Okay.
Now there are two options over here.
Serverless vector database.
Serverless non vector database.
But as you all know we are planning to probably create serverless vector database.
Okay.
And the reason is very simple.
Whatever data we have or whatever, uh, whatever data in the form of text we have, we are going to
convert that into vector.
And we are going to save in this particular vector database itself.
So first of all I'll click this.
I will give my vector database name.
Let's say this will be test database right.
And here I will go ahead and select any of the region.
And let me just go ahead and click on Create Database.
So as soon as I probably create it it will take some amount of time.
So let this get created.
Till then I will just wait probably to create this entire database and then we will proceed with the
next step.
So yes, let me just go ahead and click this and let me start it.
Okay.
So this is getting created.
It is going to take some amount of time.
So once it is getting created I will probably go with the next step.
So finally guys here you can see that my database has got created.
That is text database a test database.
It is a vector database.
And please make sure that you keep a note of this database ID, because we will be requiring in order
to access this database.
Then the next step will be that we will go ahead and create our tokens.
Now in order to create a token, first of all, you need to probably select the role that you are probably
going ahead with.
So I need administrator user.
I will just go ahead and click on Generate Token.
Now this token will be important because with the help of this particular token I will go ahead and
access my database.
Right.
So if you also want to know you can also download this JSON file.
So let me just go ahead and click and download the JSON file.
Okay.
So now I have my access token.
Now my database is ready.
Now it's time that we go ahead and start our execution of the code.
Okay.
So, uh, before we go ahead, step by step, I will try to show it to you.
Till then, just let me hide my face so that you'll be able to clearly see the code itself.
Okay.
So here I'm going to use Google Colab.
So I will change the runtime.
Let me just use any GPU that I want.
So I'll just go ahead and click on save.
Um, so once I save it, my runtime will be on.
Okay.
Now what I'm actually going to do.
First of all, we will go ahead and install some libraries.
So first of all I require library like lang chain install lang chain.
Then we are going to install lang graph.
Then along with this we are also going to install cashier okay.
So this cashier will be important because I am going to this will be helpful for me to initialize the
Astra DB database itself.
Right.
So I will show you uh, because for initializing Cassandra DB we require this library.
I'll show you as we go ahead.
How why are we using this?
Okay, so here you can see the installation has already taken it.
It has already started.
Okay.
Then the next step after this.
So what we are basically going to do is that we are going to import cashier And then we will provide
the connection of the of the Astra DB.
Okay.
So in order to connect to the Astra DB, we will be using this.
First of all, the important parameter that I actually require is my Astra DB application underscore
token.
Okay.
Now this particular token we can get it from over here.
So here you can see this token information is basically present.
So I'm just going to copy this and I'm going to paste it over here.
Okay.
So here I'm just going to go ahead and paste it.
And with the help of this particular token we will be able to connect to our Astra DB right.
Then we are going to probably use our Astra db id.
Okay.
That is our database ID okay.
So we will be using this.
We have created this particular variable.
Um let's let me go and see this So here is my test database.
And here is my database ID.
Okay.
So this basically becomes my database ID itself right.
Now in order to initialize this I require this two information.
So I will go ahead and write Cachio dot init which is nothing but sorry dot init and will initialize
this particular database.
So token is equal to astra db app store db application token.
And the second parameter will basically be my database database id which will be equal to Astra Astra
db id okay.
So here you can see once I execute this this will get initialized.
And here I have got some warnings but it's okay.
Now we are able to connect to our Astra DB right.
Perfect.
Now the next step will be that since we need to we need to develop the land graph itself.
First of all, I will also install one more very important thing that is pip install lang chain underscore
community.
Okay.
So let me go to go ahead and install this entire application over here.
Once this is installed uh, I still need to install some more things like uh, lang chain grok, uh,
lang chain hub, uh, lang Lang graphs.
There are some more libraries that we are just going to install.
So for that I will just go ahead and copy and paste over here and I'll talk about what all things we
are basically going to install.
Okay.
So here you can see I'll be installing lang chain underscore community lang underscore community I think
it is already been installed.
We will be installing tick token.
I'll talk about this.
Uh we are going to use open source LM models.
So for that we are going to use this Lang chain grok.
Then you have lantern hub chrome adb lang chain lang graph lang chain hugging face.
I will not require chrome adb because I'm specifically going to use this Datastax DB, right?
So line graph line chain hugging face is basically there.
So now let me just go ahead and install all these libraries.
These are some of the basic requirements that I require in order to start my process.
Okay.
Now if you have been seeing my entire lecture series, uh, in this course itself, you'll be able to
see that, um, I have already discussed about Lang Chain.
I have discussed about recursive character text splitter.
So first of all, what I'm actually going to do is that I'm going to read some kind of websites.
You know, from that I will try to read the text data and convert that into vector and store it in a
vector database which is present in Astro DB.
Okay.
So this is basically getting installed.
It will take some amount of time.
Let's wait till then.
Then till then I will go ahead and write my other code.
So here I'm going to use a text splitter which is called as recursive character text splitter.
And since I told you that the data ingestion, I need to read my data from different websites.
So I'll be loading underscore community dot document underscore loaders import web based loader okay.
Then the URL uh which all URLs I'm specifically trying to read.
So these are my three URLs that I'm going to use.
One is related to agent, one is related to prompt engineering and one is related to adversarial attack
LLM okay.
Now what we are basically going to do is that we are going to load the URL.
So for this I will go ahead and write docs is equal to web based web based loader.
I will give my URL dot load instead of just using zero.
I will use for URL in URLs.
And this is basically going to read each and every URL and probably go ahead with the next step.
Then I have automatically.
This is the amazing thing.
Google Colab provides you all the necessary things that is required.
Okay, so here I'm going to use a text splitter.
Now instead of using every time recursive character text splitter.
This time I will show you something in a different way.
So I am going to use something called as from underscore tech token Encoder.
And here we are going to provide the chunk size of 500.
And there will be also a chunk overlap.
Let's say that I'm not interested in putting any kind of overlap.
So I'll keep it to zero okay.
So this way we will be able to do the splitting.
Uh, so one assignment that I really want to give you is that just go ahead and explore about this particular
function why this function is specifically used okay.
The answer is given over here only if you just highlight this text splitter that uses tick token encoder
to count length okay.
So tick token encoder using uses encoding uh name as GPT two.
You can just go ahead and see the definition.
There is a slight difference between recursive character text splitter using this tick token encoders
okay.
Then I'm going to probably do the splitting my documents split, I will go ahead and define this variable.
And I here I'm going to use my document split split underscore documents with respect to docs okay.
Now before I go ahead and probably do this, you know, I will also go ahead and write one more line
of code.
That is my document list.
Here I'm going to consider item for sub list and docs.
I'm going to explore each and every docs for item and sub list.
Okay.
Let me explain about this specific code what exactly this does okay so here I'm saying hey for item
in sub list.
Then we are iterating to sub list in docs okay.
And we are just getting the item okay.
So if you just go ahead and print the document list, you'll be able to see that we'll get all the items
that is displayed okay.
So once I go ahead and execute this you'll be able to see okay let's execute this.
It is going to take some amount of time.
So here it is giving us an error saying that the list object has no attribute page underscore content.
Let me just go ahead and check this out and let me see what is the error that is probably coming up
over here.
So okay.
It should be documents doc underscore list.
Okay.
So if you want to just go ahead and print this doc list you'll be able to see what it prints okay.
Okay.
Again I'll get an error.
So it should be doc underscore list.
So in Doc underscore list you'll be able to see what item is basically getting displayed.
Okay so here is all my information with respect to this.
See.
And then I'm taking this entire information I am splitting it.
Okay.
So this is my entire metadata that I have right.
Then we do the text splitter with respect to chunk size.
And then finally we do the we get the we do the split with the help of text underscore Splitter.
Okay.
Now once I have this dock underscore split.
So if you go ahead and see this this is my dock underscore split okay.
Now if I just go ahead and display this this is my entire splits that you'll be able to see.
Right.
Perfect.
Now I need to take this content and convert this into vectors by using some kind of embeddings.
So for this I am going to use from lang chain underscore hugging face okay.
Hugging face.
And from the hugging face we are going to import hugging face embeddings.
Inside this embeddings I'm going to use hugging face embeddings.
Hugging face embeddings.
And we are going to use a free embedding technique that is available as an open source all dash mini
lm okay L6V2 two.
Okay.
So this name if you see this is already available in the hugging face.
So let's see this.
And here you can see it is a sentence transformer.
And this basically converts it maps sentences and paragraphs to a 384 dimensional dense vector.
Okay.
But for this you also require one very important thing.
If I go and click on settings you'll be requiring a token.
Right.
So this many number of tokens is basically created right in my.
You can go ahead and create a token because this token will be specifically used over here.
You can see in the secret key I've added a token which is called as hugging face right.
Hugging face API token.
So it will be asking for this particular token itself right.
So or h f underscore token.
This token also you can basically specify.
So create a token name provide a token name whatever name you want.
And you should just provide permissions and go ahead and click create this for creating.
You can see over here uh I will just go ahead and write read write.
Okay.
So let's say I want to read.
So here I will just provide my token.
Let's say my token name is something like h f token.
Okay I will go ahead and create the token.
Just copy this token over here and paste it right.
This particular key name h f underscore token and paste it over here.
I've already done that so I'm not going to do that.
I will just go ahead and execute this because this is going to ask me the token information over here.
And I just need to provide the grant access.
So here you can see once this gets executed my token will be ready.
See it is asking for grant access of h f underscore token.
And here you can see it is now able to read the token itself.
Okay.
Now the next step over here.
What we are basically going to do is that we are going to insert all the vectors that we have actually
converted, uh, by using this embedding technique, okay, into our vector database.
So first of all I will go ahead and write from lang chain lang chain Dot vector stores.
Dot.
Cassandra.
Okay.
Import.
Cassandra.
Okay.
So we are going to import Cassandra.
And then we are going to probably create our Astra underscore vector.
Underscore store is equal to Cassandra.
And inside this Cassandra we are going to use our embeddings.
Embedding is equal to embeddings.
And then I have my table underscore name is equal to.
Let me give a table name like QA mini demo.
Okay I'll just go ahead and write this.
I will keep my session as none.
Now what this is basically going to do is that it is just going to insert my data in the database that
I've actually given.
Okay.
So this will basically be none Okay, so almost done.
Any parameters has been left.
Okay, I have to remove this bracket.
Okay.
Now this is done over here.
So let me just go ahead and execute this.
Now this will get connected to my database along with this embeddings.
Okay.
And the table that I'm actually going to create is this particular table.
Now inside this table I need to just go ahead and store my data.
Right.
So first of all what I will do is that I will go ahead and import from lang chain dot indexes.
Indexes.
Dot vector store I'm going to import vector store index rapper not creator index rapper.
Let's say because this will be a rapper on top of the vector store that we have created, the extra
extra vector store.
This will be my vector store itself to probably I can use it to query my vector database.
Okay, so let me delete this.
okay vector store index wrapper.
Now I can use this Astra underscore vector underscore store dot add underscore documents.
And here I'm going to basically use doc split.
Okay.
Then I can go ahead and print inserted
modulus I headlines how many records I'm basically inserting.
I'm just putting that particular information over here.
And here I can use percentile length of the doc split.
So this is basically how the insertion how many records has been inserted okay.
So I'm just going to print this entire thing or inserted print inserted inserted records.
Let me just go ahead and see whether I've done any syntax issue over here Oh yeah.
So docs underscore split.
Perfect, right.
So now it is fine.
Now I'm going to use this extra extra vector D vector index.
And after insertion if I really want to access this I will be using this vector store index wrapper.
And I will be using this extra vector store.
Okay.
So let us go ahead and execute.
First of all it will just go ahead and insert it.
Uh let's see.
Vector store index wrapper from Lang cannot import the name.
The reason is very simple here I made a spelling mistake.
Okay, so here also I made a spelling mistake.
It should be s capital.
So now let's go ahead and execute this.
This line of code is basically going to insert all the documents.
Right.
And then on top of this I am creating a wrapper using vector store index wrapper.
Because of this wrapper you will be able to interact with the vector database.
Okay so here you have this entire thing okay.
And now let me show the next step.
It is obviously true that you cannot just use directly vector database or this particular variable to
access the vector database.
Yes, you can use it, but on top of it we will try to create it as a retriever.
So here I'm going to define my retriever is equal to Astra vector store as retriever okay.
And then I can go ahead and ask any question that I want.
Retriever retriever dot invoke.
Let's say I will go ahead and ask what is agent okay.
And let me just go ahead and execute it.
There's an error.
No worries okay.
What is an agent.
And this is my entire answer right.
Building agents with this this this C is basically coming from the vector database itself when I probably
use this retriever.
Right.
And retriever is basically created on top of vector store.
Right.
Which is basically referring to my vector database Perfect.
So if I go ahead and see this part, I have actually created it, right?
This entire part of Xtradb configuration inserting the data.
And if you also go ahead and see over here, right over here, if I just go ahead and execute it, see,
see the amount of data that has been put up.
Right.
So here is my QA mini demo default key space right.
All the data is basically available over here.
I'm again reloading it so that you'll be able to see this.
If I go ahead and click this you will be able to create a collection.
You'll be able to see this all the information over here right.
And this is great.
Just now you did a query.
And these are all the information that you are able to see.
Perfect.
Now the next step uh, obviously if this is completed I need to create a wiki search.
And for creating wiki search, I really need to go ahead and probably, uh, create and use an external
tool that is called as wiki search.
Okay.
Now before going and creating this wiki search tool, what I'm actually going to do is that I'm going
to define a router function.
Okay.
And if you are seeing my landgraaf videos, I think you should be knowing how to probably go ahead and
create it.
So let's start working on our land graph application.
So here I'm going to basically write land graph application.
And here are three important uh things we are basically going to import okay.
One is one is from typing import literal from land underscore code or prompts.
We are going to use chat prompt template.
And from lang underscore core we are going to use base model and field.
This will be important because when we are basically using or creating our root query we have to inherit
this base model.
So here let me just go ahead and create my data model.
So here I'm writing grass root query base model.
Here I've used data source as two important things.
As a literal right.
One is vector store, one is vector.
See wiki search.
Because this root query class needs to root on any one of them, whether it should go to vector store
or wiki search.
So given a user question, choose to root it to Wikipedia or a vector store.
Okay, so that is the reason we are basically using this particular root query.
So let me just go ahead and execute this.
Because after this we are also going to create some functions.
Now if you are familiar with already working with charts and all.
So here what I'm actually going to do is that I will go ahead and initialize my, uh, use my grok API.
So for grok I will just go to grok.com okay.
Inside this grok dotcom you'll need to sign up or sign in.
And here you can see that it provides you most of the open source model in the form of APIs.
Right.
You need to use the access token.
And you should be able to use any of this.
Even llama 3.1 is also available.
Okay.
So first of all I'll click on Start Building.
I will go ahead and see my grok API.
So I will go ahead and create my API token.
So here you can see API keys.
You can just go ahead and click on API key.
Give us some name and you should be able to create it.
And once you get the secret key you need to update this in the secret key.
So here you can see I have already updated it in the name of grok underscore API key.
And this is my API key right.
So with the same name you can go ahead and call it right now from this grok I'm going to use my model.
That is nothing but llama 3.1 70.
Be versatile or it be instant.
Any one of them we can specifically use.
I will use this okay.
And let's use this.
So before using this first of all we need to import or get our grok API.
So here I will go ahead and execute this um before understanding one thing that if I really want to
access any secret keys from the Google Colab, I need to import from Google Colab, import user data,
and then I'm using User data dot grok API.
So if I just go ahead and print my Grok underscore API underscore key.
You should be able to see my API key right.
Grant access.
And here you can see.
Now if you go ahead and see the secret key the same API key same API key, it is basically referring
to.
Right.
So here is my entire API key that you will be able to see.
Then we will go ahead and initialize our LM model.
So for initializing it I will write LM chat grok.
Okay.
And here I'm going to use grok underscore API.
Underscore key is equal to grok underscore API underscore key.
Okay.
And second parameter I need to give my model underscore name.
This model underscore name we will go ahead and initialize with llama 3.1.
Let's see whether I should be able to copy this or not.
Llama 3.1 70 Be.
Let's say I'll go ahead and write over here.
Llama 3.1.
Okay.
Dash.
Dash 3.1.
Dash 70.
B 70.
B and this is last is versatile.
Okay.
Please make sure that you have the name same.
Otherwise it will not be able to call.
Okay.
Now if I have this what I am going to basically do is that I will just go ahead and use this LM model.
Okay.
We'll just go ahead and print this and see whether we are able to call it or not.
So here you can see I'm able to call this particular model which is amazing.
Okay.
Now one thing that you should know is that our router you know our router this router is really important
because based on a user query I need to give it to vector store or wiki search.
Okay.
So now what we are basically going to do is that we are going to integrate this route query with our
LM model also.
Now how do I do that.
You know.
So see at the end of the day whatever output that I'm actually getting, it should be in this particular
format.
So here I'm going to create a variable called a structured LM underscore router is equal to lm dot width
structured.
So there inside the LM there is something called as which structured output.
And I'm going to use my root query over here.
Okay.
I'm basically going to call my root query class.
Okay.
Now this is fine.
Perfect.
Uh now let's go ahead and check whether everything will work or not with respect to our root query.
Okay.
Root query basically means what?
Whenever we get any user query, it should be able to tell whether it is a wiki search or wiki vector
DB search.
Right.
And for that we have also used one lm model.
And we have used one root query inside it.
Right.
So first of all, what I'm actually going to do is that I'm going to use one prompt.
Okay.
So let's say this is my prompt.
In this prompt I'm writing.
Hey you are an expert at routing a user question to vector Store or Wikipedia.
The vector store contains document related to agents, prompt engineering, and adversarial attacks.
Use the vector store for the question on this topic.
Otherwise, use wiki search.
Okay, so that is what we have written over here.
Okay.
And that is the reason we have used this chat prompt template from message.
We have used system system because this is the instruction that I need to give to my LM model.
And here we are giving the question as with respect to whatever human question is basically given,
then we will go ahead and create a variable which is called as question router.
Here I'm going to use the same root prompt and structured LM router, because this entire task will
be done by an LM model itself.
Right.
So once I execute this this is done right now I will go ahead and write print question underscore router
dot invoke okay dot invoke question underscore router dot invoke.
Let's let's test it.
Okay.
So I will just go ahead and write a question.
Let's say the question is what is an agent.
So if I'm writing, what is an agent?
Obviously my answer should be it should route it to what vector store, because my vector store has
already all this particular information.
But what if I write a question which looks like this?
Who is Shah Rukh Khan?
Shah Rukh Khan is a famous actor in India, right?
So if I go ahead and execute it, you'll be able to see that it is going to Wikipedia search, because
that kind of information is even not present in vector store.
So I don't need to probably push this particular query to my vector store itself.
It should probably go to the wiki search.
So this part of vector store I have already created, right?
I've created a retriever.
Now what is pending is basically for this Wikipedia search right now.
How do I probably go ahead and create this Wikipedia search.
So again in long chain you have this third party tool in Wikipedia.
And for that we need to install this right.
We, we have something like arXiv or Wikipedia.
But I'll just go ahead and install Wikipedia and I'll also install wiki lang underscore community okay.
so here you can see it is already been satisfied.
So it is getting downloaded okay so let's go community.
We have already installed it I guess.
No worries.
Okay now for Wikipedia.
I will probably go ahead and import because here we are working with external tools right lang underscore
community dot utilities.
I'm going to import Wikipedia rapper Wikipedia API wrapper.
Now along with this I'm also going to import from lang chain underscore community dot tools import Wikipedia
query.
Done right.
We're going to import both this.
Now first of all I will go ahead and create my API wrapper.
And this entire thing will basically be responsible in probably running my entire queries.
Right.
So whenever I give a query when it is going to the Wikipedia search, this API wrapper will make sure
that it will do that And here I will just go ahead and define my variable wiki.
Okay.
Inside this I can also go ahead and write my how many results I want.
Let's say if I go ahead and write top k results is equal to one and the max to max document content
character Max, I say that hey, I need 200 characters, okay?
And then I will be able to use this Wikipedia.
So once I execute this, you will be able to see that if I go ahead and ask any question.
Wiki dot.
If I go ahead and write wiki dot run.
Okay.
And let's say if I go ahead and write.
Tell me tell me about Shahrukh Khan.
Okay, so I should be able to get all the response from the Wikipedia itself, right?
So all the information is in Indian actor, film producer who works in Indian cinema.
So 200 characters.
You will be able to see this.
So this in short is basically querying the Wikipedia itself.
Right now my turn is basically to create a graph.
Right.
So we will go ahead and create graph.
Or we'll go ahead and create AI agents.
A AI agent application using line graph.
Because if you understand from our architecture we have also developed this.
We have developed this.
We have also developed router.
Now it's like we need to connect this in the form of graph right.
Like how this graph is entirely there.
So let's go ahead and do that.
So agents application using line graph.
So first of all I will go ahead and create one class.
Let me just go ahead and create one class.
So here I have from typing import list typing extension import Typekit.
So I'm going to create a graph state which will be my class which is inheriting type dictionary.
And this will be in the form of key value pairs like question is equal to STR generation is equal to
x, str and documents list of str okay.
So I'm basically creating three variables.
This three variables will be responsible in managing the entire state of the AI agents that I've actually
created.
So it represents the state of a graph.
Attributes are this many things.
Okay.
So for right now just keep a note of that.
Okay.
You'll be able to understand once we start this.
Okay.
Now let's do one thing.
Whenever let's say whenever I ask or whenever a query is basically coming, whenever we give that query
to the vector database, then for that particular vector database, what we really need to do is that
we need to probably retrieve from the vector database.
And we need to give the response okay.
Whenever we get a response that will basically be stored in this documents, okay.
In this document variable and whatever question user is basically asking it will get stored over here.
Right.
So here I'm going to probably go ahead and create from line chain I'm going to import dot schema.
So dot schema I'm going to import something called as document.
Okay.
Then I will write Definition retrieve.
Definition, retrieve.
Let's say I'm going to take some state over here.
So this state that we have defined it will be a type of this graph state which will have this three
information.
Okay.
And when we are calling this retrieve we are just going to give that particular parameter.
So here what we are basically going to do I will just write down some of the comments so that you'll
be able to understand what is happening inside this function.
So here you can see State dictionary the current graph state and returns a state dictionary itself.
Okay so here what I'm actually going to write I'll write print print retrieve.
Retrieve.
Dash dash dash.
Okay I'm just going to write this print.
And then let me quickly go ahead and write my question.
Question is equal to obviously the question of state which should be coming from this, because the
state, which is a type of graph state, will be keeping in a node of the question.
So we are going to probably consider the state over here.
And then we are going to go to the next line.
Uh, here we are specifically going to do the retrieval or for doing the retrieval I will go ahead and
write documents is equal to retriever dot invoke.
And here I'm going to use the question okay.
Now let me just go ahead and return.
Return.
The documents colon documents whatever documents we are going to specifically get comma.
So this should be my question over here okay.
Oops.
Questions okay.
And the second parameter that I'm going to retrieve is basically my question which will be equal to
this particular questions.
So this is what we are specifically returning.
Okay.
And just through this you will be able to see this.
Okay.
So overall I hope you are able to understand what we are basically doing with this retrieve.
Retrieve basically means we are just going to query it from the retriever, and we are going to just
display it.
Okay.
So I hope everybody was able to understand okay there was some indentation issues.
No worries.
I will just use this.
Let's see.
It should be fixed.
Okay.
Perfect.
So now it gets executed.
So retrieve function is basically done.
Now similarly I will go ahead and do it for the Wikipedia search.
So for that I can just copy and do this.
So for Wikipedia search there will be a functionality where I take the same state.
I will write print Wikipedia over here.
No need to print.
Hello.
I'll take the question.
I'll print the question over here.
Then we'll go ahead and write wiki dot invoke query of question and then whatever docs I'm actually
getting that is my wiki results.
I'll convert that into a document and I hope I have imported the document also.
So this should be imported now, right?
And now this.
Finally, I'm probably doing the Wikipedia search and converting it to a document and returning back
all the results.
Okay.
So perfect.
Uh, so what?
All things have been implemented, we have implemented this functionality.
We have implemented this functionality, we have implemented this functionality, even the LLM part
also we have implemented along with the prompt engineering.
So in short why do we say multi agents.
Because here they are multi agents.
One is communicating with the astrolabe.
One is retrieving the result.
From there.
One is doing the Wikipedia search and all of them are interacting with each other.
So finally uh now it's time that we go ahead and probably do all these things.
But one thing, uh, that we missed out, right, uh, over here is with respect to the routing functionality,
because that routing functionality needs to be also defined as a function.
Right?
So here I will just go ahead and define this.
Let's go ahead and do this.
And I think you should be able to get this.
I'll be getting state of question I'll say question router dot invoke this one okay.
And where do you get the question router.
So question router.
We have already created it earlier right.
If you remember we have created over here question router.
So we are going to use that question router over here.
And here only you will be able to see that we are just writing this particular question.
And I'm saying if source dot data source is wiki search then we return wiki search.
Otherwise it is a vector store.
We return vector store.
So all the functionalities, the chunk of functionalities of all the nodes has been created.
Okay, now it's time that we start creating our entire land graph flow, right?
So I'll write from land graph dot graph import and then you have this state graph.
Then you have this the start.
Okay.
Start now.
The first thing is that I will start my workflow.
I will go ahead and write.
Workflow is equal to state graph.
And here I'm going to use my graph state.
Whatever graph state I've actually defined that is my default class okay.
Then we will go ahead and define the nodes.
What are nodes you have over here.
Main important nodes.
One is the wiki search node and one is the vector db.
Now in order to create the nodes in a line graph, I will just go ahead and write this one workflow
dot add node wiki search.
Then you have this retrieve function.
What is the function name you can see over here retrieve.
Okay.
So I will just go ahead and write this name because the spelling is wrong over there.
So I will go ahead and define this.
Okay.
No worries.
So here uh, one node name is wiki search.
The functionality is this.
Then I have add node.
I'm adding retrieve which is the functionality.
Is this okay?
This is basically do the web search, that is Wikipedia search.
And the other one is basically to do the vector search.
Okay.
Now I'm going to build the entire graph.
This is amazing.
This is just amazing.
Okay.
Workflow dot add conditional logic.
So here I'm also going to go ahead and add conditional logics because this conditional logics will be
really important.
So sorry.
Add conditional edges okay.
Should not be logic.
So first of all we start with start.
Then we go with our root question right.
So here I'll just go ahead and define my root question.
And you know in root question what is basically going to happen.
It is going to go in this two terms right.
One is one is wiki search wiki search colon.
And then I have this wiki underscore search.
Okay.
And the second one is nothing but vector store.
The vector store will go to my which node.
See this is this will basically go to this particular node.
This node is defined over here.
Vector store should go to my retrieve node.
So here I'm going to basically go ahead and define my retrieve node.
Done.
Now this is my root question.
Okay.
So start we have started it over here.
We have put a conditional edges saying that when we are routing whenever this function is written it
should either go to wiki search that is this or this particular node that is retrieve.
Okay.
Then after completing this, what is the next step is that we go ahead and add workflow dot add underscore.
Edge is nothing from from retrieve.
Where do we go after?
Probably if we complete this retrieve where do we go see over here after we do the retrieve.
Retrieve is basically done.
From here we go to our end stage.
Right?
So end stage will go ahead and connect it.
So here I'm going to basically connect my end stage.
And then I will also go ahead and write workflow dot add wiki search should also be going to the end
stage right now.
Finally we do compile this entire graph.
So I will write app is equal to workflow dot compile okay.
And now this entire compile will happen.
Root question is there.
Let's see okay I have to give comma over here.
Then all it will work okay.
So let me just go ahead and execute this.
So root question here two options I have given them.
Then over here that I have this entire workflow.
And all the compilation is specifically happening.
Now in order to just display this entire graph, I can just use this image and display from IPython
display.
Right.
And this is what is the function app dot get graph dot draw mermaid png.
So this I probably show from the documentation.
So here you can see C start vector store retrieve.
Otherwise Wikipedia.
And in between there is something called as router.
Also that router is not getting displayed but the entire flow will happen in that way.
Okay.
Now in order to execute and see how things are basically happening, you can actually check this out,
okay.
And you will be definitely able to see this okay.
So I will just go ahead and print one of the code.
So here you can see what is agent I'm asking the input I can add multiple inputs if I want.
So I'll say for output in app dot stream input.
So key value output items I'm printing the key.
And then we are printing the document of zero dot dictionary dot metadata dot description.
It should be able to get the answer.
So once I execute this here you can see building agents with LM as it is control.
So when I ask this question this is saying hey route the question to rag that is retrieve.
This is the what is I have printed it out there.
Right.
And finally I am able to call this node retrieve function.
And finally I'm getting the output.
Similarly, if I go ahead and ask some other question, let's say I ask a basic questions like Avengers
or something.
Some movies right here.
My question is Avengers, and if I just go ahead and print the same documents you should be able to
see now it is routing to Wikipedia search.
Right?
So this was about this amazing end to end Rag application.
Land graph applications are basically a multi agents where we have added conditional edges.
We have seen how each and every node functionalities will be created and many more things.
So I hope you like this particular video.
Definitely go ahead and check it out.
With respect to astrolabe, because this was quite amazing.
Because of this we are able to do this also.
And the vector database search that we are able to do with the help of retriever.
It was possible.
And again here we have used grok and the entire application has been basically created.
Right.
So this was it from my side.
I hope you like this particular video.
I will see you in the next video.
Thank you.
Take care.