And welcome back to cursor and welcome to week five directory. 欢迎回到光标，欢迎来到第五周目录。
Here we go. 开始了。
We're going to the first lab. 我们要去第一个实验室。
Week five day one Autogen Agent Chat, which is the main part of Autogen. 第五周第一天 Autogen Agent Chat，这是 Autogen 的主要部分。
That's sort of comparable with, say, crew. 这有点类似于船员。
And a lot of what we do right now is going to look very familiar because it's very consistent with crew 我们现在所做的很多事情看起来会非常熟悉，因为它与工作人员非常一致
and OpenAI agents SDK, particularly this first thing which we always do, which is to load the env 和 OpenAI 代理 SDK，特别是我们总是做的第一件事，即加载环境
as usual. 照常。
All right. 好的。
So the first concept then is the model. 所以第一个概念就是模型。
And the model which is similar to concepts like LM that we've had before. 该模型与我们之前的 LM 等概念类似。
It's like a wrapper around calling a large language model. 它就像调用大型语言模型的包装器。
And here we import something called OpenAI Chat completion client, which is the wrapper for for the 在这里，我们导入了一个名为 OpenAI Chat 完成客户端的东西，它是
LM we'll be using, which is GPT four mini. 我们将使用 LM，即 GPT 4 mini。
And this is how you create your model client as it's called. 这就是创建模型客户端的方法。
And it's very simple indeed. 这确实非常简单。
And you just pass in the name of your model. 您只需传递模型的名称即可。
And so let's run that. 那么让我们运行一下吧。
And also I just want to show you here that you could do the same thing with Obama to run a local model 而且我只是想在这里向您展示您可以与奥巴马做同样的事情来运行本地模型
like llama 3.2. 像美洲驼 3.2 一样。
It's just exactly the same idea. 这是完全一样的想法。
You could run that and you could continue all of this in exactly the same way, running locally instead 您可以运行它，并且可以以完全相同的方式继续所有这些，而是​​在本地运行
of using GPT four or mini. 使用 GPT 4 或 mini。
Okay, that is the first concept. 好的，这是第一个概念。
The second concept is the message. 第二个概念是消息。
This is something which is a different concept for Autogen agent chat. 这对于 Autogen 代理聊天来说是一个不同的概念。
It's this idea that you create an object called text message. 您正是根据这个想法创建了一个名为文本消息的对象。
In this case that has the content. 在这种情况下，就有内容了。
I'd like to go to London is my message right now. 我现在的信息是我想去伦敦。
And the source is the user me. 而来源就是用户我。
And so if we run that and print it, we see it's a text message. 因此，如果我们运行它并打印它，我们会看到它是一条短信。
The source is the user. 源头是用户。
And the, uh, there's the content. 还有，呃，这就是内容。
And now that is all there is to it. 现在这就是全部了。
That is the message. 这就是信息。
The third concept is the agent. 第三个概念是代理。
And it's very similar to things we've seen in the past. 这与我们过去看到的非常相似。
The the thing that we import is called assistant agent. 我们导入的东西叫做助理代理。
That is the uh, you'll see this many times. 就是这个，呃，你会看到很多次。
This is the kind of most, the most fundamental class that will work with in in Autogen agent chat. 这是 Autogen 代理聊天中使用的最基础的类。
So we create a new instance of assistant agent. 因此，我们创建一个新的助理代理实例。
We give it a name, airline agent, we give it a model client, the underlying LM, we give it a system 我们给它一个名字，航空公司代理，我们给它一个模型客户端，底层LM，我们给它一个系统
message rather like the instructions in OpenAI. 消息类似于 OpenAI 中的说明。
You are a helpful assistant for an airline. 您是航空公司的得力助手。
You give a short humorous answer. 你给出了一个简短幽默的回答。
So let's give it that to see what that does to it. 因此，让我们给它一个值，看看它会产生什么影响。
Uh, and uh, model client stream is how we say that we want it to stream back the results. 呃，呃，模型客户端流就是我们希望它流回结果的方式。
Uh, and so it's something that we've, we've already done from time to time, but that is an agent 呃，所以这是我们已经不时做过的事情，但那是一个代理
that has been created. 已创建。
And then the thing that brings it all together is something called on messages. 然后将所有这些结合在一起的东西就是所谓的消息。
That is what we call on an agent to pass in a bunch of messages, which we do right here. 这就是我们要求代理传递一堆消息的方法，我们在这里就这样做了。
We just put our single message in a list. 我们只是将单个消息放入列表中。
We pass that into all messages. 我们将其传递到所有消息中。
You also have to pass in this thing called cancellation token, which is how it knows when the messages 您还必须传递一个称为取消令牌的东西，这就是它如何知道消息何时发送
are finished. 完成了。
That's a sort of fiddly thing about about agent chat, but but I wouldn't worry about it. 关于代理聊天，这是一件很麻烦的事情，但我不会担心。
You just call, uh. 你就打电话吧，嗯。
And of course it's an async. 当然，它是异步的。
It's a coroutine, so we have to await it. 这是一个协程，所以我们必须等待它。
Await agent on messages pass in the list. 等待代理在列表中传递消息。
You pass in this cancellation token, which is how the framework is going to know when this agent is 您传入此取消令牌，框架将通过该令牌知道此代理何时
completed. 完全的。
And then I'm going to print the chat message content. 然后我要打印聊天消息内容。
So we're passing in this message. 所以我们传递这个消息。
Um, the messages I'd like to go to London and the assistant is, uh, your helpful assistant for an 嗯，我想去伦敦的消息，助理是，呃，你的有用助理
airline. 航空公司。
You give short, humorous answers. 您给出简短、幽默的答案。
Let's see what happens if we do this. 让我们看看如果这样做会发生什么。
Great choice. 很棒的选择。
Just remember, if it starts raining, it's not a sign to panic. 请记住，如果开始下雨，这并不是恐慌的迹象。
It's just London welcoming you. 伦敦正在欢迎您。
Ha! 哈!
A nice humorous answer, of course, from our agent, and it's worth pointing out that we're having 当然，我们的经纪人给出了一个很好幽默的答案，值得指出的是，我们有
a same kind of moment as with, uh, OpenAI agents SDK. 与 OpenAI 代理 SDK 相同的时刻。
It's so easy to do this, to package it up and to make this call. 做到这一点非常容易，将其打包并进行调用。
It's really a nice, lightweight abstraction. 这确实是一个很好的、轻量级的抽象。
There's not a lot of heaviness to this, just a lightweight abstraction around calling LMS. 这并没有太多的繁重，只是围绕调用 LMS 的轻量级抽象。
Well, let's take that a little bit further. 好吧，让我们更进一步。
Let's of course we have to bring in tools. 当然，我们必须引入工具。
It's always about tools. 一切都与工具有关。
Let's bring in a tool, do something more interesting right now. 让我们引入一个工具，现在就做一些更有趣的事情。
Now we're going to make a tool that's going to get ticket prices. 现在我们将制作一个可以获取门票价格的工具。
And we're going to arm our agent with the ability to look up ticket prices. 我们将为我们的代理商提供查询票价的能力。
And we might as well use like a SQLite database because people often like to think, say, say, okay, 我们不妨使用 SQLite 数据库，因为人们常常喜欢思考，比如说，好吧，
what would it be like if we had, uh, our agents being able to query the database? 如果我们的代理能够查询数据库，那会是什么样子？
Now, there's there's sophisticated ways of doing it to actually write a SQL tool that gives agents 现在，有一些复杂的方法可以实际编写一个 SQL 工具，为代理提供服务
the ability to write SQL. 编写SQL的能力。
But in this case, it's perfectly simple. 但在这种情况下，它非常简单。
We can just write a tool that can just look up in the database. 我们可以编写一个可以在数据库中查找的工具。
So let's do that right now. 所以让我们现在就这样做。
So we're going to import Sqlite3. 所以我们要导入Sqlite3。
We're going to create and we're going to delete tickets database if it already exists because we ran 我们将创建票证数据库，如果它已经存在，我们将删除它，因为我们运行了
this before. 这个之前。
And then once it's been deleted, we will then connect to a new database and create a table called cities, 一旦它被删除，我们将连接到一个新的数据库并创建一个名为城市的表，
which has a city name and a round trip price. 其中有城市名称和往返价格。
And uh, people that are familiar with this will know perfectly well that it's created a DB database 呃，熟悉这个的人都知道它创建了一个 DB 数据库
that will now be empty. 现在将是空的。
And we are going to populate our database with a bunch of tickets for to London, Paris, Rome, Madrid, 我们将用一堆飞往伦敦、巴黎、罗马、马德里的机票填充我们的数据库，
Barcelona and Berlin. 巴塞罗那和柏林。
And that's been done. 这已经完成了。
And we're going to write a simple query function get city price that takes a name, and it will get 我们将编写一个简单的查询函数获取城市价格，它需要一个名称，它会得到
the round trip price to travel to the city. 前往城市的往返价格。
It will simply connect to the database and it will run a little select statement passing in the city 它将简单地连接到数据库，并运行一个在城市中传递的小选择语句
name and return the result. 命名并返回结果。
And yes, for security conscious people, there's perhaps more things that I should do to make sure 是的，对于有安全意识的人来说，也许我应该做更多的事情来确保
to validate this and make sure the city name is a city name and all the rest of it. 验证这一点并确保城市名称是城市名称及其所有其余部分。
But this is just a toy example for now. 但这目前只是一个玩具示例。
So we run this. 所以我们运行这个。
We've got get get city price. 我们已经得到了城市价格。
Let's just check it out. 我们来看看吧。
Let's try get city price for London. 让我们尝试获取伦敦的城市价格。
It's populated in the database. 它已填充到数据库中。
We get back to 99. 我们回到99。
Let's do Rome and we get back for 99. 我们去罗马吧，我们花 99 美元回来。
That does appear to be working. 这似乎确实有效。
And now check this out. 现在检查一下。
This is the same as before. 这和以前一样。
This, uh, creating an assistant agent. 这，呃，创建一个助理特工。
I'm calling it smart agent now because it's going to be smarter than before. 我现在称其为智能代理，因为它会比以前更智能。
Uh, otherwise that's the same. 呃，其他方面都一样。
This is the same. 这也是一样的。
We're passing in the same underlying client, the same underlying lm the system message. 我们传入相同的底层客户端，相同的底层消息系统消息。
Uh, I have actually added in, but I'm not sure if it's necessary to tell it that it has the ability 呃，其实我已经加进去了，但是我不确定是否有必要告诉它它有这个能力
to look for a round trip ticket. 寻找往返机票。
Um, we're streaming again. 嗯，我们又开始直播了。
We're passing in this function as one of our tools in here. 我们将此函数作为我们的工具之一传递到这里。
And there's also this slightly curious attribute reflect on tool use, uh, which is a way of indicating 还有这个有点奇怪的属性反映了工具的使用，呃，这是一种指示方式
that we don't just want it to return the tools results. 我们不只是希望它返回工具结果。
We do want it to be able to take that and continue processing even after the tool has returned. 我们确实希望它能够接受并继续处理，即使工具返回后也是如此。
True. 真的。
We we continue until it replies. 我们继续，直到它回复。
So that's like it's rare that you wouldn't want that to be true. 所以你很少会不希望这是真的。
So you should always assume that that will be your default, I think. 所以我认为你应该始终假设这将是你的默认设置。
Um, and that's I'm not thinking of something obvious. 嗯，那是我没有想到一些显而易见的事情。
So, so this is the way we do it. 所以，这就是我们这样做的方式。
And there's something to point out here, which is a tiny difference from the other frameworks, a tiny 这里有一点需要指出，它与其他框架有一个微小的差异，一个微小的差异
way in which autogen maybe is a little bit better, a little bit superior, that you'll notice we're autogen 可能更好一点，更优越一点，你会注意到我们
just passing in this Python function directly here. 直接在这里传递这个Python函数即可。
We didn't have any kind of decorator or anything. 我们没有任何装饰器或任何东西。
You remember in OpenAI agents SDK, we had to put in a decorator. 您还记得在 OpenAI 代理 SDK 中，我们必须放入一个装饰器。
We've had to in things like Landgraaf, you have to to, uh, wrap it in a, like a tool. 我们必须在像 Landgraaf 这样的东西中，你必须，呃，将它包装在一个工具中。
We haven't had to do anything like that. 我们没有必要做这样的事情。
It's, um, it's just really lightweight. 它，嗯，它真的很轻。
And it's because, of course, they've just got a little bit of extra stuff in the abstraction code 当然，这是因为他们在抽象代码中添加了一些额外的东西
that sees this as a Python function. 它将 this 视为一个 Python 函数。
It uses this comment to figure out the description of the tool. 它使用此注释来确定该工具的描述。
So it just does a bit of that for you. 所以它只是为你做了一些事情。
It makes life a tiny bit easier. 它让生活变得更加轻松。
Removing some of the of the of the learning curve, which I think is really nice. 消除了一些学习曲线，我认为这非常好。
Um, so what we're now going to do is we're just simply going to call smart agent this guy on messages 嗯，所以我们现在要做的就是简单地通过消息给智能代理打电话
passing in our same message. 传递我们相同的消息。
I want to go to London. 我想去伦敦。
Um, and then just just for the for fun, I'm going to print the inner messages. 嗯，然后只是为了好玩，我将打印内部消息。
I mentioned that the message construct isn't just for the human to the agent. 我提到消息构造不仅仅适用于人类和代理。
It's also for what's going on between agents and inside the agent. 它还适用于代理之间和代理内部发生的事情。
So we can see that by printing its inner messages. 所以我们可以通过打印其内部消息来看到这一点。
And then we will print the result and let's run that. 然后我们将打印结果并运行它。
And this is what happens. 这就是发生的事情。
So you can see first of all that there's been a function call. 所以你首先可以看到有一个函数调用。
This was an inner message with city name set to London. 这是一条内部消息，城市名称设置为伦敦。
And it's a get city price is the function call. 这是一个获取城市价格的函数调用。
Uh the results came back. 呃，结果回来了。
The result was 2.99 and there we go. 结果是 2.99，就这样。
This is linked back to there, as you would hope. 正如您所希望的那样，这又链接到那里。
And then this is the final response from the model are the city of T and top hats. 然后，这就是模型的最终响应：T 城和高帽。
A round trip ticket to London will set you back 2.99. 一张去伦敦的往返机票要花 2.99 美元。
Just remember, the only thing you should pack is your sense of humor, because the weather might require 请记住，您唯一应该带上的就是您的幽默感，因为天气可能需要
it. 它。
Very droll. 非常滑稽。
There we go. 我们开始吧。
Uh, so, uh. 呃，所以，呃。
Great. 伟大的。
Great answer. 很好的答案。
But of course, more important than the great answer is the fact that it was so simple to write that 但当然，比伟大的答案更重要的是，写起来非常简单
tool, to have it make a SQL call to a database. 工具，让它对数据库进行 SQL 调用。
That's right. 这是正确的。
Sitting right there. 就坐在那儿。
Uh, and to have that run use the tool, um, and it just shows, uh, first of all, um, how quick 呃，要运行该工具，嗯，它只是显示，呃，首先，嗯，有多快
and simple agent chat is, but secondly, how good you're getting at understanding this stuff because 简单的代理聊天是，但其次，你对这些东西的理解有多好，因为
honestly, this is so familiar to you now that this is just in a less than ten minutes, you're already 老实说，这对你来说太熟悉了，仅仅不到十分钟，你就已经熟悉了
an expert at agent chat. 代理聊天专家。