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.
第五周第一天 Autogen Agent Chat，这是 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
和 OpenAI 代理 SDK，特别是我们总是做的第一件事，即加载环境

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.
该模型与我们之前的 LM 等概念类似。

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
在这里，我们导入了一个名为 OpenAI Chat 完成客户端的东西，它是

14
00:00:45,760 --> 00:00:48,640
LM we'll be using, which is GPT four mini.
我们将使用 LM，即 GPT 4 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.
像美洲驼 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.
使用 GPT 4 或 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.
这对于 Autogen 代理聊天来说是一个不同的概念。

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.
这是 Autogen 代理聊天中使用的最基础的类。

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
我们给它一个名字，航空公司代理，我们给它一个模型客户端，底层LM，我们给它一个系统

43
00:02:15,960 --> 00:02:19,480
message rather like the instructions in OpenAI.
消息类似于 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.
与 OpenAI 代理 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.
这并没有太多的繁重，只是围绕调用 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,
我们不妨使用 SQLite 数据库，因为人们常常喜欢思考，比如说，好吧，

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
现在，有一些复杂的方法可以实际编写一个 SQL 工具，为代理提供服务

87
00:04:34,990 --> 00:04:36,270
the ability to write SQL.
编写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.
所以我们要导入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
呃，熟悉这个的人都知道它创建了一个 DB 数据库

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.
我们回到99。

114
00:05:59,350 --> 00:06:02,470
Let's do Rome and we get back for 99.
我们去罗马吧，我们花 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
autogen 可能更好一点，更优越一点，你会注意到我们

138
00:07:19,710 --> 00:07:23,270
just passing in this Python function directly here.
直接在这里传递这个Python函数即可。

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.
您还记得在 OpenAI 代理 SDK 中，我们必须放入一个装饰器。

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.
我们必须在像 Landgraaf 这样的东西中，你必须，呃，将它包装在一个工具中。

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.
它将 this 视为一个 Python 函数。

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.
结果是 2.99，就这样。

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.
然后，这就是模型的最终响应：T 城和高帽。

166
00:08:58,820 --> 00:09:01,900
A round trip ticket to London will set you back 2.99.
一张去伦敦的往返机票要花 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.
工具，让它对数据库进行 SQL 调用。

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.
代理聊天专家。
