1
00:00:00,160 --> 00:00:04,200
And welcome back to cursor and welcome to week five directory.

2
00:00:04,240 --> 00:00:04,800
Here we go.

3
00:00:04,840 --> 00:00:06,440
We're going to the first lab.

4
00:00:06,560 --> 00:00:12,520
Week five day one Autogen Agent Chat, which is the main part of Autogen.

5
00:00:12,520 --> 00:00:14,560
That's sort of comparable with, say, crew.

6
00:00:15,040 --> 00: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

7
00:00:19,280 --> 00:00:24,880
and OpenAI agents SDK, particularly this first thing which we always do, which is to load the env

8
00:00:25,160 --> 00:00:26,400
as usual.

9
00:00:26,680 --> 00:00:27,160
All right.

10
00:00:27,160 --> 00:00:29,600
So the first concept then is the model.

11
00:00:30,040 --> 00:00:34,320
And the model which is similar to concepts like LM that we've had before.

12
00:00:34,360 --> 00:00:37,800
It's like a wrapper around calling a large language model.

13
00:00:38,000 --> 00:00:45,760
And here we import something called OpenAI Chat completion client, which is the wrapper for for the

14
00:00:45,760 --> 00:00:48,640
LM we'll be using, which is GPT four mini.

15
00:00:48,720 --> 00:00:52,280
And this is how you create your model client as it's called.

16
00:00:52,320 --> 00:00:54,000
And it's very simple indeed.

17
00:00:54,240 --> 00:00:55,960
And you just pass in the name of your model.

18
00:00:56,200 --> 00:00:57,400
And so let's run that.

19
00:00:57,760 --> 00: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

20
00:01:03,400 --> 00:01:04,640
like llama 3.2.

21
00:01:04,680 --> 00:01:06,840
It's just exactly the same idea.

22
00:01:06,840 --> 00:01:11,440
You could run that and you could continue all of this in exactly the same way, running locally instead

23
00:01:11,440 --> 00:01:13,520
of using GPT four or mini.

24
00:01:14,160 --> 00:01:16,920
Okay, that is the first concept.

25
00:01:17,160 --> 00:01:19,400
The second concept is the message.

26
00:01:19,480 --> 00:01:23,840
This is something which is a different concept for Autogen agent chat.

27
00:01:23,880 --> 00:01:27,360
It's this idea that you create an object called text message.

28
00:01:27,360 --> 00:01:29,440
In this case that has the content.

29
00:01:29,440 --> 00:01:31,760
I'd like to go to London is my message right now.

30
00:01:32,040 --> 00:01:34,760
And the source is the user me.

31
00:01:35,120 --> 00:01:38,360
And so if we run that and print it, we see it's a text message.

32
00:01:38,360 --> 00:01:39,480
The source is the user.

33
00:01:39,680 --> 00:01:42,800
And the, uh, there's the content.

34
00:01:42,920 --> 00:01:45,760
And now that is all there is to it.

35
00:01:45,760 --> 00:01:47,240
That is the message.

36
00:01:47,480 --> 00:01:49,960
The third concept is the agent.

37
00:01:49,960 --> 00:01:52,760
And it's very similar to things we've seen in the past.

38
00:01:53,160 --> 00:01:56,400
The the thing that we import is called assistant agent.

39
00:01:56,400 --> 00:01:58,920
That is the uh, you'll see this many times.

40
00:01:58,920 --> 00:02:06,080
This is the kind of most, the most fundamental class that will work with in in Autogen agent chat.

41
00:02:06,080 --> 00:02:08,960
So we create a new instance of assistant agent.

42
00:02:09,000 --> 00:02:15,960
We give it a name, airline agent, we give it a model client, the underlying LM, we give it a system

43
00:02:15,960 --> 00:02:19,480
message rather like the instructions in OpenAI.

44
00:02:19,880 --> 00:02:21,760
You are a helpful assistant for an airline.

45
00:02:21,760 --> 00:02:24,240
You give a short humorous answer.

46
00:02:24,240 --> 00:02:26,600
So let's give it that to see what that does to it.

47
00:02:26,840 --> 00:02:32,920
Uh, and uh, model client stream is how we say that we want it to stream back the results.

48
00:02:33,120 --> 00: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

49
00:02:38,640 --> 00:02:40,360
that has been created.

50
00:02:40,680 --> 00:02:45,320
And then the thing that brings it all together is something called on messages.

51
00:02:45,600 --> 00:02:51,000
That is what we call on an agent to pass in a bunch of messages, which we do right here.

52
00:02:51,000 --> 00:02:52,880
We just put our single message in a list.

53
00:02:52,880 --> 00:02:54,560
We pass that into all messages.

54
00:02:54,560 --> 00:02:59,790
You also have to pass in this thing called cancellation token, which is how it knows when the messages

55
00:02:59,830 --> 00:03:00,390
are finished.

56
00:03:00,390 --> 00:03:05,630
That's a sort of fiddly thing about about agent chat, but but I wouldn't worry about it.

57
00:03:05,630 --> 00:03:07,190
You just call, uh.

58
00:03:07,350 --> 00:03:08,950
And of course it's an async.

59
00:03:08,990 --> 00:03:11,470
It's a coroutine, so we have to await it.

60
00:03:11,630 --> 00:03:15,350
Await agent on messages pass in the list.

61
00:03:15,390 --> 00:03:20,830
You pass in this cancellation token, which is how the framework is going to know when this agent is

62
00:03:20,830 --> 00:03:21,550
completed.

63
00:03:21,950 --> 00:03:25,350
And then I'm going to print the chat message content.

64
00:03:25,350 --> 00:03:26,670
So we're passing in this message.

65
00:03:27,030 --> 00:03:32,310
Um, the messages I'd like to go to London and the assistant is, uh, your helpful assistant for an

66
00:03:32,310 --> 00:03:32,590
airline.

67
00:03:32,590 --> 00:03:34,870
You give short, humorous answers.

68
00:03:34,870 --> 00:03:36,590
Let's see what happens if we do this.

69
00:03:37,070 --> 00:03:37,870
Great choice.

70
00:03:37,870 --> 00:03:41,430
Just remember, if it starts raining, it's not a sign to panic.

71
00:03:41,470 --> 00:03:43,470
It's just London welcoming you.

72
00:03:43,710 --> 00:03:44,030
Ha!

73
00:03:44,270 --> 00:03:49,870
A nice humorous answer, of course, from our agent, and it's worth pointing out that we're having

74
00:03:49,870 --> 00:03:53,030
a same kind of moment as with, uh, OpenAI agents SDK.

75
00:03:53,190 --> 00:03:56,710
It's so easy to do this, to package it up and to make this call.

76
00:03:56,710 --> 00:03:59,870
It's really a nice, lightweight abstraction.

77
00:03:59,870 --> 00:04:05,470
There's not a lot of heaviness to this, just a lightweight abstraction around calling LMS.

78
00:04:05,670 --> 00:04:07,110
Well, let's take that a little bit further.

79
00:04:07,110 --> 00:04:09,030
Let's of course we have to bring in tools.

80
00:04:09,030 --> 00:04:10,430
It's always about tools.

81
00:04:10,430 --> 00:04:13,390
Let's bring in a tool, do something more interesting right now.

82
00:04:13,430 --> 00:04:16,350
Now we're going to make a tool that's going to get ticket prices.

83
00:04:16,350 --> 00:04:19,830
And we're going to arm our agent with the ability to look up ticket prices.

84
00:04:19,830 --> 00:04:24,990
And we might as well use like a SQLite database because people often like to think, say, say, okay,

85
00:04:25,030 --> 00:04:30,070
what would it be like if we had, uh, our agents being able to query the database?

86
00:04:30,070 --> 00:04:34,990
Now, there's there's sophisticated ways of doing it to actually write a SQL tool that gives agents

87
00:04:34,990 --> 00:04:36,270
the ability to write SQL.

88
00:04:36,310 --> 00:04:37,710
But in this case, it's perfectly simple.

89
00:04:37,710 --> 00:04:41,670
We can just write a tool that can just look up in the database.

90
00:04:41,670 --> 00:04:42,790
So let's do that right now.

91
00:04:42,790 --> 00:04:45,550
So we're going to import Sqlite3.

92
00:04:45,870 --> 00:04:50,710
We're going to create and we're going to delete tickets database if it already exists because we ran

93
00:04:50,710 --> 00:04:51,230
this before.

94
00:04:51,550 --> 00:04:57,590
And then once it's been deleted, we will then connect to a new database and create a table called cities,

95
00:04:57,590 --> 00:05:01,270
which has a city name and a round trip price.

96
00:05:01,470 --> 00:05:08,270
And uh, people that are familiar with this will know perfectly well that it's created a DB database

97
00:05:08,270 --> 00:05:09,910
that will now be empty.

98
00:05:09,910 --> 00:05:16,350
And we are going to populate our database with a bunch of tickets for to London, Paris, Rome, Madrid,

99
00:05:16,390 --> 00:05:18,190
Barcelona and Berlin.

100
00:05:18,510 --> 00:05:19,990
And that's been done.

101
00:05:20,150 --> 00:05:26,950
And we're going to write a simple query function get city price that takes a name, and it will get

102
00:05:26,950 --> 00:05:30,150
the round trip price to travel to the city.

103
00:05:30,550 --> 00:05:35,790
It will simply connect to the database and it will run a little select statement passing in the city

104
00:05:35,830 --> 00:05:39,390
name and return the result.

105
00:05:39,430 --> 00:05:44,670
And yes, for security conscious people, there's perhaps more things that I should do to make sure

106
00:05:44,710 --> 00:05:48,110
to validate this and make sure the city name is a city name and all the rest of it.

107
00:05:48,110 --> 00:05:50,550
But this is just a toy example for now.

108
00:05:50,830 --> 00:05:51,990
So we run this.

109
00:05:52,030 --> 00:05:53,870
We've got get get city price.

110
00:05:53,870 --> 00:05:54,830
Let's just check it out.

111
00:05:54,830 --> 00:05:56,790
Let's try get city price for London.

112
00:05:56,790 --> 00:05:58,030
It's populated in the database.

113
00:05:58,030 --> 00:05:59,350
We get back to 99.

114
00:05:59,350 --> 00:06:02,470
Let's do Rome and we get back for 99.

115
00:06:02,510 --> 00:06:04,110
That does appear to be working.

116
00:06:04,390 --> 00:06:06,230
And now check this out.

117
00:06:06,630 --> 00:06:08,590
This is the same as before.

118
00:06:08,830 --> 00:06:11,270
This, uh, creating an assistant agent.

119
00:06:11,270 --> 00:06:15,150
I'm calling it smart agent now because it's going to be smarter than before.

120
00:06:15,590 --> 00:06:17,350
Uh, otherwise that's the same.

121
00:06:17,350 --> 00:06:17,990
This is the same.

122
00:06:17,990 --> 00:06:23,590
We're passing in the same underlying client, the same underlying lm the system message.

123
00:06:23,710 --> 00: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

124
00:06:28,510 --> 00:06:30,230
to look for a round trip ticket.

125
00:06:30,710 --> 00:06:32,430
Um, we're streaming again.

126
00:06:32,470 --> 00:06:37,750
We're passing in this function as one of our tools in here.

127
00:06:37,870 --> 00:06:45,590
And there's also this slightly curious attribute reflect on tool use, uh, which is a way of indicating

128
00:06:45,590 --> 00:06:48,910
that we don't just want it to return the tools results.

129
00:06:48,910 --> 00:06:54,190
We do want it to be able to take that and continue processing even after the tool has returned.

130
00:06:54,190 --> 00:06:54,430
True.

131
00:06:54,470 --> 00:06:56,630
We we continue until it replies.

132
00:06:56,630 --> 00:07:00,430
So that's like it's rare that you wouldn't want that to be true.

133
00:07:00,870 --> 00:07:04,950
So you should always assume that that will be your default, I think.

134
00:07:05,230 --> 00:07:07,910
Um, and that's I'm not thinking of something obvious.

135
00:07:08,230 --> 00:07:09,670
So, so this is the way we do it.

136
00:07:09,670 --> 00:07:14,430
And there's something to point out here, which is a tiny difference from the other frameworks, a tiny

137
00:07:14,470 --> 00:07:19,710
way in which autogen maybe is a little bit better, a little bit superior, that you'll notice we're

138
00:07:19,710 --> 00:07:23,270
just passing in this Python function directly here.

139
00:07:23,390 --> 00:07:25,950
We didn't have any kind of decorator or anything.

140
00:07:25,990 --> 00:07:29,630
You remember in OpenAI agents SDK, we had to put in a decorator.

141
00:07:29,630 --> 00:07:34,390
We've had to in things like Landgraaf, you have to to, uh, wrap it in a, like a tool.

142
00:07:34,510 --> 00:07:36,630
We haven't had to do anything like that.

143
00:07:36,670 --> 00:07:39,270
It's, um, it's just really lightweight.

144
00:07:39,430 --> 00:07:44,350
And it's because, of course, they've just got a little bit of extra stuff in the abstraction code

145
00:07:44,550 --> 00:07:46,710
that sees this as a Python function.

146
00:07:46,750 --> 00:07:51,500
It uses this comment to figure out the description of the tool.

147
00:07:51,620 --> 00:07:53,140
So it just does a bit of that for you.

148
00:07:53,180 --> 00:07:55,460
It makes life a tiny bit easier.

149
00:07:55,700 --> 00:07:59,820
Removing some of the of the of the learning curve, which I think is really nice.

150
00:08:00,020 --> 00: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

151
00:08:06,300 --> 00:08:07,820
passing in our same message.

152
00:08:07,820 --> 00:08:08,980
I want to go to London.

153
00:08:09,260 --> 00:08:16,140
Um, and then just just for the for fun, I'm going to print the inner messages.

154
00:08:16,340 --> 00:08:21,500
I mentioned that the message construct isn't just for the human to the agent.

155
00:08:21,500 --> 00:08:24,860
It's also for what's going on between agents and inside the agent.

156
00:08:24,860 --> 00:08:27,740
So we can see that by printing its inner messages.

157
00:08:27,740 --> 00:08:30,500
And then we will print the result and let's run that.

158
00:08:30,820 --> 00:08:32,540
And this is what happens.

159
00:08:32,540 --> 00:08:35,820
So you can see first of all that there's been a function call.

160
00:08:35,820 --> 00:08:40,180
This was an inner message with city name set to London.

161
00:08:40,340 --> 00:08:43,300
And it's a get city price is the function call.

162
00:08:43,740 --> 00:08:45,980
Uh the results came back.

163
00:08:46,020 --> 00:08:49,340
The result was 2.99 and there we go.

164
00:08:49,460 --> 00:08:52,540
This is linked back to there, as you would hope.

165
00:08:52,900 --> 00:08:58,660
And then this is the final response from the model are the city of T and top hats.

166
00:08:58,820 --> 00:09:01,900
A round trip ticket to London will set you back 2.99.

167
00:09:01,940 --> 00:09:06,900
Just remember, the only thing you should pack is your sense of humor, because the weather might require

168
00:09:06,900 --> 00:09:07,180
it.

169
00:09:08,900 --> 00:09:09,900
Very droll.

170
00:09:10,100 --> 00:09:10,860
There we go.

171
00:09:10,980 --> 00:09:12,460
Uh, so, uh.

172
00:09:12,620 --> 00:09:13,100
Great.

173
00:09:13,100 --> 00:09:13,980
Great answer.

174
00:09:13,980 --> 00:09:19,100
But of course, more important than the great answer is the fact that it was so simple to write that

175
00:09:19,100 --> 00:09:22,020
tool, to have it make a SQL call to a database.

176
00:09:22,020 --> 00:09:23,020
That's right.

177
00:09:23,060 --> 00:09:23,900
Sitting right there.

178
00:09:24,220 --> 00:09:31,340
Uh, and to have that run use the tool, um, and it just shows, uh, first of all, um, how quick

179
00:09:31,340 --> 00:09:36,420
and simple agent chat is, but secondly, how good you're getting at understanding this stuff because

180
00:09:36,460 --> 00:09:42,460
honestly, this is so familiar to you now that this is just in a less than ten minutes, you're already

181
00:09:42,500 --> 00:09:44,060
an expert at agent chat.