WEBVTT

1
00:00:00.000 --> 00:00:04.800
Hi, and welcome to another AI in C-Sharp video on the Microsoft Agent Framework.

2
00:00:05.599 --> 00:00:09.800
Today we're going to do a deep dive into chat reducers

3
00:00:09.800 --> 00:00:14.000
because we have made a previous video before, links in the description,

4
00:00:15.000 --> 00:00:17.600
about the two built-in chat reducers.

5
00:00:17.600 --> 00:00:20.000
But what if we want to do something different

6
00:00:20.000 --> 00:00:24.600
than just reduce the number of messages or do a simple summary?

7
00:00:25.799 --> 00:00:29.500
We are able to make our own custom reducers,

8
00:00:29.500 --> 00:00:31.500
and that's what we're going to cover today.

9
00:00:33.500 --> 00:00:39.099
So we have the previous sample here, chat history reducer,

10
00:00:39.700 --> 00:00:42.500
and the link to the video about that,

11
00:00:43.500 --> 00:00:47.900
where we went in and used the message counting reducer

12
00:00:47.900 --> 00:00:51.700
and the summary reducer, meaning four messages,

13
00:00:52.299 --> 00:00:54.099
get down to four messages again,

14
00:00:54.500 --> 00:00:57.500
and summary after four messages, summarized.

15
00:00:57.500 --> 00:01:03.500
But now we're going to go in and see not the built-in ones,

16
00:01:04.099 --> 00:01:06.099
but the built-in ones, as you can see here,

17
00:01:06.099 --> 00:01:09.300
is just an iChatReducer interface.

18
00:01:10.300 --> 00:01:15.099
So we can make our own, and then the first one I'm going to make here,

19
00:01:16.900 --> 00:01:20.099
or let's run it first so we can actually see

20
00:01:21.099 --> 00:01:24.500
how it works without any chat reducers.

21
00:01:25.099 --> 00:01:28.699
That might help a little with the understanding of this.

22
00:01:29.699 --> 00:01:31.300
So if I say hello to it,

23
00:01:33.300 --> 00:01:36.099
this sample will just write out the answer,

24
00:01:36.500 --> 00:01:38.500
how many number of messages there were,

25
00:01:38.500 --> 00:01:41.099
and who wrote them, user or assistant.

26
00:01:41.699 --> 00:01:47.500
And I also put in a tool so we can see tools in this.

27
00:01:47.699 --> 00:01:53.099
What is the weather like in Paris?

28
00:01:55.500 --> 00:01:58.099
And because we have a session on,

29
00:01:58.099 --> 00:02:01.099
we can of course see that both say hello, hello back,

30
00:02:01.500 --> 00:02:02.699
what is the weather like in Paris,

31
00:02:02.699 --> 00:02:05.699
then we get the tool call for get weather,

32
00:02:06.099 --> 00:02:10.100
and the tool result back, and then the final result.

33
00:02:10.500 --> 00:02:12.100
And if we keep doing this,

34
00:02:15.899 --> 00:02:20.300
we will see that this just builds up.

35
00:02:21.300 --> 00:02:25.699
And this is of course good for the AI,

36
00:02:25.699 --> 00:02:28.100
because now we could go in and say,

37
00:02:29.500 --> 00:02:33.899
what cities have we talked about?

38
00:02:42.899 --> 00:02:44.899
And it would come back and say,

39
00:02:44.899 --> 00:02:47.300
we have two cities, Paris and Copenhagen.

40
00:02:47.300 --> 00:02:50.699
And it can only do that because it has the entire history.

41
00:02:52.100 --> 00:02:55.500
But there could be reasons for getting rid of this history,

42
00:02:55.500 --> 00:02:59.899
mostly tokens and latency,

43
00:02:59.899 --> 00:03:02.899
because the more we send, the more there will be here.

44
00:03:03.699 --> 00:03:06.500
So the first one I want to introduce you to

45
00:03:06.500 --> 00:03:10.100
is one I made all back in Semantic Kernel,

46
00:03:10.100 --> 00:03:13.100
but I have now built here as well in Agent Framework.

47
00:03:13.500 --> 00:03:17.300
And that is one that always just removes all tool calls.

48
00:03:18.699 --> 00:03:21.699
So the way you do an iChat reducer

49
00:03:21.699 --> 00:03:25.899
is you just inherit from it or implement it,

50
00:03:25.899 --> 00:03:28.500
given that it's an interface.

51
00:03:28.699 --> 00:03:31.899
And the only thing you need to implement is a reduce async

52
00:03:31.899 --> 00:03:35.899
that takes in the previous messages and a cancellation token,

53
00:03:36.500 --> 00:03:40.500
and spits back what messages should be kept.

54
00:03:41.500 --> 00:03:45.500
So any messages we take out of this and put back in

55
00:03:45.500 --> 00:03:50.100
will be used and the rest will be gone.

56
00:03:51.500 --> 00:03:56.500
I'll put in a little utility just to see that it's been called.

57
00:03:57.100 --> 00:03:59.100
And then what are we doing here?

58
00:03:59.100 --> 00:04:02.699
We are making a list of the messages we want to keep

59
00:04:02.699 --> 00:04:04.699
from the previous messages.

60
00:04:05.300 --> 00:04:11.500
We are going in and say if that message is of role tool,

61
00:04:11.500 --> 00:04:14.100
then we get rid of it, meaning we just skip it,

62
00:04:14.100 --> 00:04:16.100
don't put it in to keep.

63
00:04:16.500 --> 00:04:19.100
Or if the message is from the assistant

64
00:04:19.100 --> 00:04:22.100
and it has a tool call request,

65
00:04:22.100 --> 00:04:25.100
meaning a function call content,

66
00:04:25.100 --> 00:04:27.100
then we also get rid of it.

67
00:04:27.700 --> 00:04:29.700
So let's see that as an action,

68
00:04:29.700 --> 00:04:31.899
in that we set a breakpoint here,

69
00:04:31.899 --> 00:04:35.899
and we go up and say that instead of no reducers,

70
00:04:35.899 --> 00:04:40.899
we want to use the always remove tool calls reducer.

71
00:04:43.299 --> 00:04:45.299
So let's run.

72
00:04:46.700 --> 00:04:49.700
Put this side by side so we can see.

73
00:04:52.100 --> 00:04:55.100
So if I just say hello,

74
00:04:57.500 --> 00:05:00.500
we can see it's called a reducer,

75
00:05:00.500 --> 00:05:03.500
and in the first case, because this is the first message,

76
00:05:04.100 --> 00:05:06.899
our previous message is actually zero.

77
00:05:06.899 --> 00:05:10.899
So technically it makes no sense to go to this

78
00:05:11.299 --> 00:05:15.299
because we just get to keep zero out of zero.

79
00:05:17.899 --> 00:05:21.899
And it will come back. Hello, how can I assist you today?

80
00:05:22.500 --> 00:05:27.500
So if I say, how are you?

81
00:05:27.899 --> 00:05:31.899
Again, we are called here.

82
00:05:32.700 --> 00:05:34.700
We get now two messages,

83
00:05:34.700 --> 00:05:37.100
so we don't get the third one, how are you,

84
00:05:37.100 --> 00:05:41.500
but we only get the two prior to what we're just about to call.

85
00:05:42.100 --> 00:05:46.100
And again, this happens before we get a message.

86
00:05:46.899 --> 00:05:50.899
So the two ones we have here is hello and hello back.

87
00:05:52.700 --> 00:05:55.700
So we call again the tool, and what to keep?

88
00:05:56.299 --> 00:05:59.299
Well, the first message is just hello,

89
00:05:59.700 --> 00:06:02.700
so it shouldn't be removed,

90
00:06:02.700 --> 00:06:05.700
and the second message is also just a response back.

91
00:06:07.100 --> 00:06:10.100
But it's your logic in C-sharp,

92
00:06:10.100 --> 00:06:12.700
so you can do pretty much anything here.

93
00:06:12.700 --> 00:06:14.700
You'll see a few other examples.

94
00:06:15.299 --> 00:06:18.299
So it came back, I'm just a programmer.

95
00:06:18.700 --> 00:06:21.700
So now we can, for example, say,

96
00:06:21.700 --> 00:06:27.700
what is the weather like in Paris?

97
00:06:30.299 --> 00:06:34.299
And for context here, I just gave the agent a tool,

98
00:06:36.700 --> 00:06:39.700
get weather, which just say it's sunny and 90 degrees,

99
00:06:39.700 --> 00:06:42.700
no matter what city it is, just for testing.

100
00:06:43.700 --> 00:06:47.700
So if we call this, again, we hit the reducer,

101
00:06:47.700 --> 00:06:50.700
we have four messages now, the hello and how are you,

102
00:06:52.299 --> 00:06:55.299
and we're still not going to remove anything,

103
00:06:55.700 --> 00:06:58.700
because the tool calls have not happened yet.

104
00:07:00.500 --> 00:07:04.500
So it always happens prior to the call, not after the call,

105
00:07:05.700 --> 00:07:08.700
because that could essentially remove some of the new stuff.

106
00:07:10.700 --> 00:07:14.700
So we're going to them, we see that we keep all four,

107
00:07:15.100 --> 00:07:18.100
because none of them are tool-related.

108
00:07:19.100 --> 00:07:24.100
And it comes back, and we can now see we have eight messages in here.

109
00:07:24.100 --> 00:07:27.100
So we saw the four before, and then we said,

110
00:07:27.100 --> 00:07:29.100
what is the weather like in Paris?

111
00:07:29.100 --> 00:07:32.100
We got a tool call for get weather,

112
00:07:32.100 --> 00:07:36.100
and we got a tool result back for what it was,

113
00:07:36.100 --> 00:07:41.100
and then the assistant came back and summarized these two things.

114
00:07:42.100 --> 00:07:48.100
So now we can try and say, what about Copenhagen?

115
00:07:51.100 --> 00:07:54.100
Meaning the weather in Copenhagen.

116
00:07:55.100 --> 00:07:58.100
So now we get our eight messages,

117
00:07:58.100 --> 00:08:02.100
but now we can see that at some points,

118
00:08:02.100 --> 00:08:05.100
let me just set some breakpoints here,

119
00:08:05.100 --> 00:08:09.100
we get a message that was the tool call,

120
00:08:10.100 --> 00:08:13.100
meaning it was this one here.

121
00:08:14.100 --> 00:08:18.100
So where it asks, I need to call the get weather tool,

122
00:08:18.100 --> 00:08:21.100
and we're now just skipping that,

123
00:08:21.100 --> 00:08:26.100
and we also get the tool back, meaning this one here,

124
00:08:27.100 --> 00:08:29.100
and we're also skipping that.

125
00:08:29.100 --> 00:08:31.100
So if we set a breakpoint up here,

126
00:08:31.100 --> 00:08:34.099
we now have six messages we want to return,

127
00:08:35.099 --> 00:08:37.099
and that is the hello,

128
00:08:37.099 --> 00:08:41.099
so the back and forward that we have really seen,

129
00:08:41.099 --> 00:08:43.099
but the tool call is now gone.

130
00:08:44.099 --> 00:08:48.099
And we need to remove both the assistant request and the tool,

131
00:08:48.099 --> 00:08:52.099
else the AI will actually break down and say,

132
00:08:52.099 --> 00:08:57.099
there isn't a matching pair of request and result of this tool.

133
00:08:59.099 --> 00:09:02.099
But if we do this, and you can just copy this

134
00:09:02.099 --> 00:09:04.099
if you want to have this,

135
00:09:05.099 --> 00:09:09.099
we now see that that is what is being sent prior,

136
00:09:10.099 --> 00:09:16.099
and we get rid of the tool calls up here for the ask of Paris,

137
00:09:16.099 --> 00:09:20.099
but we of course see the new tool call down here

138
00:09:20.099 --> 00:09:22.099
for the new message.

139
00:09:22.099 --> 00:09:25.099
But when we send the next one, it will remove those two as well.

140
00:09:25.099 --> 00:09:28.099
So this one will always remove all tools,

141
00:09:28.099 --> 00:09:35.099
but the tools that were used in the last sent message. Okay.

142
00:09:36.099 --> 00:09:38.099
Let's try something else.

143
00:09:40.099 --> 00:09:43.099
Let's try to just make a thing

144
00:09:43.099 --> 00:09:47.099
that gets rid of the word sunny in messages,

145
00:09:49.099 --> 00:09:53.099
because they don't need to be as big as all this up here.

146
00:09:53.099 --> 00:09:57.099
Here, it can simply see to remove all previous messages

147
00:09:57.099 --> 00:10:03.099
that contains the word sunny in the invariant case.

148
00:10:05.099 --> 00:10:10.099
So if we do that, let me get rid of this one, so we can see.

149
00:10:10.099 --> 00:10:13.099
Essentially, we're just taking the previous messages

150
00:10:13.099 --> 00:10:15.099
and filtering them a bit.

151
00:10:17.099 --> 00:10:21.099
That could be filtering for some words that shouldn't be there

152
00:10:21.099 --> 00:10:26.099
or out-of-date information or whatever it is.

153
00:10:26.099 --> 00:10:31.099
But if we take these messages with word reducer

154
00:10:31.099 --> 00:10:36.099
and set it to be the chat reducer, and restart.

155
00:10:39.099 --> 00:10:44.099
So now we can again say, what is the weather like in Paris?

156
00:10:45.099 --> 00:10:49.099
And we will get our four messages,

157
00:10:49.099 --> 00:10:53.099
the user message, the tool call request,

158
00:10:53.099 --> 00:10:56.099
the tool back, and the assistant.

159
00:10:58.099 --> 00:11:02.099
And we can see that it mentioned the word sunny.

160
00:11:03.099 --> 00:11:07.099
So if we say, how about Copenhagen?

161
00:11:09.099 --> 00:11:12.099
It might surprise you what you will see back.

162
00:11:12.099 --> 00:11:14.099
We will see that we have seven messages.

163
00:11:14.099 --> 00:11:16.099
We have the user.

164
00:11:16.099 --> 00:11:18.099
We have the assistant.

165
00:11:20.099 --> 00:11:23.099
We have the sunny and 19 degrees.

166
00:11:23.099 --> 00:11:27.099
But we said we wanted to get rid of those.

167
00:11:27.099 --> 00:11:33.099
Well, down here, I only... Let me find it.

168
00:11:33.099 --> 00:11:36.099
I only set the one that had text in them.

169
00:11:36.099 --> 00:11:39.099
And this is not text, this is content.

170
00:11:39.099 --> 00:11:44.099
So if you want to get rid of everything that has anything to do with tools,

171
00:11:44.099 --> 00:11:49.099
you first need to check each tool if they mention the word sunny.

172
00:11:49.099 --> 00:11:55.099
So the only thing that is gone here is actually the first reply back.

173
00:11:56.099 --> 00:12:00.099
The weather in Paris is sunny and 19 degrees. That is gone.

174
00:12:00.099 --> 00:12:03.099
And now we only have the Copenhagen weather.

175
00:12:03.099 --> 00:12:07.099
So it's a bit contrived, but just to show you that

176
00:12:07.099 --> 00:12:12.099
whenever you do this, it is your job to get rid of whatever it is.

177
00:12:12.099 --> 00:12:15.099
And in this case, text is only the one

178
00:12:15.099 --> 00:12:18.099
that actually gets rid of the final assistant message.

179
00:12:20.099 --> 00:12:22.099
Let's try another one.

180
00:12:23.099 --> 00:12:30.099
We can also go in and use agents within the chat reducer.

181
00:12:30.099 --> 00:12:35.099
So in this case, I'm making a pirate summary reducer agent.

182
00:12:36.099 --> 00:12:39.099
So it's a little like the summary chat reducer up here,

183
00:12:39.099 --> 00:12:43.099
but we get more control. We can use tools and so on.

184
00:12:43.099 --> 00:12:45.099
We can technically do that up here,

185
00:12:45.099 --> 00:12:47.099
but it's not with the agent framework,

186
00:12:47.099 --> 00:12:51.099
it's with the underlying Microsoft Extensions AI.

187
00:12:52.099 --> 00:12:56.099
So in this case, I'm just making a pirate summary reducer

188
00:12:56.099 --> 00:12:59.099
and telling it, giving the input messages,

189
00:12:59.099 --> 00:13:02.099
make a summary of them in the voice of a pirate.

190
00:13:03.099 --> 00:13:07.099
And I'm saying it only needs to happen after four messages.

191
00:13:09.099 --> 00:13:15.099
So let's take this one, and then we'll see the code in one second.

192
00:13:18.099 --> 00:13:23.099
So we'll just go down here and set up.

193
00:13:27.099 --> 00:13:29.099
There we go.

194
00:13:30.099 --> 00:13:33.099
So we will try again to say,

195
00:13:33.099 --> 00:13:36.099
what is the weather like in Paris?

196
00:13:36.099 --> 00:13:40.099
We see that our tool is being called,

197
00:13:40.099 --> 00:13:44.099
but we will just say no summary yet

198
00:13:44.099 --> 00:13:47.099
because we only have zero messages

199
00:13:47.099 --> 00:13:52.099
and not the minimum four before we actually do a summarization.

200
00:13:53.099 --> 00:13:57.099
We can try again and say, what about Copenhagen?

201
00:13:58.099 --> 00:14:01.099
We only have four messages,

202
00:14:01.099 --> 00:14:05.099
and in this case we have set four or lower,

203
00:14:05.099 --> 00:14:08.099
so still no summarization.

204
00:14:11.099 --> 00:14:14.099
So we now have eight messages

205
00:14:14.099 --> 00:14:19.099
with the message from Paris and a message from Copenhagen.

206
00:14:19.099 --> 00:14:22.099
Let's say Berlin.

207
00:14:23.099 --> 00:14:26.099
We now have eight messages,

208
00:14:26.099 --> 00:14:29.099
so we have one to do summary.

209
00:14:29.099 --> 00:14:32.099
And we put in an agent, so we can just use that agent

210
00:14:32.099 --> 00:14:34.099
for any kind of thing we want.

211
00:14:34.099 --> 00:14:38.099
In this case, we gave it instructions to agent

212
00:14:38.099 --> 00:14:41.099
that given the input amount,

213
00:14:41.099 --> 00:14:44.099
summarizing the voice of a pirate.

214
00:14:44.099 --> 00:14:49.099
That given the input amount, summarizing the voice of a pirate.

215
00:14:49.099 --> 00:14:54.099
This is just an agent framework call like any other.

216
00:14:56.099 --> 00:14:59.099
So it will come back and, in this case,

217
00:14:59.099 --> 00:15:03.099
summarize those eight messages.

218
00:15:03.099 --> 00:15:07.099
Now we're only returning one message back.

219
00:15:08.099 --> 00:15:10.099
So we have essentially taken the eight messages,

220
00:15:10.099 --> 00:15:13.099
turned it into one message.

221
00:15:15.099 --> 00:15:18.099
So we will see that we have five messages.

222
00:15:18.099 --> 00:15:21.099
So we have the summarized message,

223
00:15:21.099 --> 00:15:24.099
we have the Berlin message,

224
00:15:24.099 --> 00:15:29.099
we have the AI requesting the tool,

225
00:15:30.099 --> 00:15:32.099
and the tool response,

226
00:15:32.099 --> 00:15:36.099
and back what actually happens.

227
00:15:36.099 --> 00:15:39.099
So that makes all sense.

228
00:15:39.099 --> 00:15:43.099
We normally get four messages every time we ask.

229
00:15:43.099 --> 00:15:45.099
One input, one output,

230
00:15:45.099 --> 00:15:50.099
and the two tool calling request and response.

231
00:15:50.099 --> 00:15:53.099
The fifth one is now our summary.

232
00:15:53.099 --> 00:15:55.099
And given that we have a summary now,

233
00:15:55.099 --> 00:15:58.099
we can go in and say,

234
00:15:58.099 --> 00:16:11.099
what cities have we talked about so far?

235
00:16:13.099 --> 00:16:15.099
Again, we get five messages,

236
00:16:15.099 --> 00:16:18.099
which is more than what we want,

237
00:16:18.099 --> 00:16:23.099
so we actually do a summarization of this prior.

238
00:16:24.099 --> 00:16:29.099
So it will talk about all the cities and so on.

239
00:16:29.099 --> 00:16:32.099
We will send that.

240
00:16:32.099 --> 00:16:33.099
And it will come and say,

241
00:16:33.099 --> 00:16:38.099
the cities mentioned so far are Paris, Copenhagen, and Berlin.

242
00:16:40.099 --> 00:16:42.099
And now we're down to three messages

243
00:16:42.099 --> 00:16:46.099
because we only got the summary,

244
00:16:46.099 --> 00:16:48.099
the question, and the answer,

245
00:16:48.099 --> 00:16:57.099
because it didn't need to do any calls to the tools.

246
00:16:57.099 --> 00:17:00.099
Had we asked, the city mentioned so far,

247
00:17:00.099 --> 00:17:02.099
what are their weather?

248
00:17:02.099 --> 00:17:05.099
We can try and do it.

249
00:17:15.099 --> 00:17:18.099
Now we again do a summary.

250
00:17:19.099 --> 00:17:23.099
Now we're down to three, so we don't do a summary, of course,

251
00:17:23.099 --> 00:17:27.099
because we are below the four.

252
00:17:29.099 --> 00:17:32.099
And now we see a bunch of extra tool calls

253
00:17:32.099 --> 00:17:35.099
because it grabbed three tool calls,

254
00:17:35.099 --> 00:17:38.099
one for Paris, one for Copenhagen, one for Berlin.

255
00:17:38.099 --> 00:17:41.099
And the prior got the same message back for all of them

256
00:17:41.099 --> 00:17:44.099
because it's just hard-coded.

257
00:17:44.099 --> 00:17:49.099
But that makes sense that we can get multiple backs,

258
00:17:49.099 --> 00:17:54.099
and now we get the summary again, and so on and so forth.

259
00:17:54.099 --> 00:17:56.099
Of course, summary will cost extra

260
00:17:56.099 --> 00:18:00.099
because we do both in cost and time if we do an agent,

261
00:18:00.099 --> 00:18:03.099
but you could do pretty much anything here.

262
00:18:03.099 --> 00:18:06.099
And I've tried to do one more of these

263
00:18:06.099 --> 00:18:10.099
where I do a city reducer agent.

264
00:18:10.099 --> 00:18:13.099
So given the input number messages,

265
00:18:13.099 --> 00:18:18.099
return the numbers of the messages that contains a city name.

266
00:18:19.099 --> 00:18:24.099
So this one, let's put it down here and have a look at it.

267
00:18:25.099 --> 00:18:29.099
This is a bit more contrived, but just for you to see.

268
00:18:29.099 --> 00:18:32.099
So if we have no messages, we don't need to do anything,

269
00:18:32.099 --> 00:18:35.099
but if we do have any messages,

270
00:18:35.099 --> 00:18:40.099
we can go in and first use one of the other reducers,

271
00:18:40.099 --> 00:18:44.099
meaning I just get rid of all the tool calls on the fly

272
00:18:44.099 --> 00:18:46.099
by doing reduce down here.

273
00:18:46.099 --> 00:18:48.099
This is the best way of chaining it

274
00:18:48.099 --> 00:18:51.099
because you can't chain them up in Agent Framework yourself,

275
00:18:51.099 --> 00:18:55.099
but you can easily just new up one of the other reducers

276
00:18:55.099 --> 00:18:59.099
and quickly call reduce before you work with the data.

277
00:19:00.099 --> 00:19:06.099
And then I'm building up a message of all the messages in string

278
00:19:06.099 --> 00:19:08.099
and send them into the system,

279
00:19:08.099 --> 00:19:14.099
so it can give me back what numbered lines are to be containing a city

280
00:19:14.099 --> 00:19:16.099
and then get rid of it.

281
00:19:16.099 --> 00:19:19.099
So the best way to understand that one

282
00:19:19.099 --> 00:19:23.099
is probably to set a breakpoint here and restart.

283
00:19:24.099 --> 00:19:29.099
So if we say, what is the weather like in Paris,

284
00:19:30.099 --> 00:19:32.099
we of course are called, that's zero,

285
00:19:32.099 --> 00:19:35.099
so we don't need to do anything,

286
00:19:35.099 --> 00:19:38.099
and push it back.

287
00:19:40.099 --> 00:19:42.099
How about Copenhagen?

288
00:19:42.099 --> 00:19:48.099
So now we see that we have a user message, of course, that is Paris.

289
00:19:48.099 --> 00:19:52.099
We have a couple of tool calls,

290
00:19:52.099 --> 00:19:55.099
and we have the word Paris here again.

291
00:19:55.099 --> 00:20:00.099
So if we do this, try and quickly stop the video

292
00:20:00.099 --> 00:20:05.099
and think what would you expect comes back.

293
00:20:08.099 --> 00:20:11.099
Okay, let's try and do it.

294
00:20:11.099 --> 00:20:15.099
So we have four previous messages,

295
00:20:15.099 --> 00:20:20.099
so we say we will always get rid of tool calls,

296
00:20:21.099 --> 00:20:24.099
meaning we call that one.

297
00:20:24.099 --> 00:20:28.099
Let me quickly get out of it, so we go down here.

298
00:20:29.099 --> 00:20:34.099
So now we have just taken the four messages

299
00:20:34.099 --> 00:20:37.099
and reduced them to two.

300
00:20:38.099 --> 00:20:41.099
And that is the original one and the real answer.

301
00:20:41.099 --> 00:20:43.099
So the tool calls are gone.

302
00:20:43.099 --> 00:20:48.099
And now we build a small string builder here.

303
00:20:48.099 --> 00:20:54.099
So what we are saying is, zero is this line, one is this line.

304
00:20:55.099 --> 00:20:57.099
Then I'm doing structured output to tell,

305
00:20:57.099 --> 00:21:02.099
give me all the lines that actually have a city named.

306
00:21:03.099 --> 00:21:09.099
In this case, it's both the line index zero and index one.

307
00:21:10.099 --> 00:21:15.099
So given that both of these contain the word Paris,

308
00:21:16.099 --> 00:21:19.099
we actually remove all messages.

309
00:21:20.099 --> 00:21:24.099
And that results in, if we have a look,

310
00:21:25.099 --> 00:21:28.099
the weather in Copenhagen is currently sunny

311
00:21:28.099 --> 00:21:31.099
with the temperature of 90 degrees.

312
00:21:31.099 --> 00:21:37.099
So the reason why it knows this is because it,

313
00:21:37.099 --> 00:21:39.099
what about Copenhagen?

314
00:21:39.099 --> 00:21:43.099
And it had a getWeather, but it had no context of weather.

315
00:21:43.099 --> 00:21:46.099
So this is just the AI being clever enough that

316
00:21:46.099 --> 00:21:50.099
what about Copenhagen is getWeather.

317
00:21:51.099 --> 00:21:54.099
But had we said hello prior to this,

318
00:21:54.099 --> 00:21:57.099
and then ask for this, let's try to do it.

319
00:21:58.099 --> 00:22:03.099
Let me get rid of all these breakpoints so we can just see it.

320
00:22:04.099 --> 00:22:07.099
So let's say first hi to it.

321
00:22:11.099 --> 00:22:13.099
That comes back.

322
00:22:13.099 --> 00:22:16.099
What's the weather like in Paris?

323
00:22:17.099 --> 00:22:19.099
That comes back, which is okay.

324
00:22:20.099 --> 00:22:23.099
And what about Copenhagen?

325
00:22:24.099 --> 00:22:29.099
Now we can see that the hi is there, the how back,

326
00:22:29.099 --> 00:22:32.099
and then we get to what about Copenhagen.

327
00:22:33.099 --> 00:22:37.099
And now you can see it's a bit unsure.

328
00:22:37.099 --> 00:22:41.099
Are you talking about the weather or is it something else?

329
00:22:41.099 --> 00:22:44.099
Because the context was lost because all the messages

330
00:22:44.099 --> 00:22:49.099
that had a city name involved was removed.

331
00:22:49.099 --> 00:22:56.099
Again, a bit contrived, but again, the primary message here is

332
00:22:56.099 --> 00:23:00.099
you have full control to do anything you want in here.

333
00:23:00.099 --> 00:23:03.099
You just get, here are some messages,

334
00:23:03.099 --> 00:23:05.099
which of them do I want to keep?

335
00:23:05.099 --> 00:23:09.099
You can use AI, you can use regex, you can use common sense,

336
00:23:09.099 --> 00:23:15.099
you can use counting, anything you can do to your imagination,

337
00:23:15.099 --> 00:23:18.099
you can do here in order to get rid of that.

338
00:23:19.099 --> 00:23:22.099
So that's everything. See you in the next one.

