WEBVTT

00:00.160 --> 00:04.200
And welcome back to cursor and welcome to week five directory.

00:04.240 --> 00:04.800
Here we go.

00:04.840 --> 00:06.440
We're going to the first lab.

00:06.560 --> 00:12.520
Week five day one Autogen Agent Chat, which is the main part of Autogen.

00:12.520 --> 00:14.560
That's sort of comparable with, say, crew.

00:15.040 --> 00:19.280
And a lot of what we do right now is going to look very familiar because it's very consistent with crew

00:19.280 --> 00:24.880
and OpenAI agents SDK, particularly this first thing which we always do, which is to load the env

00:25.160 --> 00:26.400
as usual.

00:26.680 --> 00:27.160
All right.

00:27.160 --> 00:29.600
So the first concept then is the model.

00:30.040 --> 00:34.320
And the model which is similar to concepts like LM that we've had before.

00:34.360 --> 00:37.800
It's like a wrapper around calling a large language model.

00:38.000 --> 00:45.760
And here we import something called OpenAI Chat completion client, which is the wrapper for for the

00:45.760 --> 00:48.640
LM we'll be using, which is GPT four mini.

00:48.720 --> 00:52.280
And this is how you create your model client as it's called.

00:52.320 --> 00:54.000
And it's very simple indeed.

00:54.240 --> 00:55.960
And you just pass in the name of your model.

00:56.200 --> 00:57.400
And so let's run that.

00:57.760 --> 01:03.400
And also I just want to show you here that you could do the same thing with Obama to run a local model

01:03.400 --> 01:04.640
like llama 3.2.

01:04.680 --> 01:06.840
It's just exactly the same idea.

01:06.840 --> 01:11.440
You could run that and you could continue all of this in exactly the same way, running locally instead

01:11.440 --> 01:13.520
of using GPT four or mini.

01:14.160 --> 01:16.920
Okay, that is the first concept.

01:17.160 --> 01:19.400
The second concept is the message.

01:19.480 --> 01:23.840
This is something which is a different concept for Autogen agent chat.

01:23.880 --> 01:27.360
It's this idea that you create an object called text message.

01:27.360 --> 01:29.440
In this case that has the content.

01:29.440 --> 01:31.760
I'd like to go to London is my message right now.

01:32.040 --> 01:34.760
And the source is the user me.

01:35.120 --> 01:38.360
And so if we run that and print it, we see it's a text message.

01:38.360 --> 01:39.480
The source is the user.

01:39.680 --> 01:42.800
And the, uh, there's the content.

01:42.920 --> 01:45.760
And now that is all there is to it.

01:45.760 --> 01:47.240
That is the message.

01:47.480 --> 01:49.960
The third concept is the agent.

01:49.960 --> 01:52.760
And it's very similar to things we've seen in the past.

01:53.160 --> 01:56.400
The the thing that we import is called assistant agent.

01:56.400 --> 01:58.920
That is the uh, you'll see this many times.

01:58.920 --> 02:06.080
This is the kind of most, the most fundamental class that will work with in in Autogen agent chat.

02:06.080 --> 02:08.960
So we create a new instance of assistant agent.

02:09.000 --> 02:15.960
We give it a name, airline agent, we give it a model client, the underlying LM, we give it a system

02:15.960 --> 02:19.480
message rather like the instructions in OpenAI.

02:19.880 --> 02:21.760
You are a helpful assistant for an airline.

02:21.760 --> 02:24.240
You give a short humorous answer.

02:24.240 --> 02:26.600
So let's give it that to see what that does to it.

02:26.840 --> 02:32.920
Uh, and uh, model client stream is how we say that we want it to stream back the results.

02:33.120 --> 02:38.640
Uh, and so it's something that we've, we've already done from time to time, but that is an agent

02:38.640 --> 02:40.360
that has been created.

02:40.680 --> 02:45.320
And then the thing that brings it all together is something called on messages.

02:45.600 --> 02:51.000
That is what we call on an agent to pass in a bunch of messages, which we do right here.

02:51.000 --> 02:52.880
We just put our single message in a list.

02:52.880 --> 02:54.560
We pass that into all messages.

02:54.560 --> 02:59.790
You also have to pass in this thing called cancellation token, which is how it knows when the messages

02:59.830 --> 03:00.390
are finished.

03:00.390 --> 03:05.630
That's a sort of fiddly thing about about agent chat, but but I wouldn't worry about it.

03:05.630 --> 03:07.190
You just call, uh.

03:07.350 --> 03:08.950
And of course it's an async.

03:08.990 --> 03:11.470
It's a coroutine, so we have to await it.

03:11.630 --> 03:15.350
Await agent on messages pass in the list.

03:15.390 --> 03:20.830
You pass in this cancellation token, which is how the framework is going to know when this agent is

03:20.830 --> 03:21.550
completed.

03:21.950 --> 03:25.350
And then I'm going to print the chat message content.

03:25.350 --> 03:26.670
So we're passing in this message.

03:27.030 --> 03:32.310
Um, the messages I'd like to go to London and the assistant is, uh, your helpful assistant for an

03:32.310 --> 03:32.590
airline.

03:32.590 --> 03:34.870
You give short, humorous answers.

03:34.870 --> 03:36.590
Let's see what happens if we do this.

03:37.070 --> 03:37.870
Great choice.

03:37.870 --> 03:41.430
Just remember, if it starts raining, it's not a sign to panic.

03:41.470 --> 03:43.470
It's just London welcoming you.

03:43.710 --> 03:44.030
Ha!

03:44.270 --> 03:49.870
A nice humorous answer, of course, from our agent, and it's worth pointing out that we're having

03:49.870 --> 03:53.030
a same kind of moment as with, uh, OpenAI agents SDK.

03:53.190 --> 03:56.710
It's so easy to do this, to package it up and to make this call.

03:56.710 --> 03:59.870
It's really a nice, lightweight abstraction.

03:59.870 --> 04:05.470
There's not a lot of heaviness to this, just a lightweight abstraction around calling LMS.

04:05.670 --> 04:07.110
Well, let's take that a little bit further.

04:07.110 --> 04:09.030
Let's of course we have to bring in tools.

04:09.030 --> 04:10.430
It's always about tools.

04:10.430 --> 04:13.390
Let's bring in a tool, do something more interesting right now.

04:13.430 --> 04:16.350
Now we're going to make a tool that's going to get ticket prices.

04:16.350 --> 04:19.830
And we're going to arm our agent with the ability to look up ticket prices.

04:19.830 --> 04:24.990
And we might as well use like a SQLite database because people often like to think, say, say, okay,

04:25.030 --> 04:30.070
what would it be like if we had, uh, our agents being able to query the database?

04:30.070 --> 04:34.990
Now, there's there's sophisticated ways of doing it to actually write a SQL tool that gives agents

04:34.990 --> 04:36.270
the ability to write SQL.

04:36.310 --> 04:37.710
But in this case, it's perfectly simple.

04:37.710 --> 04:41.670
We can just write a tool that can just look up in the database.

04:41.670 --> 04:42.790
So let's do that right now.

04:42.790 --> 04:45.550
So we're going to import Sqlite3.

04:45.870 --> 04:50.710
We're going to create and we're going to delete tickets database if it already exists because we ran

04:50.710 --> 04:51.230
this before.

04:51.550 --> 04:57.590
And then once it's been deleted, we will then connect to a new database and create a table called cities,

04:57.590 --> 05:01.270
which has a city name and a round trip price.

05:01.470 --> 05:08.270
And uh, people that are familiar with this will know perfectly well that it's created a DB database

05:08.270 --> 05:09.910
that will now be empty.

05:09.910 --> 05:16.350
And we are going to populate our database with a bunch of tickets for to London, Paris, Rome, Madrid,

05:16.390 --> 05:18.190
Barcelona and Berlin.

05:18.510 --> 05:19.990
And that's been done.

05:20.150 --> 05:26.950
And we're going to write a simple query function get city price that takes a name, and it will get

05:26.950 --> 05:30.150
the round trip price to travel to the city.

05:30.550 --> 05:35.790
It will simply connect to the database and it will run a little select statement passing in the city

05:35.830 --> 05:39.390
name and return the result.

05:39.430 --> 05:44.670
And yes, for security conscious people, there's perhaps more things that I should do to make sure

05:44.710 --> 05:48.110
to validate this and make sure the city name is a city name and all the rest of it.

05:48.110 --> 05:50.550
But this is just a toy example for now.

05:50.830 --> 05:51.990
So we run this.

05:52.030 --> 05:53.870
We've got get get city price.

05:53.870 --> 05:54.830
Let's just check it out.

05:54.830 --> 05:56.790
Let's try get city price for London.

05:56.790 --> 05:58.030
It's populated in the database.

05:58.030 --> 05:59.350
We get back to 99.

05:59.350 --> 06:02.470
Let's do Rome and we get back for 99.

06:02.510 --> 06:04.110
That does appear to be working.

06:04.390 --> 06:06.230
And now check this out.

06:06.630 --> 06:08.590
This is the same as before.

06:08.830 --> 06:11.270
This, uh, creating an assistant agent.

06:11.270 --> 06:15.150
I'm calling it smart agent now because it's going to be smarter than before.

06:15.590 --> 06:17.350
Uh, otherwise that's the same.

06:17.350 --> 06:17.990
This is the same.

06:17.990 --> 06:23.590
We're passing in the same underlying client, the same underlying lm the system message.

06:23.710 --> 06:28.510
Uh, I have actually added in, but I'm not sure if it's necessary to tell it that it has the ability

06:28.510 --> 06:30.230
to look for a round trip ticket.

06:30.710 --> 06:32.430
Um, we're streaming again.

06:32.470 --> 06:37.750
We're passing in this function as one of our tools in here.

06:37.870 --> 06:45.590
And there's also this slightly curious attribute reflect on tool use, uh, which is a way of indicating

06:45.590 --> 06:48.910
that we don't just want it to return the tools results.

06:48.910 --> 06:54.190
We do want it to be able to take that and continue processing even after the tool has returned.

06:54.190 --> 06:54.430
True.

06:54.470 --> 06:56.630
We we continue until it replies.

06:56.630 --> 07:00.430
So that's like it's rare that you wouldn't want that to be true.

07:00.870 --> 07:04.950
So you should always assume that that will be your default, I think.

07:05.230 --> 07:07.910
Um, and that's I'm not thinking of something obvious.

07:08.230 --> 07:09.670
So, so this is the way we do it.

07:09.670 --> 07:14.430
And there's something to point out here, which is a tiny difference from the other frameworks, a tiny

07:14.470 --> 07:19.710
way in which autogen maybe is a little bit better, a little bit superior, that you'll notice we're

07:19.710 --> 07:23.270
just passing in this Python function directly here.

07:23.390 --> 07:25.950
We didn't have any kind of decorator or anything.

07:25.990 --> 07:29.630
You remember in OpenAI agents SDK, we had to put in a decorator.

07:29.630 --> 07:34.390
We've had to in things like Landgraaf, you have to to, uh, wrap it in a, like a tool.

07:34.510 --> 07:36.630
We haven't had to do anything like that.

07:36.670 --> 07:39.270
It's, um, it's just really lightweight.

07:39.430 --> 07:44.350
And it's because, of course, they've just got a little bit of extra stuff in the abstraction code

07:44.550 --> 07:46.710
that sees this as a Python function.

07:46.750 --> 07:51.500
It uses this comment to figure out the description of the tool.

07:51.620 --> 07:53.140
So it just does a bit of that for you.

07:53.180 --> 07:55.460
It makes life a tiny bit easier.

07:55.700 --> 07:59.820
Removing some of the of the of the learning curve, which I think is really nice.

08:00.020 --> 08:06.260
Um, so what we're now going to do is we're just simply going to call smart agent this guy on messages

08:06.300 --> 08:07.820
passing in our same message.

08:07.820 --> 08:08.980
I want to go to London.

08:09.260 --> 08:16.140
Um, and then just just for the for fun, I'm going to print the inner messages.

08:16.340 --> 08:21.500
I mentioned that the message construct isn't just for the human to the agent.

08:21.500 --> 08:24.860
It's also for what's going on between agents and inside the agent.

08:24.860 --> 08:27.740
So we can see that by printing its inner messages.

08:27.740 --> 08:30.500
And then we will print the result and let's run that.

08:30.820 --> 08:32.540
And this is what happens.

08:32.540 --> 08:35.820
So you can see first of all that there's been a function call.

08:35.820 --> 08:40.180
This was an inner message with city name set to London.

08:40.340 --> 08:43.300
And it's a get city price is the function call.

08:43.740 --> 08:45.980
Uh the results came back.

08:46.020 --> 08:49.340
The result was 2.99 and there we go.

08:49.460 --> 08:52.540
This is linked back to there, as you would hope.

08:52.900 --> 08:58.660
And then this is the final response from the model are the city of T and top hats.

08:58.820 --> 09:01.900
A round trip ticket to London will set you back 2.99.

09:01.940 --> 09:06.900
Just remember, the only thing you should pack is your sense of humor, because the weather might require

09:06.900 --> 09:07.180
it.

09:08.900 --> 09:09.900
Very droll.

09:10.100 --> 09:10.860
There we go.

09:10.980 --> 09:12.460
Uh, so, uh.

09:12.620 --> 09:13.100
Great.

09:13.100 --> 09:13.980
Great answer.

09:13.980 --> 09:19.100
But of course, more important than the great answer is the fact that it was so simple to write that

09:19.100 --> 09:22.020
tool, to have it make a SQL call to a database.

09:22.020 --> 09:23.020
That's right.

09:23.060 --> 09:23.900
Sitting right there.

09:24.220 --> 09:31.340
Uh, and to have that run use the tool, um, and it just shows, uh, first of all, um, how quick

09:31.340 --> 09:36.420
and simple agent chat is, but secondly, how good you're getting at understanding this stuff because

09:36.460 --> 09:42.460
honestly, this is so familiar to you now that this is just in a less than ten minutes, you're already

09:42.500 --> 09:44.060
an expert at agent chat.
