And week three. 第三周。
Day four is a go. 第四天就可以开始了。
Let's get started. 让我们开始吧。
So last time we did a stock picker and we just have a little tiny bit more to put into that. 所以上次我们做了一个选股器，我们只需要多投入一点点。
And then we're going to move on with our next project. 然后我们将继续我们的下一个项目。
A developer agent. 开发商代理。
But first another repetition. 但首先是另一个重复。
I hate to repeat these things, but sometimes it's good to drum it in. 我讨厌重复这些事情，但有时重复一遍是件好事。
Building a crew project involves five things. 建立船员项目涉及五件事。
First of all, crew I create crew. 首先，船员我创建船员。
The name of the project to set up your crew, build those directories and those files. 用于设置您的团队、构建这些目录和文件的项目名称。
Number two, you find the YAML files for your agents and tasks, and you fill them in to define your 第二，您找到代理和任务的 YAML 文件，然后填写它们以定义您的代理和任务
agents and tasks. 代理和任务。
Number three, you go to the crew.py module, which is where you actually create the instances. 第三，您进入crew.py模块，这是您实际创建实例的地方。
And you use decorators to identify the agents and tasks that you'll be using. 您可以使用装饰器来识别您将使用的代理和任务。
And then you create your crew itself. 然后你自己创建你的团队。
And this is where you can have structured outputs to make sure that the outputs conform to a schema. 您可以在此处获得结构化输出，以确保输出符合架构。
And you can use tools, both tools like Serpa, that crew provides for us that run remotely, and then 你可以使用工具，像 Serpa 这样的工具，工作人员为我们提供远程运行的工具，然后
custom tools that we build locally, like the thing that sends a push notification. 我们在本地构建的自定义工具，例如发送推送通知的工具。
And then number four, you update Main.py to set any inputs so that we can pass something in, configure 第四，更新 Main.py 以设置任何输入，以便我们可以传递一些内容，配置
the fields that are templated with the curlies. 使用花括号模板化的字段。
And finally we run with crew. 最后我们和船员一起跑。
I run and off goes our project. 我跑了，我们的项目就开始了。
So I now want to cover a feature of crew called memory. 所以我现在想介绍船员的一个称为记忆的功能。
And this is a feature that is a bit more prescriptive, a bit more opinionated in the crew framework. 这是一个在船员框架中更加规范、更加固执己见的功能。
Memory, of course, is talking about how you provide information, contextual information to Llms each 记忆当然是讲你如何向每个LLMS提供信息、上下文信息
time you call them, and you can implement that yourself just by storing variables and then passing 当你调用它们时，你可以自己实现，只需存储变量然后传递
them in when you do things like creating tasks so you can do it the sort of manual way. 当您执行创建任务之类的操作时，您可以使用它们，这样您就可以以手动方式完成任务。
But the crew framework also comes with some building blocks that lets you use their constructs around 但船员框架还附带了一些构建块，让您可以使用它们的构造
memory out of the box, and that comes with pros and cons. 开箱即用的内存，有优点也有缺点。
The pro is that you get up and running quickly, and you can use a lot of the thinking that they put 优点是你可以快速启动并运行，并且你可以利用他们提出的很多想法
behind this. 这背后。
The con is that there's there's a learning curve, and it obscures some of the detail of how prompts 缺点是有一个学习曲线，并且它掩盖了提示的一些细节
actually work behind the scenes. 实际上在幕后工作。
So as as with many times when you're adopting a framework like this, it's something to be aware of 因此，正如很多时候，当您采用这样的框架时，需要注意这一点
the benefits and the trade offs of doing so. 这样做的好处和权衡。
But let's say that we are going to embrace cruise way of handling memory and talk about what it actually 但是假设我们将采用巡航方式处理内存并讨论它实际上是什么
does. 做。
Well, it has five different types of memory, five different frameworks that you can include. 嗯，它有五种不同类型的内存，您可以包含五种不同的框架。
And one of them is called short term memory. 其中之一称为短期记忆。
And this is just about storing recent interactions using a vector database in in a in a rag way. 这只是使用矢量数据库以一种破烂的方式存储最近的交互。
If you're familiar with retrieval augmented generation. 如果您熟悉检索增强生成。
And you don't need to be for this course because we're just going to put the code in there and see it 您不需要参加本课程，因为我们只需将代码放在那里并查看即可
run. 跑步。
But if you do no, then this will make more sense. 但如果你不这样做，那么这会更有意义。
So this will allow agents to access recent relevant information when they are currently executing. 因此，这将允许代理在当前执行时访问最近的相关信息。
And then a different concept called long term memory is when more important information is stored in 然后一个不同的概念称为长期记忆，即更重要的信息存储在
a SQL database for longer term recall to build up knowledge over over a longer period of time. 用于长期回忆的 SQL 数据库，以在较长时间内积累知识。
And then there's something called entity memory that's very similar to short term memory, actually. 还有一种叫做实体记忆的东西，实际上与短期记忆非常相似。
It's it's basically when there's things about people, places and concepts, then those can also be 基本上，当有关于人、地方和概念的事情时，那么这些也可以是
stored in a Rag database for vector based similarity search and to be included in the context. 存储在 Rag 数据库中，用于基于向量的相似性搜索并包含在上下文中。
And then there's crew describes this as another kind of memory. 然后工作人员将其描述为另一种记忆。
But I think it's a bit misleading. 但我认为这有点误导。
I think what they call contextual memory is just a sort of umbrella term for the short term, long term 我认为他们所谓的情境记忆只是短期、长期的一个总称
and entity memory that can all together be queried and passed in as context when prompting an LM and 和实体内存，在提示 LM 时可以一起查询并作为上下文传递，并且
crew abstracts all this away from you. 船员将这一切从你手中抽象出来。
So it's just going to be a few lines of code to have all of these types of memories running. 因此，只需几行代码即可运行所有这些类型的内存。
But in doing so, as I say, big benefit. 但正如我所说，这样做有很大的好处。
A lot of work happens behind the scenes. 许多工作都在幕后进行。
And perhaps also there the trade off is that you've got less visibility into it. 也许还需要权衡的是你对它的了解较少。
So if things don't go the way you want, then it's a bit harder to debug and figure out what's going 因此，如果事情没有按照您想要的方式进行，那么调试和弄清楚发生了什么就会变得有点困难
on. 在。
And then there is another kind of memory called user memory, which is to store user specific information. 然后还有一种内存叫做用户内存，它是存储用户特定信息的。
And actually, at least as of now in Crewe, this is a concept that they support and have some frameworks 实际上，至少到目前为止在克鲁，这是他们支持并拥有一些框架的概念
around, but it's mostly left up to you to be querying user memory and then inserting it into the prompt 周围，​​但主要由您来查询用户内存，然后将其插入提示中
or providing it at the right time. 或在适当的时候提供。
So user memory is a bit of an odd one out here, and I suspect that they're looking to build more into 因此，用户记忆在这里有点奇怪，我怀疑他们正在寻求构建更多内容
that in time. 及时。
And for now, for the code that we're about to look at, we're just going to really look at contextual 现在，对于我们要查看的代码，我们将真正查看上下文
memory. 记忆。
So short term long term and entity memory and seeing how we can incorporate that into our stock picker 所以短期、长期和实体记忆，看看我们如何将其纳入我们的选股器中
solution. 解决方案。
And so we're back in the stock picker project in cursor. 所以我们回到了光标中的股票选择器项目。
And we are looking at the crew.py module. 我们正在研究crew.py 模块。
And I'm going to start by putting in some new imports in here, which are interesting ones from crime 首先我将在这里添加一些新的导入内容，这些都是来自犯罪的有趣内容
memory. 记忆。
We're going to import long term memory short term memory and entity memory. 我们将导入长期记忆、短期记忆和实体记忆。
The three types we'll be working with. 我们将使用的三种类型。
And I do believe you can also have user memory in there too. 我相信你也可以在那里拥有用户内存。
But then you have to manually manage it yourself. 但随后您必须自己手动管理。
And then from my memory storage rag storage, we're importing a class called Rag storage for vector 然后从我的内存存储 rag storage 中，我们导入一个名为 Rag storage 的类，用于向量
based retrieval. 基于检索。
And with that we're also going to import from long term memory SQLite storage, long term memory SQLite 我们还将从长期内存 SQLite 存储中导入长期内存 SQLite
storage object like that a class. 像类一样存储对象。
All right. 好的。
So that's a few things that we're now going to put to good use. 这就是我们现在要充分利用的一些东西。
We're now going to go to the crew function the function that creates the crew within this this module. 我们现在将转到船员功能，该功能在该模块中创建船员。
And you can see where we made our manager. 你可以看到我们在哪里培养了我们的经理。
And we've got a few more things to make. 我们还有更多东西要做。
As as cursor is trying to prompt us, they're trying to ignore cursors, insisting let's do it ourselves. 当光标试图提示我们时，他们试图忽略光标，坚持让我们自己做。
All right. 好的。
So we are going to to want to be creating a short term memory a long term memory and an entity memory. 因此，我们需要创建短期记忆、长期记忆和实体记忆。
And we're going to do them one by one. 我们将一一进行。
So let's start by saying that the short term memory, which is the one that we'll begin with, short 因此，我们首先要说的是，短期记忆，也就是我们首先要讲的，短期记忆
term memory, trying not to press a tab or it's going to fill it all in for me. 术语记忆，尽量不要按选项卡，否则它会为我填满所有内容。
But I'm going to I'm going to do it right here. 但我要在这里做。
Short term memory is going to be something which has rag storage. 短期记忆将是有碎布存储的东西。
We we come up with a provider which is OpenAI, and a model, an embedding model to generate vectors 我们提出了一个提供者，即 OpenAI，以及一个模型，一个用于生成向量的嵌入模型
from text. 从文本。
And we'll be using this one. 我们将使用这个。
And you can substitute in whatever models you would like here. 您可以在这里替换您想要的任何模型。
It's going to be short term. 这将是短期的。
And we give it a path to where we'd like it to create that memory, that vector store as memory. 我们给它一条路径，让它到达我们希望它创建内存、向量存储为内存的位置。
And it will use chroma as it happens, something which people who've taken my, uh, alarm engineering 它会在发生时使用色度，这是那些接受过我的，呃，警报工程的人的东西
course know very well. 当然知道得很清楚。
I love chroma. 我喜欢色度。
All right, so that's the short term memory. 好吧，这就是短期记忆。
Let's also create some long term memory. 我们还可以创建一些长期记忆。
So here it is. 所以就是这样。
The long term memory is going to be just simply creating an instance of this of this class long term 长期记忆只是简单地创建此类长期的实例
memory SQLite storage. 内存 SQLite 存储。
And we'll give that also a place to go. 我们也会给它一个地方。
We'll make a database file also in the same directory. 我们还将在同一目录中创建一个数据库文件。
So that's the long term memory object. 这就是长期记忆对象。
And now finally, thirdly, we're going to create a, uh, entity memory object. 现在最后，第三，我们将创建一个，呃，实体内存对象。
So entity memory. 所以实体记忆。
And there it is. 就是这样。
So oh I see. 所以哦，我明白了。
Hang on. 不挂断。
We've got two of them. 我们有两个。
Let's do that. 让我们这样做吧。
Looks like that's good. 看起来这样不错。
So entity memory is going to be an entity memory object. 所以实体内存将是一个实体内存对象。
It's also going to be a rag storage object. 它也将成为一个碎布存储对象。
We give the provider and the embeddings model and we put it in the memory folder. 我们提供提供者和嵌入模型，并将其放入内存文件夹中。
So here we have our three types our short term memory our long term memory and our entity memory. 所以这里我们有三种类型：短期记忆、长期记忆和实体记忆。
And now we get to the place where we create our crew. 现在我们到达了创建团队的地方。
And now it's going to be very challenging. 现在这将非常具有挑战性。
It's not going to be challenging at all. 这根本不会有挑战性。
We're going to say memory equals true. 我们会说记忆等于真实。
And we are going to then just do exactly that. 然后我们就会这么做。
So crew was a little bit off base with the memory equals part. 所以船员们有点偏离基地，记忆也占了一部分。
But it's got the rest of it right. 但其余部分都是正确的。
That's all you need to do. 这就是您需要做的全部。
You set the long term memory the short term memory and the entity memory. 您设置长期记忆、短期记忆和实体记忆。
And we are almost done with memory. 我们的记忆就快结束了。
Just just as simple as that. 就这么简单。
And I said almost because there is just one or rather two very small extra changes we need to make. 我说几乎是因为我们只需要做一两个非常小的额外改变。
We need to go back up in the module to where we created these agents the trading company, the financial 我们需要返回到模块中创建这些代理的位置：贸易公司、财务公司
researcher and the stock picker. 研究员和选股者。
And we need to give them memory. 我们需要给他们记忆。
Now what we want is for the trading company finder to have memory. 现在我们想要的是贸易公司查找器有记忆。
And we just do it by saying memory equals true. 我们只是通过说记忆等于真实来做到这一点。
We don't actually want the researcher to have memory because we want it to go and do research every 我们实际上并不希望研究人员拥有记忆，因为我们希望它能够每次都进行研究
time. 时间。
But we do want the stock picker to have memory, because we don't want it to recommend the same thing 但我们确实希望选股器有记忆，因为我们不希望它推荐同样的东西
more than once. 不止一次。
And I don't know if you remember, but in the prompts, in the YAML files, I said a couple of times, 我不知道你是否还记得，在提示中，在 YAML 文件中，我说过几次，
uh, don't, don't recommend the same stock twice and things like that. 呃，不要，不要两次推荐同一只股票之类的事情。
And, and surface new companies for the for the trading company finder. 并且，为贸易公司查找器提供新公司。
And that would normally be the final change you need to make is go back and make sure that your instructions 通常，您需要做的最后一个更改是返回并确保您的指示
and your YAML files are very clearly making sure that it will take advantage of memory. 并且您的 YAML 文件非常清楚地确保它将利用内存。
Because remember, whilst memory, these abstractions are trying to make memory seem quite magical and 因为记住，在记忆的同时，这些抽象概念试图让记忆看起来非常神奇并且
taken care of for you. 为你照顾。
At the end of the day, memory just means more stuff shoved into the prompt, more relevant context 归根结底，记忆只是意味着将更多内容塞入提示、更相关的上下文中
put into the prompt so that when you call an LLM it has knowledge. 放入提示中，以便当您致电法学硕士时它会提供知识。
It's in the input is included information about prior conversations or about prior information that 输入中包含有关先前对话的信息或有关先前信息的信息
it retrieved. 它恢复了。
So with that we have set up set up the memory. 这样我们就设置好了内存。
And we are now going to bring up our terminal and then run this. 现在我们将打开终端并运行它。
And so as usual I go into already in the stock picker. 像往常一样，我已经进入选股器了。
So all I have to do is type a query I run and we'll be good to go. 因此，我所要做的就是输入我运行的查询，然后我们就可以开始了。
Let's see what happens. 让我们看看会发生什么。
So just right off the bat, we expect it to be able to take advantage of memory without needing anything 因此，我们希望它能够在不需要任何东西的情况下利用内存
more. 更多的。
What we should see is it should create a memory directory in here. 我们应该看到它应该在这里创建一个内存目录。
And it just has there is a memory directory. 它只是有一个内存目录。
Stuff is going on. 事情正在发生。
My computer is hard at work and I can see already within memory there is a Croma database that's being 我的电脑正在努力工作，我可以看到内存中已经有一个 Croma 数据库正在运行
created. 创建的。
There is a long term memory. 有一个长期记忆。
If I expand this, there is indeed a database that's been a SQLite database that's been created there. 如果我扩展它，确实有一个数据库是在那里创建的 SQLite 数据库。
And so things are happening and we can see that, that, uh, various companies are being surfaced by 所以事情正在发生，我们可以看到，呃，各种公司正在浮出水面
the market watcher and more is going on and we will, uh, let this thing run. 市场观察员和更多的事情正在发生，我们将，呃，让这件事继续下去。
My computer's hard at work, and I will see you when we have a conclusion. 我的电脑正在努力工作，当我们有结论时我会见你。
And that definitely took a bit longer. 这肯定需要更长的时间。
It was going around around the houses a little bit, but it completed it. 它在房子周围绕了一圈，但它完成了。
Recommended Microsoft this time. 这次推荐微软。
Remember, don't use this for real decisions. 请记住，不要用它来做出真正的决定。
But it was, uh, entertaining to see it at work and bouncing around between the different agents. 但看到它在工作中并在不同的代理之间来回切换是很有趣的。
And whilst we don't have as much visibility into what's happening in terms of its use of memory and 虽然我们对内存使用情况没有太多了解，
what context got provided, we can see that it's certainly built and populated different data stores 提供了什么上下文，我们可以看到它肯定是构建并填充了不同的数据存储
and both the short term memory, the long term memory and the entity memory in the Rag chroma data store 以及 Rag 色度数据存储中的短期记忆、长期记忆和实体记忆
that's been created there. 那是在那里创建的。
And the main point I want to get across is that, of course you can see the benefits of what this brings 我想表达的主要观点是，你当然可以看到这带来的好处
us. 我们。
It was so easy to set up quite a complex situation, multiple types of memory with both vector similarity 很容易设置一个相当复杂的情况，多种类型的内存都具有向量相似性
queries and SQL queries too. 查询和 SQL 查询也是如此。
And we didn't need to know anything about it. 我们不需要知道任何事情。
We simply created the objects. 我们只是创建了对象。
The short term long term entity memory objects passed them in. 短期长期实体内存对象将它们传递进来。
And then we told our agents we turned memory on by saying memory equals true for the agents that we 然后我们告诉我们的特工我们打开了记忆，说记忆对于我们的特工来说是正确的
wanted to remember things. 想要记住一些事情。
And that is a wrap on the stock picker project. 这就是选股器项目的总结。
We saw a lot of different aspects of Q AI with this project. 我们通过这个项目看到了 Q AI 的许多不同方面。
We reminder, we saw structured outputs. 我们提醒一下，我们看到了结构化的输出。
We saw our own homegrown tool as well as Serpa. 我们看到了我们自己开发的工具以及 Serpa。
We also used the not the sequential but the hierarchical process. 我们还使用了分层过程而不是顺序过程。
And now we have added in the memory feature all, all the three main types of memory in there as well. 现在我们已经在内存功能中添加了所有三种主要类型的内存。
And that is a nice tour of a lot of the functionality in crew. 这是对船员中许多功能的一次很好的游览。