So it's business time right now.
We are going to build a Rag pipeline to estimate the price of products, drawing on context for similar
products that we have in our training dataset.
Uh, and so we are right here.
Day 2.3 building a Rag pipeline with GPT four mini.
Uh, we're going to go pretty quickly today through a lot of code.
So bear with me then.
Remember the trick is to come back, run the code yourself and get a good sense of what's going on.
So we do some imports, we log in as usual, and then we, uh, connect with OpenAI.
Um, and it's using the fact that we've set the OpenAI API key.
We're going to load in the test data set.
Now, we don't need to load in the training data set because we'll be using the chroma data store for
that.
All right.
So here's a function make context.
We're going to use this function to produce the context that we're going to send GPT four mini that
tells it about similar products that it could use.
So we're going to say to provide some context, here are some other items that might be similar to the
one you need to estimate.
And then do you remember this zip construct that allows us to iterate through two different lists together.
So we're going to collect similar and price from iterating through the similars and prices that are
passed in.
And then we're going to add in a message there's a potentially related product similar with this price.
I'll execute this in just a second.
So you see what it actually looks like.
But then just as we've done in the past, I'm making this function messages for and it takes an item,
it takes things similar to it and it takes prices and it's going to build that standard list of dicts
that we know so, so well.
There's going to be a system message which is going to be just the same one we've used before.
You estimate price of items, reply only with the price we're going to add in the context.
We're going to add in the stuff that we just built right here.
And then we're going to say.
And now the question for you.
And now this is basically exactly the same as we used before when we called GPT four back in week six
when we built that pipeline.
So the thing to notice here is that we're doing this Rag pipeline ourself without Lang chain.
And it's actually not that hard.
Lang chain gave us a nice little abstraction on top with with a few simple objects.
And I remember going on about how it was just like one line of code or something to do it, but it's
not that much more just to do it ourselves.
Particularly now that you understand what's going on under the hood and you know how to call llms and
you know how to look up similar objects and so on.
Um, it becomes relatively straightforward.
Uh, so, um, hopefully this is not going to be too, uh, um, difficult, but let's keep going.
Um, so we're going to, um.
Collect our chroma.
Um, and once more, I've done this again, haven't I?
I deleted the cell where I defined the DB variable.
It should be products Underscore.
Vector store.
Some possibly hard word to spell.
There we go.
And now run that again.
Fine.
So we've now we're looking in the products collection in our vector data store.
Um okay.
And so we're now getting to the meat of the whole thing.
So we're going to have a method, a function description that's going to take an item.
And it's going to strip out from that item the stuff that we don't care about.
So basically we'll take the prompt and we're going to take out this.
How much does it cost to the nearest dollar.
And we're going to then uh, ignore everything that comes after price is dollars.
Let me show you exactly what's going on here.
Uh, so if I have a quick look at my first training data point.
Oops.
Sorry.
What have I done?
Test the first test data point.
We're not looking at training data anymore.
The first test data point is this the repair kit for Ford, blah blah, blah, blah, blah with a price
on it.
Uh, if we look at its prompt, you'll see that that that's got all of this gubbins in there with the
price and the question at the top.
But what I can do is I can just say describe description of test zero.
And what we should now get is this just the blurb without the price.
So that should be clear.
All right.
Now we're going to load the model that is the sentence transformer from hugging face.
That gives us our simple vector encodings.
It is our vectorizer.
And so this method here, this is in fact our Vectorizer function.
It takes an item.
It it calls description on that item to turn it into text.
And it then puts it in a list and calls model dot encode.
Simple as that.
And now and now we get to a function find similars.
It will be given an item and it will return similar items.
And this, this is the some of the hardest part that Lang was doing for us before, but it's not that
hard.
You can see what we do is we say to our collection, our chroma DB collection, I want to query this.
These are the query embeddings.
This is it's going to be based on this, uh, the vector that we will pass in.
We do have to turn that into a floating point number from being a numpy array.
That's uh, from being a numpy float 32.
You have to turn them into floats, and then we have to turn it into a list instead of being a numpy
array.
And then you just simply say, uh, number of results and that's how many you want back.
So you can pass in a vector as your query embedding and get back five results.
That's all there is to it.
And what we'll get back is uh, is some similars, uh, so let's just run this.
So now what we can do is we can look at test number one and let's look at test number one's prompt.
So this is its prompt.
Um how much does it cost to the nearest dollar.
It's a fan clutch package.
Uh, from, um motorcraft.
So anyway, you get a sense.
So we can now say documents and prices are fine.
Find Similares from test one.
We're going to call this function right here to find five similar results.
Let's do that.
And now it's thinking.
And now let's print that.
And here you will find five related results are potentially related products.
And they are all potentially related product.
They are all sort of fan clutchy kind of things that have various prices.
Uh, and they do appear to be similar products at first blush.
Uh, so you should convince yourself, do some more testing and make sure you're comfortable that indeed
we can call this find similar function, which is simply calling query on our Cromer collection.
And we will be able to collect similar products from our Cromer database.
It's as simple as that.
Uh, and once we've done that, we will then be able to put the final touches on our Rag data flow on
our Rag pipeline and then use that to call GPT four zero.
And we will do that in the next video.