Transcripts
1. What is an AI Agent ?: So let's start by
understanding what is AI agent to make things
as simple as possible. I will go with this
practical example. Here I am in Cage Pit, and I selected GPT 3.5. I will simply ask this question. Should I take an umbrella and going out today in
California, Okay. So let's try this and now GPT three will
tell me or Ch GPT, to provide you with the most accurate advice I
would need to know your specific location within California and the current
weather forecast for the day. So I think you learned in the prompt engineering course that one of the
main limitations of language models is that it only generate answers based on
the data it was trained on. So it doesn't have access
to real time data. This is why it can't
answer my question. It needs to know the
weather first before it tell me if I need
an umbrella or not. Simple. Now, if we take the same question
and go to GPT four. Let's ask the same question now. And look what will happen. GPT four is still a language model and
it has a limitation, but it answered my question. What happened? Simply, what
A I did with GPT four, if you go here and up in this, you will see with Dale web
browsing and some other tools. So it's not only a
language model here. It's the language model. It is GPT four, but combined with some
external functions and tools. So it got the weather. Then based on the weather, it suggested that
carrying umbrella might not be necessary and it provided the resource
of the weather. I contacted National
Weather Service and based on the result,
generated the answer. This is a simple form
of how AI agents work. If you want to define an
AI agent in simple terms, it is a language model, plus some tools and
external functions. These two are powered
by a technique. It allows the model,
please focus. It allows the model to think about the
question or the prompt, understand it, maybe
split into tasks, then from a list of tools
that it is assigned to, I can pick the right
tool for the task it generates the result
with the tool with the action and use the
output of the action, again, in the prompt
generate the final result. In some way, it mimics
how humans work. I hope you got the idea.
2. Basic Setup: Okay, friends. In this lecture, I will create a virtual
environment for our project, install Open AI package, and create a simple function for text generation using
pen AI and test it. So let's get started. Step one, I will go
up in a U terminal here and create a
virtual environment. Simply run this
common Python VM, and then the name of the
virtual environment, again, Wait for seconds. Perfect. You can see now the virtual environment
folder is created. Let's now activate it. I will go to the scripts
and activate and perfect. Now you can see we are inside the virtual environment and
we can install our packages. Simply run pip, install open AI to install pen I
package in our project. And perfect, step one
done, step two done. Let's now create
a simple function to generate text with pen AI. I will create a new module for pen AI to make our project somehow
structured and organized. I will say pen module by now I will create a function here that
uses the Open AI API. From pen AI, importen AI, And that's create an
environmental variables file. Get your APIK from Open
AI and paste here. And let's go back now to read
the key from our scripts, I'm going to install
the Python package, so we can read the values
from this file. Perfect. Let's go back here and load
the environmental variables. Let's import OS and
the function, perfect. Now let's create an instance of the open AI class this way, we have an open object. That we can call to generate text perfect and we
pass the pen API key. Now, let's create the function that generates text with pen AI. Here we are generate text basic, using the pen I client and we're passing the model by
the photo GPT 3.5. We can change this if you want, we pass a system prompt
and the user prompt, and we read the response, very simple and basic. We did this a lot before in the prompt engineering
course and other classes. Let me zoom out and
explain it fast. We use OpenAI package, we loaded the
environmental variables, we get the API key and
create an instance. Let's call this client
just to make things clear. We created this open AI object from the class or instance, and then we generate text using this instance and
return back the content. Let's test this. I will go here and create
a test module and go and import open
module or let's say, from open import
generate text basic. And now I will say prompt, generate a five word
sentence response is equal to generate text basic and pass the
prompt print response. Let's test this now Python
test PIs we got an error. Let's go back here.
I think it's APK. This way, we declere
the instance. Let's try and perfect. Yes. We need to add the API key as the variable
name or the parameter name, and we got the sentence. Let's clear again, and
I will say now generate a paragraph about
quantum computing. Let's run Okay. And perfect. You can see now we have a simple
function that we can call anytime to
generate text with pen AI. Step three is done, and we are ready to start
creating our agent.
3. LLM Limitation in action: Okay. Before we go and start
building our AII agent. Let's see in action and taste how language models are limited. I will go back to the
test model I created, and I will ask the same
question, if you remember, should I take an
umbrella and going out today in California, by default, the model as GPT 3.5 Tb Let's
remove the system prompt. We don't need it for now. So now, I am using this prompt
and the model is GP 3.5. Let's see the response
from the model. I recommend checking the
weather forecast for your specific location to determine if it's
raining or whatever. So you can see it
doesn't have access to any live data to
decide based on that. Again, I will clear and
test with GPT four. It tells you, I am currently unable to
access real time data. It's again, GPT four and doesn't
have access to any data. Remember, when we
asked this question to C GPT powered by GPT
four and other tools, it was able to access and answer this question
because it's using external tools and this kind of an AI agent, as
you mentioned before. That's what we are
going to build from scratch together in this course. It got the forecast and then it responses based on that data. But if you contact
the model directly, without ChagpT without
any external tools, it is limited. You see? As an AI assistant, I am currently unable to
access real time data. This is the limitation
that we are going to tackle and solve
with AI agents.
4. Hardcoded AI Agent: Okay, friends. So from now on, we will start solving the problem and limitations
of language models. And we will give the
language model access to external tools. But in this lecture, I'll be showing you how
to build what I call a hard coded AI
agent. Let's start. And I think with the example, you'll understand the point. So If you remember, we said if we want to pass
this limitation and make GPT 43.5 or any language model answer questions that
requires real time data, we will need to give it access to external
functions and tools. So I will go here and say, sample functions and
create a simple function that simply returns the
weather based on city. It's very simple and basic
returns sunny, rainy, cloudy, and so on, based on
the city, the perimeter. As I told you before, I'm keeping things
very, very simple, so anyone can
understand the concept before moving on with
advanced examples. There's a very basic function in Python that returns
a simple text, which is the weather
based on the city. Okay? Now, I will go here
and create a new module, and I will call it
hard coded agent. You understand what I
mean in a little bit. This is the hard coded agent. In this scenario, we are going
ourselves, the programmer. We are going to call
the custom function, which is get weather, and we are going to pass the result and inject it in the prompt before
calling the model. If I go back into the test, I will copy the same. Go here again to the agent, and let's say hard coded agent. Now before we run this
sample which is limited, I will import my get
weather function and say current weather and get from California
from this function. Now in the prompt, I would say based on the following weather
condition and pass the current to weather that we got from the function. Let's make this clear by splitting into
multiple lines this way and remove the pluses and add the current weather
this and format the string. So we can see now our prompt
is totally different. We asked the question, but we injected the result from the function inside
the prompt itself. So now the model knows the current weather
based on this function, and we'll answer based
on that. Simple, huh? Let's try now to run
this script again, Python and you run
hard coded agent. Let's see. Based on the weather conditions
you described as sunny, you typically wouldn't
need an umbrella. You see? Now it answered the question and we passed the limitation. But it's hard coded, which means that we
call the function ourselves in the code and passed it to the
prompt ourselves, injected the value
and then called the model to generate the response or
answer the question. While an autonomous AI agent that we are going
to build later, the AI model by itself will understand if it needs a function and it will
call it by itself. So everything will be automated. This is the main difference. This is a hard coded solution. It looks similar to
the automation work flows we built in the prompt engineering course or what I call a function chain. So you create a function
and you chain it inside a prompt and
then call the model. This is what I call a
hard coded solution to solve the limitation
in language model. The next step is to
optimize or improve or build an automated solution that can do all this by itself. Prepare yourself for some
magic coming up next.
5. The ReAct Prompt: Okay. So in the last example, I showed you how to
create what we call the hard coded agent or
hard coded workflow, and we solve the
limitation of LM by injecting the result
of the function or the output of the
function directly inside the prompt and
we got the result. Now it's time to level up and see how we can
automate this, how we can make AI or the
agent call the function automatically and
passing it automatically to the prompt or the
question to answer it. In simple terms, how
to automate this, how we can build an agent that thinks and
responds automatically. Please from now on
focus very well. I will go step by step, and I will split the ID into multiple lectures to
make things simple. What I will do now is
to create a new module, and I will call it prompts. The whole idea is
about this prompt. Let's zoom out. This is what
we call the react prompt. I don't know if you
heard about react. It's a type of prompting that allows
the model to think about the user input or
the question or the prompt and take the light action to
answer the question. Here is the prompt I'm using. You were run in a
loop of thought, action, pose, and
action response. At the end of the loop, you output an answer. Then I explain this.
I tell them mod the thought is to understand the question
you have been asked. So it will use thought to understand the
user requirement. Then it will use action to run one of the actions
available to you. So based on its
understanding to the task, it will pick an
action and run it. And then the action response is the result of the
actions it's selected. Then I will tell the model what actions are
available to use. In our case, we have the
get weather function. So we'll call it this
way, get weather, and the parameters
is California, which is the city name, and then returns the current
weather state of the city. So this is the
available functions or actions to the model. And at the end, I will
give an example session. So I will tell it, for
example, if the question was, should I take an umbrella
in California today, The thought will be, I should check the weather in
California first. So this is how humans think. When I ask you, should
I take an umbrella? You will think first
about the weather? Is it sunny? Is it rainy? Then you will check the weather. You use an action, for example, you will check the forecast
online or something. In our case, we have a
function to get the weather. Then it will pause,
and then it will be cold again with
the action response. For example, if the
action response was, weather in California is sunny. So we got now the weather so we can then answer the question
based on this output. The output would be
something like, no, I should not take an umbrella today because the
weather is sunny. So we are giving
an example session for the model to understand
how it will work. So again, this very act prom, it allows the model to think about the question or the
prompt it was asked and try to understand it
and then understand what tasks or what actions should it take to
solve the problem, and we'll give it a
list of actions to use. It will use these actions, get the response, and use
the prompt to get the final. Answer. Let's see now how this prompt will work
in our scenario. So I will go and create
a new model again, and I will say,
react agent test. And I will copy now the
hard coded agent here. And I will remove this one. I will keep the question
as it is in L four, and then I will pass the
system here the system prompt. Simply I will pass
the system prompt, but let's import it before. I will say prompt, react system prompt, perfect, and I will pass it here. So the only tweak we
did here is we got the prompt again to a simple question without
providing any data. We move the function,
and now we will see how the model will work with
this new system prompt. Let me change
California also to, for example,
Arizona, other city. We don't want a perfect match. Just to make sure the prompt
is working perfectly, and it is somehow generic. So let's run this script, Python and copy the script name. And let's see now
what will happen. Please focus on the output. And perfect. You can see
now is somehow thinking. It has a thought. This
is the first step. If you go back to the prompt, you see the first step is thought to understand
the question. So the thought is, I need to check the current
weather in Arizona. Then the action it
picks an action, which is get weather and the parameters at Arizona,
and then it poses. You see, we reached
this point, perfect. But since we don't
have the function yet, the model doesn't have access to any function in this test. We didn't define any
functions here, anything. So it just paused and we
didn't have the final output. But that's perfect for this
lecture for this point. The idea that I want
to show you here is this pact prompt will allow the model to understand
the question and decide an action and run
it and pick the action, we reached this point
where the model can automatically pick an action to use and then
answer the question. The next step, is to define this function so the model
can call it and use it.
6. Adding Functions: Okay. So in the last step, the agent was able to think about the question and
decide the action. But since there is no action
defined in our script, the model didn't call any
actions or any functions. So in this lecture, let's go to level three
and make the model automatically call the action needed for it to
answer the question. So I will go here and
applicate This script, I want to keep track for
every update so you can go to each script and follow
up step by step to see how we develop the
agent from scratch. In fact, I will rename this to R A to make the naming
somehow smaller, R A, which stands for react agent, and I will say with
functions test, and this one, the basic test RA. And now I will copy the
same code and move it here, and I'll define the set of functions available
to the model. So let's remove this. I will say here,
available actions, and simply I define a list that contains the name
of each function. And let's import the function from sample functions
and port get to weather. So we have a list that
the model can pick from. In our case, we have only one to make things simple
as I mentioned before, but this is a list, the model can access and
pick a function and call it. We will see how in a little bit. We have a list of
available actions. Now, what I'm going to
do is I want to read the action name from the output and instruct the
model to call this function. You can see the model
knew that we need to get weather function with
Arizona as a parameter. But how it will
call it, let's see. So now we have the response. Here, we want to instruct the model to call the
action or the function. So what we need is to extract the function name
from the response, and the response is
a text response. So my little trick here
is inside our prompt. Instead of returning A
simple text like this, I'm going to return
a structured Jason. It would be a lot
easier for us to read the name and
the parameters. What I will do here is I will
go to the example session and you will see here that the action returned
is in this format, the function name, and
then the parameter. Instead, I will tell the model to return this in JS this way. So instead of this
simple text here, we have a JSN inside the
text so we can extract it easily from the text from the response and
call the function. Let's go back to our test here, and before we write the code
to extract the function, let me show you what
happened, what changed now. I will run this in script, And you can see now, the
thought is the same. I need to check the
weather in Arizona first, but now the action
is in Jason format. And this is super important. Now I can easily read this Jason string and convert into Jason and
call the function. I hope you got the
idea because if you have a simple text
here as before, Maybe we need
regular expressions. It will be somehow harder
to extract a function or a simple string from
within the response. When we have a structured
Jason like this one, it will be way easier. But how we can extract this
JSN text from this response. Here it comes my helper
functions that I developed, I created to make
things easier for you. I will go here and create
a new Python module, I will call it JS helpers I will paste the functions I created This can help
you in many scenarios. But what we need in our
case is this function, extract Jason from text. Look how easy is this now. I'll go back to our
example and simply I will say Jason function is equal
to the function name. Let's first import it from
Jason helpers import. What's the name of
function? Extract Jason. Helpers and simply say extract
JS and from the response, and let's now print
the JSN function. Let's also print the response, I would say response from model, And then I will say extracted JS functions past
this formdatring. And let's now see
what will happen. So what I'm doing here
simply is printing the main response and then
extracting the JS function, and then printing the
extracted function to see if everything
is working perfectly. So let's run this You can see. This is the main
response from the model. Then the extracted JSN function. You can see we have now a list of function
with its parameters. You can see the
function name and the function parameters
City, Arizona. Now we have a JCN. It's way easier to just read
the values from this JSN. I hope you got the idea here. I instructed the model
here with the prompt generate the action
and the parameter or the function with the
parameter in JSM format, so it will be easier
for me to extract it later and allow the
model to run this function. Let's comment these lines. Now I have the JSN
function as text. The next step is to
execute the function. Let me show you the code.
It's somehow simple. I just checked if this JSN function exists
when we extract it. Then I get the function name
and the function parameters. You can see now how
easy it is to get these from the JSIN by
axing the JSN directly. Then I check the function name exists in the available
actions list. Where is this list, remember, we defined it here. It will check if this function
name exists in this list. If not, it will say
unknown action. If yes, it will print running the action name
with the parameters, then I'll define
the function and code it here with the
parameters and save the result inside this
variable and simply show this function result
message action response, which is the result of the
function, here the result, and let's print print
function result message. That's it. We extracted the JSN, then we extract
the function name and the function parameters, and we check if it's
available here, and we get the function
name and call it, get the result, and
I'm printing it now to show everything is
working as expected. Let's clear this again
and try our new code. Let's see what will
happen now, run, And you can see now, it
says running, get weather. So the mode automatically
picked the function and ran it. But we have here a problem. It says the action
response is none. Why? Simply because if we
go to our sample function, we don't have
Arizona in the list. So let's pick something
that is available here like London and go here and
change the prompt, should I take umbrella
with me in London, and let's now try to
execute and perfect. You can see now
running get weather, the city is London and the
action response is cloudy. The model now in this step was able to decide that it
must check the weather, I tran the function, and we get the response. The next step, is
to make the model, use the response to answer our main prompt,
our main question. And there's where we'll
need the AI agent loop. If we go again to the prompt, remember, we have a loop. So we will explain more about
this loop and we'll create our own agent loop
so the model or the AI agent can respond
with the final answer.
7. The Loop: Okay, friends, on
the last lecture, we reached a point
where the model we're able to get the function, extract it from the response, and call it and get a
result from the function. So in this example, for London, we got that
the response is cloudy. So now the model knows that the weather in London is cloudy. But it didn't generate
the final output. This is where the
loop is important. If we go back at the
prompts, remember, you run in a loop of thought
action and action response. So what we should do now is we should make the model
run inside a loop. In this way, it will have the action response in the
second iteration of the loop. So in the first iteration, it got the response in
the second iteration. It will have the response so
it can answer the question. I hope we got the idea
because in this script here, we entered the prom and we extract the function
and we got the result. But we stopped here. The model didn't
know how to answer. We just got the response
from the function. So we need to nest this
code here inside a loop. So the model can run again, and now it will hold
the information. It will have the action
response inside the messages. To do that, in this lecture, I'm going to introduce
a new function. Go back to our open module here. I will create this new function. And instead of taking a
simple prompt and the system prompt and creating the messages manually here in
the function body, simply I'll be passing the
messages as a parameter. So somehow we can make the model, remember
the conversation. We will save the conversation
within the loop. So we have now this function
in the next lecture, we will finalize our agent. And if you felt that there's
something here unclear, I think we apply it practically
in the next lecture, you'll understand how the
loop work and how the model and how the A agent is
working inside this loop. And you'll understand the
importance of the loop.
8. The Final Agent: Okay, friends, in this lecture. Let's finalize our AI agent by implementing the final loop. So again, I will
create a new module, I will call RA final. And this is our
final react agent. I hope with this example, you understand the full
idea behind agents. So I will copy the
same code here. Okay, let's remove
these comments. And now, what I will do
is I will add messages. Remember, we add this function here so we can pass messages. So we are going to use
this function instead. So we need to import
it here, perfect. And instead of the prompt
and the system prompt, we'll pass messages, and
we'll define them here. Messages is equal. And let's face this simple code. You know in OpenAI, we have the system prompt and the user prompt and
a conversation, and you can add as much as you want here between
user assistant as a conversation to pass with the model to start with if you want anyway So we have here
only the system prompt, which is our react
system prompt, and we have the content, which
is our simple prompt here. Now we pass this messages as a parameter to our new function, so now it will generate
based on this conversation. Till now, nothing
new, very simple. Instead of passing the prompt a system prompt each
as a parameter, we use the new function with
this messages parameter. Then we extract the
function, nothing new. Now we need to implement the
loop that we talked about. Please focus on this part.
This is very important. It's almost the
most important part in the AI agent structure. Now, instead of
directly generating the response from the AI and then extracting
and continuing, we will nest this inside a loop, as I mentioned before
in the last lecture. Simply define two variables, the turn count and
the max turns. How many times you want the Asian to run,
how many iteration, and then a simple y
loop, And you will say, while turn count is
less than max turns, it will keep looping. I will print the iteration or the turn count just to know
where I am inside the loop. And then I will add one to turn count so we can break
the loop when it reaches five and simply let's add a tab orientation here to make
the code within the loop. So, iteration one,
what will happen, we will generate the response, we will extract the function, and we will get the
action result. Okay? Now, what we need to do is to append the result to
the conversation. You see here we
have the messages. What we are going to do
now here is to append a new message that we
got a action response. We got a result
from the function. We are going to tell the model. Okay, now we have the response to so you can answer the
question. How to do this? Simply. After we get the
function result message, We simply append
this new message or new chat message to the
messages array here. Okay? So now, if you want, we'll
have something like this. The messages array will be something like
this at this stage. So now we have the
full conversation, and we can again in the second iteration
when we call this, now the messages will hold
the result from the function. So this is the
importance of the loop. We are keeping track of the results that
the model generated. We got results
from the function, so we added it to the messages. So in the second iteration, it will see that we have a
result from the function. And I will add here at the end, if we don't have
adjacent function, I would say else break. To see everything
happening within the loop, let's me here also. Print the response,
so we can see what exactly is happening
inside the agent mind. So loop, print the response, extract the function, we get
the function, we run it. We get the result
from the function. We add the result to
the messages history, and then loop again
with the new results. Let's see what will happen now. Clear, and let's run our final a implementation or a final.yn. Perfect. So In the first
iteration in the loop, the model, the agent said, I should check the
weather in London first. So I need this action. It ran the action. You see? I need get weather.
The city is London. The action response is cloudy. In the second loop, look at this awesome magic. Really, it's like magic. Something really interesting. In the second iteration, the agent now knows that
the weather is cloudy. So it tells you, yes, you should consider taking an umbrella as it's
cloudy in London today. So in the first iteration, understand the question,
I run the function, and then adds the results to the second loop to
the second iteration, and so on till it finds
the final answer. And what's interesting here, you can now ask the agent any question that requires
knowing the weather. It's not about the
umbrella only. Let me show you an example. I will change this prompt, and I will ask the
agent something else. Very basic question. What is the weather
in London today? So if you're on this, again, now the AI agent can answer
this question easily. It will tell me the
weather is cloudy. It uses the external function.'s ask some
other tricky questions to see how much intelligent
this AI agent. For example, is a good day to visit the beach in
California. Let's test this. Yes, it's a good day. The weather is Sonny. Let's try something else. It's really interesting.
Let me ask it this way. What is the weather in New York, and I will write New
York misspelled. Let's see if it can
handle this by itself. Run again. Perfect.
It handles it. Let me ask it something that
has nothing with weather. What is digital marketing? Let's see what will happen. I thought about it, and
it's a fact based question, so I got an answer directly. Maybe you are wondering
now how I can restrict this agent to answer only questions related
to a specific use case, like for example, whether
or maybe only for marketing or only for maybe
financial help and so on. How I can restrict
the model to do this. It's all about the
system prompt. I will discuss this more
in the next lecture.
9. The Prompt: So in the last lecture, we finished the AI agent, and I showed you how
the loop works and how the AI agent was able to use an external function
automatically to answer our questions. And we played with this prompt and asked different questions. And I showed you also
that it can answer generic questions like
what digital marketing. Let's run this again. And you'll see that it got an answer about
digital marketing. In this lecture, I want
to talk a little bit about the system,
the react prom. First, I want to mention that this prompt is not
the perfect prompt. Sometimes if you change
a little in this prompt, you'll get maybe better results. For example, in this block
post, and by the way, it's a great block post, here, Simon, the author uses
this prompt here. It's somehow similar,
and to be honest, I got some ideas from his blog
when I created the course. But if you see here, When he gets the function, he returns it in this way, action, the function name
and the parameter name. If you go to the Python script
he built or he created, you see using
regular expressions. This approach, in my opinion, is not the best approach
using regular expressions. This is why in my prompt, I preferred using
structure Jason response, and extract this
from the response, so we can easily interact with
the functions. Even though I'm using in some way regular expressions
to extract the JSN. But when we extract it, we will get something
like this one, which is easily accessible
and we can play with way easier than
simple plain text. So what I want to
tell you is that this prompt is not
the holy prompt. You can play with
it, you can change. I will share with you some different versions
of this prompt. While I'm preparing the course, I was playing with some
different prompt ideas. Let me show you this one. So you can see here, I have
different prompt versions. I will attach all this, so you can play and
test with if you want. Just one final note. If you want the AI
agent to answer questions only in your specific case
scenario or use case. For example, I want the agent to answer only questions
related to whether. I can add this to
the system prompt. Don't answer any
questions that are not related to the weather. And then you write
the react prompt. If you go here now
and let's test this, I ask what is digital marketing? Let's see what will
be the response. You can see I'm sorry, but I can only provide
information about the weather. This system prompt is very
important, if you want. To make your agent specific to a special use case scenario. I think from now on, you can use the same
prompt template, this react template. You can add more functions
and test with if you want, and we'll do this later. I will show you how
you can implement some advanced AI agents with different scenarios and use cases later in the
project videos. But for now, I think you
got the full idea and the importance of
this system prompt.
10. Simplify With SimplerLLM: Okay, friends. In this lecture, I will show you how to simplify
the process of building AI agents with a library
called simpler LM. So what I will do
simply is I will clone this copy paste and
simply rename it. Let's call it something
like simpler. And the idea here is simpler
LM is a library I developed. It's a Python package that will help you interact with
language models way easier. Believe me, it helped me a lot and I made it
public and open source, anyone can use it and build
AI power tools easily. Look at this now. We will not
need this module anymore, we will not need the JS
Helpers module anymore. We need the prompt and the
GT weather sample function, and simply here, install simpler LM and make sure you are in the
virtual environment. Just pip install it, wait a little bit and perfect. Now, from simple LM dot language dot LLM import LLM
and LLM provider. Look at this. Now, I will create
a new LM instance, and I will select
the provider as open AI and the model
name as GPT four. Now I have this instance. I can use it to
generate text with AI. What's really nice,
you can simply change the provider to Gemini
or autoanthropic, and the code will stay as it is. This is the main advantage
of using simple LM. With one instance, you can
create the application, and if you want to
change the model, you just change
the provider here. Anyway, so and AI, and let's go down here, the generate function
and say generate response and pass the
messages, equal messages, and we don't need the
model anymore here since it's defined
with the instance, and then simply we can
input also the JSN tools, which is built in
inside this library. We have a set of
tools, JS helpers, import extract JS from text and simply replace it
with this function here. With this library, now, you
don't need all these modules around your Python
script. Let's test it. I will run this and see what
will happen and perfect. It works as before. So now we don't need
all these modules, as I mentioned, you just need to import the library
and use the tools. Maybe you think now, it's not that big deal. I can create these files, but you'll see later
when you create more AI projects and
tools and so on, you will see how this
will be a game changer. Believe me, based
on real experience, more than 100 AI projects, this will change the way you interact with
language models.
11. Build SEO Auditor AI Agent: Hi, everyone, I hope you enjoyed this course on building AI agents from
scratch with Python. Now, in this lecture, we will build a
real world example. Something really
super interesting. I will build an agent, an AI agent that can answer
questions about web pages. It's an SEO auditor agent. So it will have direct
access to web pages, and you can ask anything
you want, for example, How many pictures are
there in this webpage. How to optimize this web
page to rank on Google. Anything you want. It's
like your SEO assistant. SEO stands for search
engine optimization, and it is the technique used
to optimize your website, so we can rank on Google
or search engines to get organic traffic. Let's build together
the SEO older AI agent. I'm sure you'll love this. It's super interesting.
Let's start. So here is a new project. I will start again
from scratch so we can review everything we
took and you will see now the importance and the power of simpler
LM in this project. Create a new Python file. This is the main file, and let's create the prompt
file, pen the terminal. Let's create a new
virtual environment. Don't worry about the codes. Everything will be attached with the course. Please focus now. Understand the concept so we can build any agent you want. Let's activate. Okay. Perfect. Install
simpler LM, Okay, perfect. Let's now add our
APIke paste here, and now let's start. First step, I will
import simpler LM and I will create a new instance as
we did in the last lecture. Now we have this that
extracts JS from text, and we have the instance
generate text with GPT for API. Now, before we move on, let's understand the asian
that we are going to build. As I mentioned, this is
an SEO auditor agent. So we can ask it anything about any web page and it can
access the web page data. It can read the web page
and answer based on that. So what is our external action, the external function?
Let's define it. Let's say actions,
for example, dot Y, and this function is
simply a function that can generate an SEO
report for any webpage. So to do that, I'll be using
a simple API I developed. It's called the website
SEO analyzer tool. If you go into rapid
API and test it, for example, this is my
website, test end point, you will see we get
in depth reput, the HTTP headers, the titles, the description, word
count, images, everything. So the data about
any webpage Perfect. So now, I will create
a function that calls this API and returns
back the SEO audit. Now, here it comes the
power of simple LM. We have built in rapid API
module in this library. So we can call any API on rapid API with
this simple class. So I will define this function. It takes a URL. I will provide the
APURL from rapid API. Let's see, this is the URL, and we have the
parameter as URL, you can see, I define
the parameters, and then I call the API with this simple function
and return the response. You can see now how simple
LM made things a lot easier now and we create the function easily with the
help of simple LM. Perfect, we have the action. Now it's time to
define the prompt. Of course, I prepared
this before. Just not to waste time now
writing the full prompt. Let's paste it and explain
it briefly. Look at this. It's almost the same action
thought action response, the same react template, but now we have different
available action. We have the get SCO
page reput you can see, and we'll have a
URL as a parameter. And this is the example session.
This is very important. Look at this. Is the
heading optimized for the keyword marketing in the web page nas.com
is my website. The thought I should generate a full SCO reput for
the web page first. It calls this function and
then based on the report, it will answer your question. You will see now how
interesting is this? This is our prompt. I
will go back to the main Session to the main script, and I will import the
function from actions, import, get a CO page report, and from import axt
and prompt, perfect. We have the action now,
we have the prompt, and we have our functions, we have the LM instance, we are ready to
create the agent. So from now on, the code is almost the same.
I will paste it. You'll see now we
have the user query, which is the question
you want to ask. For example, how many images
in the following web page. The messages, I defined them, then the turn count, the loop. You remember the loop. So
we have the agent response. By using now the
element instance to donate the response
and pass the messages, and then simply
append the response. Simply we have the
agent response, and then we extract the action and we check
if it's available. So we need to define
the available actions in remember this list,
let's define it. Let's go here and
define it available. Actions, here is the action. Now, I think
everything is perfect. I pen the message.
Okay. Let's test this. The question now
is how many images in the following web page? Let's try it Python main BY Runs we got an error API
must be provided for pi. We need to provide the
rapid APK also here, added this is my AP Ike and you can find it,
by the way, here, the rapid APIKe Let's go
back to our main code. Let's clear and try again. Now, hope it works. Function, perfect,
generating report, loop two. There are eight images
in this web page. Perfect. Let's ask a
different question. What is the response
speed of this web page. Let's run. The final
answer is 0.08 seconds. Wow, somehow fast. So you can see now we have this AI agent that is
capable of answering any question related to your web pages with the help
of an external function. This is our SEO audit AI agent. Remember that this is the
back end of our agent. We are interacting now
with the terminal. This can be a great project
for maybe a tool on your website as we explained
in my other courses on building AI
tools on WordPress, or maybe building a chat bot. Again, on WordPress, powered by an assistant like this one here. When you create this agent, and by the way, we have
infinite possibilities here, we can create any agent you want that can help
you in anything or provide this as a service to your clients on
your own website, to your own customers,
to your own visitors. It would be something
super helpful for your business or for yourself, or maybe to be more productive
in anything you want.
12. What is next?: So we reached the
end of this course. I hope you enjoyed
it to learn how to build AI agents with the help of react prompt and Python totally from scratch.
Now, what's next? The most important thing
to do is to practice, go and build your own agent. You can get all my codes. Everything is attached,
downloaded and try to tweak it, change it to build
your own custom agent. Don't forget to share everything with us with the community. We can share ideas, we can. I'm here to help you
anytime if you face any problem, post
your questions. I'm here almost every day. The second thing is
that I'll be publishing more AI Asian projects almost every month on my
video library on my website. If you like to join, can go
there and continue and see more examples and more
projects every month. Thank you and see other course.