Okay, so here we are, back in Jupyter Lab, ready for the next use of a frontier model, and see this
now as somewhere where you're really building expertise.
This should be very natural to you.
As usual, we start by considering our system prompt and being thoughtful about how we will prompt the
model.
Um, what we're going to say to to the model is, look, you identify and summarize the five most detailed
items from a list by selecting deals that have the most detailed, high quality descriptions and the
most clear price, uh, respond strictly in JSON, blah, blah, blah.
Um, most important is you respond with five deals that have the most detailed product description.
It's not important to mention the terms.
Um, and now I also I have laid out here the JSON structure.
I should probably mention that this isn't required because we're going to be using structured outputs,
but I like to do this to use it as another opportunity to give more direction on exactly what's expected
in each one.
Uh, so this is something which you can experiment with.
But strictly speaking we shouldn't need to specify JSON like that.
So that's my system prompt.
The user prompt says respond with the most promising five deals from this list.
Selecting those.
Blah blah blah blah blah.
I will let you read it through.
But you know, this kind of stuff.
Well, uh, and um, uh, the deals and then it lists the deals and it calls deal dot describe for each
of those.
Okay.
That's simple.
So now let me just print the beginning of the user prompt is quite long.
So I'm just going to print the first 2000 characters of the user prompt.
So you get a sense for it.
Here it is.
Let's have a look.
So there's the uh user prompt respond with blah blah blah blah blah blah blah.
And then here are the deals.
And this is the description that we've taken from um, the, the, the description function that we
looked at before the describe method.
So essentially the task that we're assigning to GPT four is to digest this list of deals, select pluck
out from this list and there's going to be 50 of them.
Pick the five which have the clearest description and from which it can take the price.
Parse the price out of this in a way that is that is most clear.
And that is, of course, a fuzzy kind of problem that requires some thought.
It's the kind of thing that would be only a year or two ago would have been unthinkably hard.
How would you do that in a reliable way, using just engineering code.
And it's the kind of problem that's so perfect for frontier models, because that kind of nuanced understanding
and being able to realize that up to something means a price and that, but up to something off is not
the same as a price.
That kind of logic is really hard.
But frontier models are remarkably effective at understanding that, understanding the meaning behind
these expressions and using that intelligently to give us an output.
So just based on these prompts, we're able to now say, okay, let's get recommendations.
And now we're going to see a different API than usual because we're using structured output.
So my function get recommendations completion is OpenAI.
And look it's a bit different here.
The this is at least as of this moment uh is a beta offering from OpenAI.
But I imagine it's going to be fully productionized very, very soon.
So you say OpenAI beta or that's beta for Americans.
Uh, OpenAI beta, dot chat, dot completions, dot parse.
Uh, that's the, the the way that you call this, um, and you provide the model as, as usual.
And this is supported by GPT four and mini and GPT four, the frontier version two.
Uh, as always, the message is, you know, I'm just going to not even say it.
You know it.
You don't need me to tell you.
Surely, uh, this is the difference.
You remember, in the past, we've used this response format as a way of specifying that we require
JSON to come back.
Now, this time we don't say JSON.
What we do is we pass in the class the deal selection class.
That's the class we were just looking at here, the one that I did some scrappy JSON for.
We pass in that class in as our response format.
And then when we're getting the results we don't call our usual API calls.
We say completion is zero as usual dot message and then not content but instead dot parsed.
And that is going to take the response and parse it into an instance of deal selection.
And that is what it will return.
So with that build up it better return it.
Let's run that code and see what happens and what we get back.
So we are calling GPT for zero and we're sorry GPT for mini.
We are specifying a particular structured format of output.
It has returned.
Let's see why don't we print what type of thing has come back type results.
Let's see what kind of object has come back.
It is an agents deals deal selection.
That is indeed the kind of object that we were hoping to get back.
I think we better, uh, see what it actually looks like.
It's going to be a set.
Uh, it's a deal selection which has within it deals is the one field.
If you remember.
Deals is the one thing we set.
And here are the deals.
So let's let's find out how many deals we got back.
We are hoping of course, that we got back five deals.
Let's see.
We did indeed get back five deals.
And let's print the first of those deals.
And we get a Samsung Galaxy Watch Ultra, a top tier smartwatch.
Uh, and we've got a nice summary of this.
And you'll notice this is actually looking it's looking great for us.
It's looking like this is the kind of summary that we're going to want to use when we pass it into our
prices to estimate the price of this, and GPT four mini has pulled out a price and a URL.
And of course, if we click on that URL, it's going to open up that deal.
There it is.
Wonderful.
Uh, so it is working well for us.
Uh, okay.
Now, the last thing to show here is that, um, we are going to, uh, look to import our scanner agent.
So I'm going to do the thing again where I'm going to kick this off because it's going to take a minute
or two.
Um, and while it's running, I'm going to go over and show you what the scanner agent is.
So the scanner agent is over here.
Uh, this is a bunch of code that's going to look very similar to the code that we looked at before.
Uh, and so it's, uh, just been written now with comments and with better structure so that this is
something we can consider to be production quality.
see.
Um, but it has the system prompts that we just looked at.
It has a user prompt that we looked at and it says respond strictly with five deals, no more.
And what we get is an init method that sets it up and connects to OpenAI.
The fetch deals is the method that we were just looking at that does, uh, gets a list of scraped deal
by calling scrape deal fetch, just as you would expect.
What it also does here, though, is that potentially it gets a memory object, which is a list of URLs,
and it will remove from the results anything that it remembers has already been surfaced in a previous
search.
And this is how we make sure that we fetch new deals, not deals that we've already looked at before.
And of course, memory, which is one of the of the facets of building an agent framework, is something
we'll be looking at more in the future.
Uh, and then we make user prompt and then we do, uh, this is the same code as before.
And then scan is basically exactly the function that we just looked at.
We make a user prompt and then we call OpenAI beta dot chat, dot completions, dot parse.
We pass in the model.
We specify that the response format needs to be the deal selection class, so that we know that it's
going to return that and we parse it back into result.
And that is what we get to right here.
When we then, um, uh, we do first remove any deals.
If the price isn't greater than zero, we don't want to return deals which have zero price.
Um, and we return that result.
And if we got no deals back, um, because perhaps we'd already surfaced everything, then we return
none.
And that's indicated by that optional at the top there.
So that's the class.
And with that, let me go back to day three.
And we should see that the results have come back.
Now one of the things I'm aware of that you may have caught, you may have noticed this yourself when
I was doing this, but I was busy thinking about this while I was talking is that I think we might have
just seen a goof from the model that the price here it's saying, is $350 associated with this Samsung
Galaxy Watch.
But I think when this website came up, let's close this down that it says up to 350 off with trade
in.
If you have another device you can save up to 350.
So after I showed off about how frontier models are so incredibly good at this stuff, this is an example
of a mistake that the model has actually made, that it thinks that this is worth 350, when in fact
it's $350 off.
So I should eat my words.
And it did actually get that wrong.
And it's going to be interesting.
That is, it's useful to have learned that and to have seen that.
And I might try adding that in to the system prompt and see if we can improve on the performance of
that.
Now that we see that it can make those mistakes.
Anyway, I will see you next time for the slides.