Here we are in our favorite place to be in JupyterLab, ready for some coding and a lot of coding that's
going to be today.
The first thing you may spot, it may have caught your eye that there isn't a day two Jupyter Notebook.
There are, in fact, five day two Jupyter notebooks, which is a clue that we've got some work cut
out for us.
And it does also mean that I might be going a bit faster through the notebook than usual, and that's
just an opportunity for you to come back and do it yourself and and experiment.
Um, so today we're going to be building a more complex approach to estimating prices of goods, the
same thing we've been working on for a while.
Um, and the biggest part of today is that we're going to try and approach a rag pipeline solution for
doing the same thing.
Uh, the the intuition is surely this is actually a really great use case for Rag.
We've got 400,000 prices of products in our training data set.
Uh, what if we put all of them in a data store, and when we're calling a model, we give it some similar
products and say, hey, please, can you give me the price of this electric toothbrush?
And by the way, here are five similar electric toothbrushes from our training data and give that to
a model to help it.
And it will make total sense to use a frontier model for that.
And you've got to imagine that armed with some similarities, we're giving a huge head start to the
frontier model.
And so it is a great example of where rag should shine, because we've got a repository of accurate
data.
So it's definitely fits right within the sweet spot.
So what we're going to do in this notebook, the 2.0 notebook that I'm in right now in the week eight
folder, we're going to create our vector data store with 400,000 training data points.
So that's a big deal.
That's a lot more than what we did in week five.
In the next notebook day 2.1.
We're going to visualize it in 2D and then we're going to visualize it in 3D.
So those are quick notebooks.
But it's great to get a hands on sense of these things in day 2.3.
We're then going to build our Rag pipeline and test it.
And in day 2.4 this is where we do a lot in 1 in 1 notebook.
We're going to bring back the random forest processor that we looked at back in week six.
And but we're going to do an improved version of it.
And then we're going to build what's called an ensemble which which allows all of the models.
Essentially, you can think of it as a sort of voting together.
They're working together on the problem, and it's picking a linear combination of them that is going
to give the best results.
So a lot to get through.
I did want to to mention, look, that we've already built a powerful product processor in the form
of a specialized LLM.
So you might be thinking, why are we spending so much time working on this?
And the answer is simple.
It's what I said a moment ago.
This is see this as really about solidifying your expertise.
We're doing this so you can get more practice with rag.
More practice with other kinds of aspects of building sort of agent based functions, whilst also working
on the same project.
So we know what we're doing and it's satisfying, and we don't have to curate more data because we have
the data.
So hopefully that that sort of motivates you for what we're about to do.
All right, without further ado, we will run some imports environment variables, log in to hugging
face stuff we know.
And I'm going to again I'm going to load in the training data, the pickle file that we have from before
to simplify things just loaded in the training data.
So you may remember we can look at the first of the training data points fuel pump module.
We can call prompts if you remember this.
And we will get the familiar prompt that's used for training.
How much this cost to the nearest dollar.
And then the price is and the value right there.
Okay.
With that time to create our Chroma Datastore.
So you may remember back in week five, we created this store with it had 123 documents, if I remember
that right, 123 chunks which were taken from our documents, um, of the fictional company ensure ensure
um, that we used.
So this time we're going to create a chroma datastore with 400,000 documents.
That's a lot.
We're not going to chunk it up.
Of course we don't need to.
These are already small chunks.
Each each product is going to go in chroma as its own item.
Um, so, uh, we're not going to be using Lang chain for this rag pipeline because it's good for us
to, to actually build it ourselves.
Uh, Lang chain gives some good, useful abstractions, but it's not necessary.
And once you get to the level of proficiency that you are now at, you can just be working directly
with Llms often you won't need necessarily these these sorts of abstraction layers built on top of it.
Um, so that's what we're going to do.
Uh, so we are going to create a chroma database, and I called it somewhere up here, I think.
Um, I might have taken that constant out earlier.
Let's do that right now.
Apologies.
Uh, we will add in here another constant DB equals products underscore vector store.
I was cleaning up this notebook to make it useful, and I cleaned it up too much.
Uh, if I spelled that right, I have products.
Vector store.
There we go.
All right, so back we go.
Momentary diversion.
So we're going to create a chroma data store with that path.
There we go.
We're going to check if it exists and delete it if it does already exist.
I'm not going to run that.
It will delete this products vector store which I have created.
And which takes a little bit of time.
Uh, although not that much time, but.
But I will let you run this yourself.
So we are going to use a new a different type of embedding.
We are going to use something called the sentence transformer.
It's a useful model from hugging face it maps text chunks to 384 dimensions.
Um, and it's ideal for things like semantic search.
It's a transformer model that produces these vector embeddings.
You may remember we used OpenAI embeddings when we built our Rag pipeline before.
So if you compare the two of those together, this hugging face one, uh, it's it doesn't have the
same dimensionality.
So and it's probably not as accurate as the OpenAI embeddings one.
But it's free, it's fast and we can run it locally.
And that last point is super important because there are some cases where you might not want the data
to leave your box.
For example, if you're working on that project I assigned you from week five and you're looking to
build your own personal Rag database with your own stuff like your emails and things.
You might not want to send them all to OpenAI, even though it should be safe, you just might prefer
to do it yourself, and this is an easy way to do it without it leaving your box.
So we load in that model from hugging face.
It gives a warning that I think we can ignore about in the future, that something will have to be set
differently.
Um, so what you can then do once you've created this model is you can call model dot encode.
You pass in a list of text documents.
What you'll get back is a numpy array.
And you can just I can pluck the first one out of that and I will get my vector.
And it was pretty quick.
And it works well in bulk as well.
So if we if we look at the length of this vector we will see it's got 384 dimensions to it.
We can just print it and we'll see.
It will be a big old vector.
There it is.
So that vector in some way represents well hi there in numbers in a way that is a multi-dimensional
reflection According to the LM.
So that is, uh, is worth, uh, um, playing with and getting a sense of it.
So what I'm now going to do is write this utility method description that takes an item, one of our
items, and turns it into just a chunk of text.
So if I run that just to show you, if you remember, if we look at something like the first training
point and we look at its prompt, that's what the prompt looks like.
If instead of that I now call description on that item, I just get the same thing, but without the
stuff at the front and without the price, it's just a description of the product on its own.
And what we now do is we go and put that into our vector data store.
So we run that code and it will take a while.
And by the time it's finished, it will be in the vector data store and you'll be ready for business.
I've already run it, so it's already created my vector data store.
You can see over on the left.
You should do that now.
And when you're done, I will see you for the next video.