WEBVTT

00:00.760 --> 00:05.760
And now we want to import the create agent function from LinkedIn.

00:08.280 --> 00:13.840
Now, in order to create an agent, we need to give the agent two minimal things.

00:14.120 --> 00:19.520
The first one is going to be tools, and the second is going to be an LM, which is going to be the

00:19.520 --> 00:20.440
reasoning engine.

00:20.600 --> 00:26.760
So let's go and import the tool decorator from LinkedIn.

00:27.960 --> 00:31.840
Let's go and import from LinkedIn core.

00:31.880 --> 00:34.600
I want to import human message.

00:34.840 --> 00:39.480
And we're going to be using human message to invoke the agent.

00:39.480 --> 00:42.760
So this is going to be the input for the agent execution.

00:43.280 --> 00:48.200
And lastly let's go and import from OpenAI chat OpenAI.

00:49.040 --> 00:53.960
And this is going to supply for our agent our LM that we're going to be using.

00:54.240 --> 01:03.710
All right so now is a good point to introduce blockchain tools and tools for the agent and tools for

01:03.710 --> 01:04.430
an LLM.

01:04.630 --> 01:06.830
And let's go and clarify this topic.

01:07.030 --> 01:07.710
All right.

01:07.710 --> 01:14.950
So a tool is a function that an agent can execute.

01:15.150 --> 01:17.710
It can be any function that we want.

01:17.750 --> 01:21.830
So we can write the internal implementation of that function.

01:22.070 --> 01:28.990
So this means we can give our agent endless possibilities of actions it can perform.

01:29.350 --> 01:35.270
So we can make it call an API to search in a database to run and execute code.

01:35.470 --> 01:37.270
We have total flexibility.

01:37.310 --> 01:38.790
What this tool can do.

01:39.110 --> 01:44.550
And once we write this tool, we can plug it into the agent and boom!

01:44.550 --> 01:49.830
Like magic, the agent has the ability to go and execute this tool.

01:50.310 --> 01:53.630
And creating a tool is actually very, very simple.

01:53.830 --> 02:01.630
All we need to do is to implement a function, a regular Python function with type hints and with some

02:01.670 --> 02:02.430
docstrings.

02:02.430 --> 02:08.670
So we'll have some description of what this function does, what arguments does it receive and what

02:08.710 --> 02:10.150
output does it output.

02:10.430 --> 02:14.030
We then use the tool decorator by link chain.

02:14.270 --> 02:21.110
This transforms this regular Python function into a tool which can be plugged in into an agent.

02:21.430 --> 02:23.670
Now I want to emphasize a couple of things.

02:23.710 --> 02:33.230
Now the type hinting the arguments, names and the docstrings of the function are very, very important

02:33.230 --> 02:35.430
because underneath the hood.

02:35.430 --> 02:40.830
And trust me, we'll be learning exactly how this works in later in this course.

02:41.030 --> 02:47.670
But underneath the hood, the large language model is going to use this description and the arguments

02:47.990 --> 02:48.790
type hints.

02:48.790 --> 02:54.670
And it's going to be using that in order to choose whether to call this tool or not, and with which

02:54.710 --> 03:01.550
arguments and state of the art models these days support this functionality using something which is

03:01.660 --> 03:06.220
called function calling, which we will be elaborating a lot on this course.

03:06.540 --> 03:14.300
Now, the gist of function calling is that instead of LMS just returning text or pictures or videos,

03:14.580 --> 03:21.980
they can also return a special response, which is a function that needs to be invoked in case it decides

03:21.980 --> 03:22.260
to.

03:22.460 --> 03:26.620
And this is an extra capability that an LM can provide us.

03:26.740 --> 03:30.460
And we are going to be treating this like a black box right now.

03:30.580 --> 03:33.060
So we're not going to be diving deep into this.

03:33.100 --> 03:35.340
We have plenty of time to do this in the course.

03:35.380 --> 03:35.900
All right.

03:35.900 --> 03:37.140
So back to the code.

03:37.180 --> 03:39.620
We are going to implement a search agent.

03:39.620 --> 03:42.020
So it's going to have a search tool.

03:42.220 --> 03:45.300
And the search tool is going to receive a query.

03:45.340 --> 03:47.780
It's going to search it in the internet.

03:47.780 --> 03:49.940
And then it's going to output the response.

03:49.940 --> 03:50.540
Right.

03:50.580 --> 03:54.900
So this is the Python signature of the tool we're going to be writing.

03:54.900 --> 03:57.820
So this is a custom tool that we're going to be writing here.

03:58.140 --> 04:02.370
And let me write here a very simple implementation.

04:02.370 --> 04:07.170
Let's simply here print the query and let's return the string.

04:07.210 --> 04:09.490
Tokyo weather is sunny right now.

04:09.490 --> 04:11.330
This is a static function.

04:11.330 --> 04:15.050
It doesn't really search over the internet, it simply returns the string.

04:15.330 --> 04:17.370
Tokyo weather is sunny.

04:17.490 --> 04:19.410
This is what this function is doing.

04:19.570 --> 04:22.970
Of course, we'll be writing the real implementation in this video.

04:23.370 --> 04:27.210
So let's go now and add to this function the doc strings.

04:27.370 --> 04:32.290
And here I have a description that this is a tool that searches over the internet.

04:32.330 --> 04:36.570
The argument is query and it returns the search result.

04:36.650 --> 04:42.490
So this is not the best description right now but it will suffice for this example.

04:42.490 --> 04:50.050
In general, the rule of thumb is that we want to make this description as explicit and unambiguous

04:50.050 --> 04:51.210
as possible.

04:51.210 --> 04:55.690
So the LLM would really have an easy time to decide whether to call this tool or not.

04:55.730 --> 04:56.290
All right.

04:56.290 --> 05:02.010
So this is now a regular Python function if we want to convert it into a tool.

05:02.080 --> 05:05.960
Let's go and decorate it with the tool decorator of chain.

05:06.240 --> 05:11.360
And this is going to convert this Python function into a link chain tool.

05:11.560 --> 05:15.200
Now what is exactly a link chain tool.

05:15.240 --> 05:18.120
This is still not clear at this point of time.

05:18.200 --> 05:20.400
Trust me, we'll get into that later.

05:20.600 --> 05:28.080
But what chain is going to do is going to take all this metadata of the tool, name the arguments that

05:28.080 --> 05:35.560
they take the description and it's going to format it nicely, and it's going to help us plug this metadata

05:35.560 --> 05:38.480
and this information to an LLM call.

05:38.480 --> 05:40.320
This is what is going to be doing under the hood.

05:40.320 --> 05:40.800
Don't worry.

05:40.840 --> 05:42.240
We're going to soon see this.

05:42.240 --> 05:42.720
All right.

05:42.720 --> 05:47.320
So assume now we have now an object which we can inject to our agent.

05:47.320 --> 05:50.200
And it can suddenly has a certain capability.

05:50.640 --> 05:52.160
So let's go and create that agent.

05:52.160 --> 05:56.160
And for that let's go and create an instance of the LLM.

05:56.440 --> 05:59.400
And I'll initialize a chat OpenAI object.

05:59.640 --> 06:02.590
And let me now create a list of tools.

06:02.590 --> 06:05.790
So it's going to be a list containing the search tool here.

06:06.190 --> 06:09.670
And let's go now and create the agent.

06:09.950 --> 06:12.310
So let's define a variable.

06:12.310 --> 06:14.430
We'll call this variable agent.

06:15.310 --> 06:19.430
We're going to give it as an input our model.

06:19.430 --> 06:27.670
So our LM and we're going to provide it with the list of tools which is a list with a single item which

06:27.670 --> 06:29.070
has the search tool.

06:29.390 --> 06:29.870
Boom.

06:29.910 --> 06:30.470
That's it.

06:30.510 --> 06:36.270
We have now an agent ready to go, ready to run with a search capability.

06:36.470 --> 06:36.830
Right.

06:36.870 --> 06:38.070
So it has an LM.

06:38.230 --> 06:45.550
It has a list of tools which contains only one tool, which is the search tool that is allegedly supposed

06:45.550 --> 06:49.350
to do some online search, but currently it's only going to return us.

06:49.390 --> 06:56.950
Tokyo weather is sunny here, so let's go and try to run a certain query with the agent right now.

06:57.150 --> 06:57.630
All right.

06:57.630 --> 06:59.870
So this agent is actually a runnable.

06:59.870 --> 07:02.950
So let me write a result variable and let's go.

07:02.950 --> 07:04.550
Right agent dot invoke.

07:05.070 --> 07:12.350
And in order to run the agent, we need to give it a dictionary with the messages field.

07:12.670 --> 07:20.110
And the messages field should be a list of messages for the agent which is going to contain the input.

07:20.110 --> 07:21.950
So this is the user's query.

07:22.230 --> 07:29.910
So here under messages I'm going to be sending the human message alongside with the content of what's

07:29.910 --> 07:31.750
the weather in Tokyo.

07:31.910 --> 07:35.190
Now notice here that the key here is messages.

07:35.190 --> 07:38.270
But I did not provide here a list of messages.

07:38.270 --> 07:43.670
I just provided one message, one human message, not inside the list.

07:43.870 --> 07:50.550
And this is okay because link chain under the hood is going to cast this into a list containing a single

07:50.550 --> 07:52.590
element of a human message.

07:52.590 --> 07:55.270
So this is why it should still be working.

07:55.270 --> 07:58.870
If you want, you can put here this inside of a list.

07:59.270 --> 08:02.510
So let me go now and print here the result.
