1
00:00:00,200 --> 00:00:01,240
And week three.

2
00:00:01,280 --> 00:00:02,680
Day four is a go.

3
00:00:02,720 --> 00:00:03,880
Let's get started.

4
00:00:03,920 --> 00:00:08,000
So last time we did a stock picker and we just have a little tiny bit more to put into that.

5
00:00:08,000 --> 00:00:10,560
And then we're going to move on with our next project.

6
00:00:10,600 --> 00:00:12,320
A developer agent.

7
00:00:12,600 --> 00:00:14,640
But first another repetition.

8
00:00:14,640 --> 00:00:17,960
I hate to repeat these things, but sometimes it's good to drum it in.

9
00:00:18,240 --> 00:00:20,760
Building a crew project involves five things.

10
00:00:20,800 --> 00:00:23,120
First of all, crew I create crew.

11
00:00:23,560 --> 00:00:28,440
The name of the project to set up your crew, build those directories and those files.

12
00:00:28,480 --> 00:00:33,760
Number two, you find the YAML files for your agents and tasks, and you fill them in to define your

13
00:00:33,760 --> 00:00:35,040
agents and tasks.

14
00:00:35,280 --> 00:00:40,400
Number three, you go to the crew.py module, which is where you actually create the instances.

15
00:00:40,400 --> 00:00:44,960
And you use decorators to identify the agents and tasks that you'll be using.

16
00:00:45,000 --> 00:00:47,320
And then you create your crew itself.

17
00:00:47,360 --> 00:00:53,240
And this is where you can have structured outputs to make sure that the outputs conform to a schema.

18
00:00:53,240 --> 00:00:58,960
And you can use tools, both tools like Serpa, that crew provides for us that run remotely, and then

19
00:00:58,960 --> 00:01:03,060
custom tools that we build locally, like the thing that sends a push notification.

20
00:01:03,340 --> 00:01:10,020
And then number four, you update Main.py to set any inputs so that we can pass something in, configure

21
00:01:10,020 --> 00:01:12,140
the fields that are templated with the curlies.

22
00:01:12,500 --> 00:01:15,100
And finally we run with crew.

23
00:01:15,140 --> 00:01:18,220
I run and off goes our project.

24
00:01:18,740 --> 00:01:22,420
So I now want to cover a feature of crew called memory.

25
00:01:22,420 --> 00:01:28,060
And this is a feature that is a bit more prescriptive, a bit more opinionated in the crew framework.

26
00:01:28,100 --> 00:01:34,140
Memory, of course, is talking about how you provide information, contextual information to Llms each

27
00:01:34,140 --> 00:01:39,180
time you call them, and you can implement that yourself just by storing variables and then passing

28
00:01:39,180 --> 00:01:43,660
them in when you do things like creating tasks so you can do it the sort of manual way.

29
00:01:43,820 --> 00:01:49,180
But the crew framework also comes with some building blocks that lets you use their constructs around

30
00:01:49,180 --> 00:01:52,140
memory out of the box, and that comes with pros and cons.

31
00:01:52,180 --> 00:01:57,020
The pro is that you get up and running quickly, and you can use a lot of the thinking that they put

32
00:01:57,020 --> 00:01:57,820
behind this.

33
00:01:58,020 --> 00:02:03,610
The con is that there's there's a learning curve, and it obscures some of the detail of how prompts

34
00:02:03,650 --> 00:02:05,250
actually work behind the scenes.

35
00:02:05,250 --> 00:02:10,730
So as as with many times when you're adopting a framework like this, it's something to be aware of

36
00:02:10,810 --> 00:02:13,050
the benefits and the trade offs of doing so.

37
00:02:13,410 --> 00:02:18,130
But let's say that we are going to embrace cruise way of handling memory and talk about what it actually

38
00:02:18,130 --> 00:02:18,610
does.

39
00:02:18,850 --> 00:02:24,330
Well, it has five different types of memory, five different frameworks that you can include.

40
00:02:24,530 --> 00:02:26,850
And one of them is called short term memory.

41
00:02:26,850 --> 00:02:34,570
And this is just about storing recent interactions using a vector database in in a in a rag way.

42
00:02:34,570 --> 00:02:37,210
If you're familiar with retrieval augmented generation.

43
00:02:37,210 --> 00:02:40,650
And you don't need to be for this course because we're just going to put the code in there and see it

44
00:02:40,650 --> 00:02:41,130
run.

45
00:02:41,130 --> 00:02:43,250
But if you do no, then this will make more sense.

46
00:02:43,250 --> 00:02:49,850
So this will allow agents to access recent relevant information when they are currently executing.

47
00:02:50,450 --> 00:02:56,330
And then a different concept called long term memory is when more important information is stored in

48
00:02:56,330 --> 00:03:03,010
a SQL database for longer term recall to build up knowledge over over a longer period of time.

49
00:03:03,610 --> 00:03:08,510
And then there's something called entity memory that's very similar to short term memory, actually.

50
00:03:08,550 --> 00:03:15,310
It's it's basically when there's things about people, places and concepts, then those can also be

51
00:03:15,310 --> 00:03:22,430
stored in a Rag database for vector based similarity search and to be included in the context.

52
00:03:22,830 --> 00:03:26,110
And then there's crew describes this as another kind of memory.

53
00:03:26,110 --> 00:03:27,230
But I think it's a bit misleading.

54
00:03:27,230 --> 00:03:33,990
I think what they call contextual memory is just a sort of umbrella term for the short term, long term

55
00:03:33,990 --> 00:03:42,070
and entity memory that can all together be queried and passed in as context when prompting an LM and

56
00:03:42,070 --> 00:03:43,910
crew abstracts all this away from you.

57
00:03:43,910 --> 00:03:48,190
So it's just going to be a few lines of code to have all of these types of memories running.

58
00:03:48,390 --> 00:03:50,430
But in doing so, as I say, big benefit.

59
00:03:50,470 --> 00:03:53,070
A lot of work happens behind the scenes.

60
00:03:53,070 --> 00:03:57,030
And perhaps also there the trade off is that you've got less visibility into it.

61
00:03:57,030 --> 00:04:01,270
So if things don't go the way you want, then it's a bit harder to debug and figure out what's going

62
00:04:01,270 --> 00:04:01,630
on.

63
00:04:01,910 --> 00:04:08,500
And then there is another kind of memory called user memory, which is to store user specific information.

64
00:04:08,500 --> 00:04:14,940
And actually, at least as of now in Crewe, this is a concept that they support and have some frameworks

65
00:04:14,940 --> 00:04:21,300
around, but it's mostly left up to you to be querying user memory and then inserting it into the prompt

66
00:04:21,300 --> 00:04:22,900
or providing it at the right time.

67
00:04:22,900 --> 00:04:28,020
So user memory is a bit of an odd one out here, and I suspect that they're looking to build more into

68
00:04:28,020 --> 00:04:28,940
that in time.

69
00:04:29,260 --> 00:04:33,180
And for now, for the code that we're about to look at, we're just going to really look at contextual

70
00:04:33,180 --> 00:04:33,540
memory.

71
00:04:33,540 --> 00:04:40,300
So short term long term and entity memory and seeing how we can incorporate that into our stock picker

72
00:04:40,300 --> 00:04:40,980
solution.

73
00:04:41,420 --> 00:04:44,540
And so we're back in the stock picker project in cursor.

74
00:04:44,540 --> 00:04:47,260
And we are looking at the crew.py module.

75
00:04:47,580 --> 00:04:53,100
And I'm going to start by putting in some new imports in here, which are interesting ones from crime

76
00:04:53,100 --> 00:04:53,460
memory.

77
00:04:53,460 --> 00:04:58,500
We're going to import long term memory short term memory and entity memory.

78
00:04:58,500 --> 00:05:00,180
The three types we'll be working with.

79
00:05:00,220 --> 00:05:03,660
And I do believe you can also have user memory in there too.

80
00:05:03,700 --> 00:05:06,220
But then you have to manually manage it yourself.

81
00:05:06,800 --> 00:05:13,320
And then from my memory storage rag storage, we're importing a class called Rag storage for vector

82
00:05:13,320 --> 00:05:14,880
based retrieval.

83
00:05:15,160 --> 00:05:24,120
And with that we're also going to import from long term memory SQLite storage, long term memory SQLite

84
00:05:24,160 --> 00:05:26,720
storage object like that a class.

85
00:05:26,920 --> 00:05:27,400
All right.

86
00:05:27,400 --> 00:05:30,640
So that's a few things that we're now going to put to good use.

87
00:05:30,640 --> 00:05:37,080
We're now going to go to the crew function the function that creates the crew within this this module.

88
00:05:37,080 --> 00:05:39,320
And you can see where we made our manager.

89
00:05:39,320 --> 00:05:41,320
And we've got a few more things to make.

90
00:05:41,360 --> 00:05:48,120
As as cursor is trying to prompt us, they're trying to ignore cursors, insisting let's do it ourselves.

91
00:05:48,320 --> 00:05:48,720
All right.

92
00:05:48,720 --> 00:05:55,960
So we are going to to want to be creating a short term memory a long term memory and an entity memory.

93
00:05:55,960 --> 00:05:58,280
And we're going to do them one by one.

94
00:05:58,280 --> 00:06:04,440
So let's start by saying that the short term memory, which is the one that we'll begin with, short

95
00:06:04,480 --> 00:06:08,780
term memory, trying not to press a tab or it's going to fill it all in for me.

96
00:06:08,780 --> 00:06:10,820
But I'm going to I'm going to do it right here.

97
00:06:10,860 --> 00:06:14,940
Short term memory is going to be something which has rag storage.

98
00:06:15,340 --> 00:06:22,460
We we come up with a provider which is OpenAI, and a model, an embedding model to generate vectors

99
00:06:22,460 --> 00:06:23,420
from text.

100
00:06:23,420 --> 00:06:24,620
And we'll be using this one.

101
00:06:24,620 --> 00:06:27,860
And you can substitute in whatever models you would like here.

102
00:06:27,860 --> 00:06:29,220
It's going to be short term.

103
00:06:29,220 --> 00:06:35,380
And we give it a path to where we'd like it to create that memory, that vector store as memory.

104
00:06:35,620 --> 00:06:41,540
And it will use chroma as it happens, something which people who've taken my, uh, alarm engineering

105
00:06:41,540 --> 00:06:42,540
course know very well.

106
00:06:42,540 --> 00:06:43,460
I love chroma.

107
00:06:43,700 --> 00:06:47,180
All right, so that's the short term memory.

108
00:06:47,340 --> 00:06:50,660
Let's also create some long term memory.

109
00:06:50,660 --> 00:06:51,620
So here it is.

110
00:06:51,620 --> 00:06:57,260
The long term memory is going to be just simply creating an instance of this of this class long term

111
00:06:57,260 --> 00:06:58,820
memory SQLite storage.

112
00:06:58,820 --> 00:07:01,020
And we'll give that also a place to go.

113
00:07:01,060 --> 00:07:04,660
We'll make a database file also in the same directory.

114
00:07:04,980 --> 00:07:07,140
So that's the long term memory object.

115
00:07:07,500 --> 00:07:13,850
And now finally, thirdly, we're going to create a, uh, entity memory object.

116
00:07:13,850 --> 00:07:15,810
So entity memory.

117
00:07:16,290 --> 00:07:17,250
And there it is.

118
00:07:17,450 --> 00:07:19,450
So oh I see.

119
00:07:19,490 --> 00:07:19,730
Hang on.

120
00:07:19,770 --> 00:07:20,570
We've got two of them.

121
00:07:20,570 --> 00:07:21,530
Let's do that.

122
00:07:21,930 --> 00:07:22,810
Looks like that's good.

123
00:07:22,850 --> 00:07:25,250
So entity memory is going to be an entity memory object.

124
00:07:25,250 --> 00:07:28,930
It's also going to be a rag storage object.

125
00:07:28,930 --> 00:07:33,770
We give the provider and the embeddings model and we put it in the memory folder.

126
00:07:33,970 --> 00:07:40,370
So here we have our three types our short term memory our long term memory and our entity memory.

127
00:07:40,530 --> 00:07:44,530
And now we get to the place where we create our crew.

128
00:07:44,850 --> 00:07:47,210
And now it's going to be very challenging.

129
00:07:47,250 --> 00:07:48,570
It's not going to be challenging at all.

130
00:07:48,610 --> 00:07:51,170
We're going to say memory equals true.

131
00:07:51,890 --> 00:07:54,810
And we are going to then just do exactly that.

132
00:07:54,850 --> 00:07:57,490
So crew was a little bit off base with the memory equals part.

133
00:07:57,490 --> 00:07:58,690
But it's got the rest of it right.

134
00:07:58,690 --> 00:08:00,330
That's all you need to do.

135
00:08:00,490 --> 00:08:04,970
You set the long term memory the short term memory and the entity memory.

136
00:08:04,970 --> 00:08:07,010
And we are almost done with memory.

137
00:08:07,010 --> 00:08:08,610
Just just as simple as that.

138
00:08:09,170 --> 00:08:14,510
And I said almost because there is just one or rather two very small extra changes we need to make.

139
00:08:14,550 --> 00:08:21,910
We need to go back up in the module to where we created these agents the trading company, the financial

140
00:08:21,910 --> 00:08:23,430
researcher and the stock picker.

141
00:08:23,550 --> 00:08:25,310
And we need to give them memory.

142
00:08:25,350 --> 00:08:29,830
Now what we want is for the trading company finder to have memory.

143
00:08:29,830 --> 00:08:32,190
And we just do it by saying memory equals true.

144
00:08:32,510 --> 00:08:36,390
We don't actually want the researcher to have memory because we want it to go and do research every

145
00:08:36,430 --> 00:08:36,950
time.

146
00:08:37,110 --> 00:08:41,830
But we do want the stock picker to have memory, because we don't want it to recommend the same thing

147
00:08:41,870 --> 00:08:42,550
more than once.

148
00:08:42,550 --> 00:08:48,470
And I don't know if you remember, but in the prompts, in the YAML files, I said a couple of times,

149
00:08:48,790 --> 00:08:52,590
uh, don't, don't recommend the same stock twice and things like that.

150
00:08:52,590 --> 00:08:56,190
And, and surface new companies for the for the trading company finder.

151
00:08:56,190 --> 00:09:01,070
And that would normally be the final change you need to make is go back and make sure that your instructions

152
00:09:01,070 --> 00:09:05,750
and your YAML files are very clearly making sure that it will take advantage of memory.

153
00:09:05,750 --> 00:09:11,580
Because remember, whilst memory, these abstractions are trying to make memory seem quite magical and

154
00:09:11,580 --> 00:09:12,780
taken care of for you.

155
00:09:12,780 --> 00:09:19,660
At the end of the day, memory just means more stuff shoved into the prompt, more relevant context

156
00:09:19,700 --> 00:09:24,220
put into the prompt so that when you call an LLM it has knowledge.

157
00:09:24,260 --> 00:09:30,220
It's in the input is included information about prior conversations or about prior information that

158
00:09:30,220 --> 00:09:31,020
it retrieved.

159
00:09:31,340 --> 00:09:34,660
So with that we have set up set up the memory.

160
00:09:34,820 --> 00:09:39,860
And we are now going to bring up our terminal and then run this.

161
00:09:40,220 --> 00:09:43,900
And so as usual I go into already in the stock picker.

162
00:09:43,900 --> 00:09:47,540
So all I have to do is type a query I run and we'll be good to go.

163
00:09:47,740 --> 00:09:49,060
Let's see what happens.

164
00:09:49,260 --> 00:09:55,420
So just right off the bat, we expect it to be able to take advantage of memory without needing anything

165
00:09:55,420 --> 00:09:55,820
more.

166
00:09:55,980 --> 00:09:59,540
What we should see is it should create a memory directory in here.

167
00:09:59,540 --> 00:10:02,260
And it just has there is a memory directory.

168
00:10:02,460 --> 00:10:04,020
Stuff is going on.

169
00:10:04,060 --> 00:10:10,340
My computer is hard at work and I can see already within memory there is a Croma database that's being

170
00:10:10,340 --> 00:10:10,980
created.

171
00:10:10,980 --> 00:10:12,960
There is a long term memory.

172
00:10:12,960 --> 00:10:18,480
If I expand this, there is indeed a database that's been a SQLite database that's been created there.

173
00:10:18,480 --> 00:10:26,440
And so things are happening and we can see that, that, uh, various companies are being surfaced by

174
00:10:26,440 --> 00:10:33,320
the market watcher and more is going on and we will, uh, let this thing run.

175
00:10:33,360 --> 00:10:37,640
My computer's hard at work, and I will see you when we have a conclusion.

176
00:10:38,080 --> 00:10:39,640
And that definitely took a bit longer.

177
00:10:39,640 --> 00:10:43,040
It was going around around the houses a little bit, but it completed it.

178
00:10:43,040 --> 00:10:44,760
Recommended Microsoft this time.

179
00:10:44,880 --> 00:10:46,680
Remember, don't use this for real decisions.

180
00:10:46,680 --> 00:10:51,080
But it was, uh, entertaining to see it at work and bouncing around between the different agents.

181
00:10:51,200 --> 00:10:56,280
And whilst we don't have as much visibility into what's happening in terms of its use of memory and

182
00:10:56,280 --> 00:11:02,320
what context got provided, we can see that it's certainly built and populated different data stores

183
00:11:02,360 --> 00:11:09,320
and both the short term memory, the long term memory and the entity memory in the Rag chroma data store

184
00:11:09,320 --> 00:11:10,480
that's been created there.

185
00:11:10,520 --> 00:11:15,550
And the main point I want to get across is that, of course you can see the benefits of what this brings

186
00:11:15,550 --> 00:11:15,750
us.

187
00:11:15,750 --> 00:11:23,590
It was so easy to set up quite a complex situation, multiple types of memory with both vector similarity

188
00:11:23,590 --> 00:11:25,710
queries and SQL queries too.

189
00:11:25,750 --> 00:11:27,430
And we didn't need to know anything about it.

190
00:11:27,470 --> 00:11:29,830
We simply created the objects.

191
00:11:29,830 --> 00:11:33,950
The short term long term entity memory objects passed them in.

192
00:11:34,070 --> 00:11:40,190
And then we told our agents we turned memory on by saying memory equals true for the agents that we

193
00:11:40,190 --> 00:11:41,910
wanted to remember things.

194
00:11:42,350 --> 00:11:45,510
And that is a wrap on the stock picker project.

195
00:11:45,550 --> 00:11:49,350
We saw a lot of different aspects of Q AI with this project.

196
00:11:49,390 --> 00:11:52,150
We reminder, we saw structured outputs.

197
00:11:52,310 --> 00:11:55,870
We saw our own homegrown tool as well as Serpa.

198
00:11:56,190 --> 00:12:01,750
We also used the not the sequential but the hierarchical process.

199
00:12:01,950 --> 00:12:09,750
And now we have added in the memory feature all, all the three main types of memory in there as well.

200
00:12:09,790 --> 00:12:14,390
And that is a nice tour of a lot of the functionality in crew.