Welcome to the tools section of this course.
So, in this example, we're going to make
some tools and going to try and use
them against an agent.
So, you can make both informational tools and
action tools.
And in this case, I have made some
tools down here.
In terms of, I have a person info
record here with just name and favourite colour
of people.
And I made a few tools that can
work with them.
I have a set of data here with
just some names and some favourite colours.
And then I exposed two tools.
I exposed a get persons, and I've just
put in so we can see that when
they are called.
And I get a person by name.
This is just a C# class.
It has nothing to do with AI or
tools or anything.
But it's just a class, and we are
going to turn this into a tool.
In the same manner, we just have a
small static class down here.
That's called change colour of the console.
And again, we are calling so we can
see what happens.
So, this is how we define our tools.
Just plain C# code.
In this case, it's something that just works
here in memory.
But it could easily have gone out to
a hard drive.
It could have gone into another API.
The action could have been to place an
order in a system or whatever you want.
It's just C#, and it can be
pretty much anything.
And it can take input, and it can
take output.
So, what we're going to do is we're
going to make our normal Azure OpenAI client.
We are going to new up our tool
because we need that a little later on
here.
And then we're going to make our agent.
And we have told the agent it knows
people, and it can change colours of the
console.
We probably don't need to tell it this.
Sometimes when it's very advanced tools, you might
want an instruction to say, when this happened,
call this tool, when this happened, call this
tool.
So, I could, for example, here say change
the colours of the console.
I could have told that that tool is
called change console colour.
In this case, it's so obvious that we
actually don't need it.
Then we have our tools.
And our tools are just something called an
AI function factory, where we say create.
And there we give some kind of delegate.
In this case, it's the get person method
down here that we are getting.
And we're telling it this is the name
of the tool.
And tools and AIs in general really like
this snake case with the underscores.
So, we just call it get persons, and
we tell here's the description of the tool
that it can get all persons you know,
you meaning the AI.
And get persons, so you get a specific
person by name.
And the final tool is just static methods.
So, we're going to use that and say
change console colour, and change the colour of
the console.
And beyond that, it's the same chat loop
that we have done in the past.
So, let's see this in action on how
it works.
So, we can ask it a set of
questions.
For example, we could ask, what is John's
favourite colour?
And it will come back and say John's
favourite colour is red.
We could then ask it, who have blue
as their favourite colour?
And we saw that up here it called
get person because it could do that.
And we can set breakpoints in here if
we want to and see what's going on.
So, we can do that.
We get a breakpoint.
We call it to get person.
And it tells that Rasmus have blue as
their favourite colour, which was down here as
well.
So, that is fairly simple to work with
information like that.
And we can, in a similar manner, do
actions.
So, we can say change the colour to
green.
And it will now change the console colour
to green because we essentially just ran this
piece of code being our action in our
case.
But because this is AI, it can also
be more advanced than just simple commands like
this.
So, for example, what we could do is
we could go in and say, change the
colour of the main colour of the Canadian
flag.
And now it knew that the Canadian flag
is red and white and the main colour
is red.
So, it used that information to grab that
and run it.
In a similar manner, let me try to
restart this just so it's even easier to
see what is going on here.
If I go in and say, change the
colour of Rasmus to Rasmus' favourite colour, what
will happen is it's going to call persons.
So, it finds all persons.
In this case, it was unsure who Rasmus
was, so it didn't dare try to call
this, so it's confirming.
It found me, it found the blue, and
then it changed the colour by chaining the
two tools together.
So, it called this tool in order to
call this tool after the fact.
And you can chain tooling a lot of
different things.
So, we could say, change colour to yellow
and then green.
So, now it called both.
We didn't see anything of yellow, but of
course, it just follows instructions as it should.
And again, these are just small party tricks
here, but it could just as easily have
been get the information for order number this
and this in this API, get the weather
for Paris, place this order into the system,
because if we don't provide all information, it
is intelligent enough to say, okay, I also
need this, this, and this in order to
call this tool.
So, tools are very, very powerful.
You just write some code that would normally
be called, and then you create them using
these AI function factories.