- [Laurence] It's time to write some code.
So, in this video, we're going to build
a very simple Hello World agent,
and this will help us see the concepts
of intent, planning, tools, and reflection in action.
Our agent's goal is going to be very simple,
it's to greet the user,
but instead of just printing a static message,
it's going to use a tool to generate the greeting.
So, let's get started with this one,
and I've started by just creating
this file called hello_agent.py,
and I'm going to start coding in it right away.
Okay, first of all is to define the tool,
so I'm going to call the tool create_greeting.
It's going to take the name as a string.
I'll just have that return a string.
And then here I'm going to return, "Hello,"
to the name that was passed in.
And maybe just something simple like,
"Welcome to the world
of AI agents."
Okay, so that's my very simple tool.
So, now let's take a look at the main logic
of the actual agent itself,
so I'm just going to call that run_agent.
So, step one is going to be to understand the intent.
Now in this case, understanding the intent is very simple.
We're just getting the user's name.
Typically with an agent,
you're going to have like a vague thing
that you want the agent to do that's an unstructured text,
like, "Find me a place where I could hike today,"
or, "Find me a restaurant that serves my favorite dish
that's open after nine o'clock on Mondays
and is within 25 miles of me."
And the whole idea of intent is being able
to take that big block of text
and really understand what the user wants.
It's one of the magical things of large language models
compared to old-style natural language processing,
where you can throw that block of text and say,
"Help me figure out the intent.
What does the person want to do?
Do they want to hike, do they want to go to a restaurant?
Tell me those details."
And then to turn that unstructured data
into structured data.
But in this case, the agent is much, much simpler than that.
The intent is to just figure out the person's name
so that you can say hello to them.
And in this case, we're not going to bother with an LLM,
we're just going to ask them their name.
So, then the second part is when your agent understands
what the tools are available to it,
it's got to plan to be able to use those.
So, I'm going to put the plan together,
but of course, here my plan
is just to use the greeting tool.
We're just going to say hello to that person.
So, my plan is I'll just put out something,
I'll print that the agent is going to say,
"I should use the create greeting tool."
I know I'm cheating a little bit,
but the whole idea is just to show that level of
breaking it down into the steps that an agent would do.
And again, typically, if you're thinking about
the vague intent that I mentioned earlier on,
I would like to have dinner of a particular cuisine,
or I'd like to hike in a particular place.
Then once you've established the intent from that,
then you could also call a large language model to say,
okay, my user wants to do this.
These are the tools that are available to me.
Help me break it down into a plan to use those tools,
that type of thing.
But in this case, I just have a single tool
and I'm going to bypass all of that and just create a greeting.
So, create_greeting, and the user has entered their name.
So, now I'll call that create greeting tool to do that.
And then finally, of course,
the final step was to use the tools.
I should put that here.
This we're actually using the tools was step three.
And then finally step four is to reflect on what we've done.
And in this case, reflecting would be, did it work?
Did we create a greeting?
Did something go wrong?
And because I'm returning my greeting, I could just say,
if greeting,
print,
agent
says
the greeting that was returned.
Otherwise it failed on the greeting,
so I'm just going to say, print,
agent says
I failed
to create a greeting.
If you wanted to make it
a little bit more sophisticated there,
you could have it do something like,
if it failed to create the greeting,
maybe it could go back and ask or tell the user to be
more specific about the data that they put in,
or something along those lines.
But again, this is just a very simple
Hello World-type thing.
And we just want to show all of the steps
that would be in creating an agent.
So, if you're a Python developer, you'll be like,
why on earth is he doing it this way?
There's a much simpler way of doing that.
All you have to do is input and print.
But the whole idea here
is to start getting into that mindset
when you're building an agentic AI
to think about breaking it into those steps
of understanding intent, making a plan,
using tools to achieve that plan,
and then reflecting on them.
So, this is our app, let's see what happens if we run it.
Hopefully I don't have any bugs in there, but...
So, now to run it, I'm just going to do
python hello_agent.py.
It's going to ask me my name, I'll tell it the truth, Laurence.
And you can see here,
the agent decided to use the greeting tool,
the create greeting tool created the string,
"Hello, Laurence, welcome to the world of AI agents."
And then the agent outputted that to me.
So, let's quickly break that down into our agentic loop.
And you'll hear me saying this one a lot,
but it is important.
Breaking it down your applications
when you want to build AI agents
is to really think about
how you're going to meet those four steps,
understanding the user's intent.
Now, you can do that with AI or not.
Making a plan, you can do that with AI or not.
Using the tools, here's the important part
where the agent executes the plan
by calling whatever tools are available to it.
In this case, it was just that create greeting function,
and then reflect.
And in this case, the agent was able
to check the output of the tool.
It got a valid greeting, so it printed it.
Otherwise it would acknowledge that it failed.
And there you have it.
This is the very first, very, very basic AI agent.
The next module, we're going to start building out
our much more advanced hiking assistant agent.