Okay, it's time to complete the Rag workflow in our Jupyter Lab on day 2.3.
We've got this function Getprice that you are very familiar with now, which is the super simple one
that takes a string and plucks out the floating point number from it.
And now the function for GPT for mini.
What we do is we say documents and prices is fine similares of items.
So it's passed in an item.
We find what is similar to that item.
And then we call OpenAI chat dot completions dot create.
We're going to specify GPT for mini.
We're going to pass in the messages using the function messages for.
And we'll give it a seed.
We want back five tokens.
And what comes back we say response dot choices zero dot message content.
And then we call get price on the reply to pluck out the number from that.
And that's all there should be to it.
Let's try it out on one before we before we go crazy here.
Uh, so we're going to say, um, let's see, uh, GPT four mini rag and we will pass in the test one
that we just looked at that clutch thing.
Uh, and let's see what we get back.
Uh, we get back $201 and 45, uh, and let's see what what actually, it should be test one dot price.
Uh, it's pretty close.
It's pretty close.
Look at that.
Uh, so, uh, it's great the way it's so very quick to run.
And you can see everything that's happened there.
It looked up five similar items.
It constructed a query, uh, for OpenAI that incorporated the context of those five similar items in
the prompt that it sent to OpenAI.
And then it made the call, got back the response, and stripped out the price and told us how much
it cost.
And it got pretty close.
So with that.
Do you remember our tester from before?
Our framework for testing the 250, uh, top, uh, test results.
And you remember where we came out with our model?
Uh, it's definitely scary to think.
Okay, but but now we've given a huge benefit to the frontier model.
Uh, how can it do with this?
So we can run it at this point, and we will see how it does?
Uh, and off it will go while it's doing that.
And you can already see a lot of greens in here.
Uh, it's, of course, getting a lot of information.
I want to show you what I've done over here in the agents folder.
Uh, in the agents folder, I have made something called Frontier Agent, and this is now taking exactly
the code we've just looked through and turning it into production ready code.
And the way I've done that is that you can see from this sea of red that I've been adding in comments,
which is an important step and I've been putting in type hints so that we get to see exactly what we're
doing along the way.
And you can see as we go through it's it's very simple.
It's the same stuff.
Make context creates the context to insert into the prompt messages for creates the message list in
that open AI format.
You may be familiar with this way of specifying putting a comment against each of the parameters and
then what gets returned.
You can also see some more sophisticated type hints here that specify when we have lists of strings,
and a list of dictionaries with the key of a string and a value of a string, and then find similares,
which returns a list of items similar to the given one by looking in chroma get price.
We know it well.
And then finally the price method here.
So all of this is now part of a class called Frontier Agent, which is a subclass of my superclass agent,
and it's packaged up nicely so that we can use it for production purposes.
And so again, as part of your learning as you as you see that difference between moving from a Jupyter
world into more production ready code, this is the kind of jump you would make.
And typically you'd be building this in a tool like VSCode or in PyCharm or something like that.
I tend to love PyCharm, but you can also use JupyterLab for this as we've got it right here.
You can write code in Jupyter Lab, but there are ideas that are designed for this kind of thing and
make it better.
And we'll do stuff like filling in your, your, your prompts for your for what comments to write and
things like that.
So it's worth using an IDE when you get to this point.
All right.
Hopefully I've jabbered away for long enough to see if we're doing well here.
It's getting close.
And there we have it.
Okay, so here's what we notice.
First of all, it is substantially better than GPT four mini and GPT four without Rag.
We have moved the needle in a big way, but we're still at the point that our fine tuned frontier model
that we deployed up there is working better.
So we still managed to beat even a rag pipeline based GPT four.
But it is great to see that the Rag pipeline does well and gets this close.
So and you can see what looks like a beautiful chart here.
Uh, so that was great fun.
Uh, and I hope you've enjoyed that and see the same thing.
And do spend time looking through this code, see the prompts that are created, and print out some
of these prompts and satisfy yourself that we've basically, uh, in a more, more of a manual way.
We have done what exactly what Lang chain was automating for us before with with its wrapper code.
We have gone out there, we've taken an item, we've found five similar items.
We've packaged that up into some context.
And that's what we've sent to GPT four.
And that's given us significantly better results than when we didn't do that.
Another thing to experiment with, of course I tried.
I was passing in five.
Results.
That is what you would call a hyperparameter.
We can explore what would happen if you make that ten.
What would happen if you make it two?
Having it at five means that this is still super cheap.
It's a cent or two to run all of this.
Of course you should.
You should check for yourself in case pricing may vary, but.
But it's very cheap.
Um, if you make it ten, then it's going to start to.
It will cost double that.
Uh actually not not less than double that, but it will cost a little bit more.
And so it's worth, uh, um, experimenting with these things within reason and see what you think,
how it does.
Anyway, with that, that concludes our rag pipeline, and I will see you for the next part, which
is when we move to 2.4 and start talking about random forests again and ensemble models.
See you then.