Okay, well, here we are, back in cursor, one of my very favorite places to be.
And we're going into the week.
One directory on the left.
Check the LM Engineering's in block caps here.
So you're in the right project.
And then go to day two, which is where we find ourselves right now.
And I need to remind you before you do anything, you have to go to the top right to where it says Select
kernel for you.
You click on that.
You click in the Python Environments option.
The first option you pick the dot VM.
That's Python 3.12 that is matching this VM right here.
And that means your kernel is set.
Welcome to the day two lab.
So I just wanted to mention here before we start that there is a page of resources on my home page and
I've linked to it here.
I'll try and keep adding useful resources.
So do keep that bookmarked.
You can go there and check it out right now if you wish.
Okay okay so we're now going to go back again to doing what we did yesterday, but do it with a little
bit more discipline.
And I want to tell you about this thing called the Chat Completions API.
And this is the name of it's the simplest way that you can call an LLM, particularly a frontier LLM.
And it's called chat completions because it's a nod to what you're actually doing.
When you call an LLM, you're giving it a chat a conversation so far, and you're asking it to predict
the most likely message to come next.
And you can think of that as as like completing a chat.
That's what the LLM thinks it's doing.
It's it's it's like predictive text.
It's just trying to predict the most likely words to come next.
And as a side effect of that, it happens to be really good at answering whatever questions it's asked.
But all it's actually trying to do is to predict the most likely next few words, or as we'll shortly
explain, the most likely next tokens.
Okay, so it was actually this chat completions API.
This this approach was invented by OpenAI first and foremost.
But it was so popular the structure and style of this type of request became so popular that it's become
ubiquitous.
All of the providers offer the Chat Completions API.
It is the kind of standard way to interact with an LLM.
And we're going to start again with OpenAI.
I know some of you are antsy to get off OpenAI and use free models.
Your time is coming any second now.
Just watch this if you don't want to use OpenAI, and we'll get to you in a second.
All right.
So first of all, I'm going to do a repeat of last time where we use this thing called load dot EMV,
which is going to load in the secrets in our EMV file and check that we have OpenAI key set.
So this load EMV function is something which which makes which loads in anything you've saved in your
EMV.
And then we'll just check that the key is good.
It says API key found and it looks good so far.
And if yours doesn't say that, then you know what to do.
Check the EMV, check everything, and look in the look in the troubleshooting and ask me if you get
stuck.
Okay, let's talk about end points.
Now, I imagine most of you know exactly what an end point is.
It's one of those words people throw around all the time.
But you might not.
And for people that don't know about it, you should quickly find out.
I explain it all in the Technical Foundations guide.
There's nothing special at endpoint.
It's an http url which you can call to, to to to make an API request by hitting some web address.
And that web address would be known as an endpoint.
It's a way that you have an API.
But if things like API's and endpoints are new to you, then read that guide.
Okay.
There's an endpoint in particular which might interest you, which I want to show you right now.
And it's an endpoint, an API endpoint offered by open AI.
So in order to call this endpoint, which I will just do, we first have to set a couple of things which
are very common for any HTTP request.
There's HTTP headers, the headers that go down in that request.
You normally you probably if you know anything about web requests.
You specify what content type, what type of thing you want to come back.
And that's a way of saying, I want Jason to come back, please.
And then this is a fairly standard way of, of, of sending down a secret in an HTTP message that authorizes
you, you put in a header called authorization and the value of that header, there's the word bearer
and then a space and then some secret that identifies you to the third party.
And in this case we are going to stuff the OpenAI API key.
The thing that I got just here, we're going to stuff that into the header.
And then I've got something here called payload.
Payload is just a chunk of JSON.
And it's going to be a dictionary a dictionary with two keys.
One of them is model, which is going to be GPT five nano, the tiny version of GPT five.
The latest model, the one of the strongest on the planet right now.
And then the second field in this dictionary has a key of messages.
The value is, you know, this is a list of dictionaries.
It's a list you can see there.
It is a list.
Each dictionary, let's put that on another line, has a key role with value user a key content.
And the value is tell me a fun fact okay.
Nothing special here.
Let's look at this.
Uh, there is the payload JSON.
It's is just what I said.
Model GPT five nano messages, a list of dictionaries, role user content.
Tell me a fun fact.
Okay, that's a chunk of JSON.
Uh, headers and payload.
What we're now going to do is we're going to send that to an endpoint.
This is the endpoint API.
V1.
It's chat completions.
And sometimes when you make a Post request people think of that as like creating a new resource for
people that are like rest, rest people.
Uh, that's that's sometimes called creating a resource.
So you could think of this as being like chat completions create some familiar.
Uh, so chat completions create.
We're passing in our headers with our API key.
And the payload is this JSON blob right here.
So let's run that.
And what comes back is a bunch of JSON.
We asked for JSON back.
Let's see what we get back.
Let's see what kind of JSON we get.
Here it is.
Uh so it's a chunk of JSON as like an ID object, various stuff.
And then it has a field called choices and Choices.
In this response, choices is a list.
And the first item of that choices with index zero is something which has a field message.
And that message is itself a JSON dictionary.
And that has something called content, which is fun fact.
There are possible unique games, more possible unique games of chess than there are atoms in the observable
universe.
About ten to the power of 120 There is a fun fact.
And so that fun fact came back in the JSON response from our call to GPT five to that endpoint, that
URL that I just gave you.
And so there's obviously there's another way I could do this.
Let me get a new code thing here.
Let's say, uh, um, we said response JSON.
That was the JSON that we just were just looking at right up here.
Uh, I spelt response wrong.
That's that's what you get if you make me type Response.json.
So we could say, okay, let's look in the choices field.
I guess it knows what I'm doing here.
Look at the first element in there.
Element index zero at the field message at the field content okay.
You see that.
That's you know what that's going to do.
We're just going to choices the first one message content.
And let's see what that prints.
It prints that that very fun fact of course that we just looked at uh, the more possible unique games
of chess than there are atoms in the observable universe.
Okay, but you know what?
Uh, so so this is a perfectly good way to call OpenAI in the cloud using an HTTP endpoint.
And it's fine.
We could do this.
We could we could always type this.
But it is kind of messy fussing around with JSON, looking in dictionaries at keys and things like that.
It's kind of hokey, and it would be a real pain if every time we wanted to call GPT or any frontier
model, we had to stitch together these HTTP requests, call that slash chat completions with a post
or create and then be be sort of navigating our way through JSON objects like this.
It would be a pain.
It would be nice if there were a better way to do it.
Yes, yes, I know you get the joke.
You know what I'm gonna say?
There is a better way to do it.
And OpenAI created this better way.
They made a package called OpenAI, and that package is what's known as a Python client library.
Python client library is nothing fancy at all.
A Python client library that you often use for APIs all over the place for you.
Name it for APIs, for sending emails, for APIs to do so much.
Um, and these things are typically very lightweight libraries that manufacture an HTTP request to an
endpoint.
And with what comes back, it turns it into Python objects so that you're not messing around with this
kind of stuff with with these, uh, ploughing your way through JSON dictionaries.
But you can just write some nice, elegant Python code, and that is all the OpenAI library is.
It's a Python client library that wraps a call to an HTTP endpoint.
It's very simple.
It's completely open source.
You can open it, look at it, look at all the code.
It's perfectly simple.
Some people, the first time they come across this, think that when you're dealing with OpenAI, the
object and the code, that somehow we're sort of running GPT and we have some some fancy code from OpenAI.
Not at all.
It's vanilla code that just wraps making a web request.
And we'll we'll quickly use that code now, but it's going to be much more familiar to you.