And welcome to part four of day two of week eight.
Uh, there's a lot happening this week, and I have to tell you that this is the heftiest of the parts
for today.
Uh, let's get right into it.
You remember, what we're going to do today is we're going to build some other kinds of prices and put
them all together to improve the accuracy of our ability to estimate the value of products.
And again, the reason we're doing this is an opportunity to revise on the various techniques we've
learned about and solidify some of the learning, um, as well as being a fun exercise.
Uh, we're going to be using this random forest type of machine learning that we experimented with in
week six.
That is a traditional ML, but we're going to be doing it differently.
We're going to be using the vector embeddings that we have in chroma that are based on the, uh, hugging
face sentence transformer Vectorizer, which means that we're using Transformers and, uh, traditional
machine learning together.
So we'll start by doing some imports and set some constants.
This time I do remember to set the product vector, store constant, load the environment, load in
the test data.
We don't need the training data because it's sitting in Cromer and connect to Cromer itself, to the
products collection in Cromer that we put in the variable collection and then we load in from Cromer.
The results, which then gives us our vectors, our documents and our prices.
So with that we are now going to look at random forests again.
And you may remember this line which was how we did it last time.
We train a random forest regressor.
Uh, this here njobs is how many concurrent processes can run.
And if you put minus one, it will use up your entire machine.
It will run a process for every core.
And it really hammers my box.
And my M1 Mac takes about an hour to run this and I ran it already, so you should, uh, time it,
uh, to to fit your box and only use -one inch here.
If you can afford to step away and let your machine hum for a little bit.
Once that's done, you can then save the model weights to a file using this useful utility Joblib dot
dump, which is from again scikit learn.
And you can then provide a model and then just save it, save those model weights.
And what I then do is load it back in again so that I don't have to run the hours worth of training.
Now, what I do here is I load in three agent objects the specialist agent, the frontier agent, and
the random forest agent.
Let's take a quick look in the agents folder and look at those.
The specialist agent is one that we already looked at before.
We already wrote this.
This is Productionized code.
Uh, that is uh, basically in the init in the in the constructor for the specialized agent, we use
the we call modal.
By saying modal class lookup.
And we provide our service name and class name and we instantiate that class.
And then when we're actually calling this to price, we simply say self dot price dot price, which
is the function, the modal function dot remote.
And that you'll remember is how we tell modal.
We don't want to run this locally.
We want to call out to the cloud, run it remotely and bring back the results, and then it returns
it.
So this is the specialist agent that we looked at before.
If we look at the frontier agent you'll see what's here is the code that we went through last time as
I said polished up, made to look nice with comments with the parameters are identified.
There's docstring.
There's the docstrings here.
There's type hinting to describe what kinds of objects we're working with.
And this is the kind of process that you would go through to take code from being Jupyter Notebook code
to being ready for production.
And typically you wouldn't write this in JupyterLab, you would be doing this in an IDE like VSCode
or PyCharm is my favorite.
And you would build it there because it will do things like help you with the type hints and fill in
some of this gumpf.
But you can you can use JupyterLab if you wish.
Um, so this is the frontier agent.
And now if I look at the random forest agent, this is super simple.
In the constructor, we first of all create our the sentence transformer, the model that we use to
create a vector.
And then we load in the model that we just saved a second ago.
Uh, and then when it comes to the actual doing and inference running a price, uh, what we do is we
first take the description that's passed in the description of our product, we encode it into a vector.
And then we call self dot model.predict with that vector.
And that gives us our random forest results and we return it.
It's as simple as that.
You'll see I do a max of zero here.
I suggest that I floor it at zero so it can't return negative numbers.
I don't know if if it would or not.
I think I might have seen it in an earlier version, and so that seemed like a sensible precaution to
take.
We don't want it predicting negative prices.
Okay.
So anyways that is the those are the the agents.
We can then instantiate those agents.
Now this function here description is exactly the same as we did in the last one.
We simply take the item and we take its prompt and we pull out the to the nearest dollar.
The the introductory text the header.
And we also pull away the prices dollars.
So we just get back to the blurb itself, the simple description of the product.
Um, and with that in mind, we can now have a function, uh, RF, which is randomforest, which will
take an item, turn it into a description, call our Randomforest agent price to price.
It.
Uh, and with that in mind, You, of course, remember our great test harness tester test.
We can now test this with 250 data points and see how the random forest performs.
Here we go.
There's quite a lot of red in there.
You remember last time?
It got about 97, I think.
Uh, let's see how it does.
Uh, and it's, in fact, just a hair worse than it was last time.
But obviously this is super close.
It's basically the same.
So the random forest, given, uh, these improved vectors versus the word two vec vectors, gives essentially
the same number.
And you can see visually it's doing okay.
There's a sort of a wrong slope here.
And uh, some problem uh, um, over there.
But generally speaking, it's done.
Laudably.
Well, uh, not like our recent models though.
So that is the random forest.
And in the next video, we are going to move to the ensemble model that brings everything together.
I will see you there.