From the course: Introduction to Large Language Models

Accessing large language models using an API

From the course: Introduction to Large Language Models

Accessing large language models using an API

- [Instructor] Now, in the previous video, we looked at comparing large language models using GitHub models. Now in this one we looked to see how we can access the large language models via an API using a programming language like Python. Now I prefer GitHub Codespaces for prototyping any apps. It's a cloud-based coding environment and you can access it from any device. And what I like about it is I don't waste any time with setting things up. I'm going to demonstrate the step-by-step process for setting up a GitHub repo and using Codespaces to work with GitHub models. You can also go directly to the course GitHub repo to see the configuration already in place. I've also included the coding files in the repo for you. So let's kick things off with creating a GitHub repo. So I'm going to head over to github.com, and this is my GitHub repo. And I'm going to head over to the top and select, create a new repository. And I'm going to call this repository joke. I'm going to leave it as public. And because this is going to be in Python, I'm going to add a Python .gitignore template. This is kind of standard to whatever I do for creating any new repository. And then I'm going to go ahead and select Create repository. So now that we've got the repository set up, let's head over to GitHub Codespaces. So I'm now at github.com/codespaces, and I'm going to go ahead and select a new codespace. And this is where I'm going to select the repository that I just created, and I can see it over here. So it's jonfernandes/joke. I'm going to leave everything else as is, and I'm going to go ahead and select Create codespace. And I'm just going to go ahead and ignore this walkthrough. And so I'm just going to go ahead and close this window. Now, in the previous video, we found that the GPT‑4o mini model was the better of the two at explaining jokes. So let's head back to GitHub Marketplace. So I'm going to head over to github.com/marketplace. I'm going to go ahead and select Models, and I'm going to go ahead and select the GPT‑4o mini model. So let me go ahead and enter the joke that needs to be explained. And let's send this off to the GPT‑4o mini model. And you can see that I've got the response back from the model. So now I'm going to head over to the Code tab. And what I'm going to want to do is I'm selecting Python as my language, and I want to go ahead and copy all of this code. And you'll notice that the first thing that you do here is to install OpenAI. So you need to install the OpenAI Python package. So I'm going to go ahead and copy this and head back to Codespaces. So let me go ahead and create a new file. And so I'm going to create this file and I'm going to call it joke.py, and let me paste the details that I picked up from the marketplace. And the first thing I'll need to do, as you can see over at the top, is to do a pip install openai. So pip install openai. Now I'm going to go ahead and install the OpenAI Python package. And this will allow me to access the OpenAI large language model via Python. Now let me just make a little bit of space for myself, and let me just bump the font up a little bit, and I'm going to go ahead and clear my screen. And now I'm going to go ahead and run this file. So python joke.py. And you can see that the output I get is the capital of France is Paris, because that's the response back from the user. So what I'll actually want as the output here or the input from the user is going to be explain the joke. So if I head back to the GitHub Marketplace and head back to the Chat, and I'm going to select explain this joke, I can then replace this content, which is the default that you'll get, with the explain this joke. And because I've got a couple of single and double quotes, I'm going to use triple quotes so that there's no confusion. And let me just go ahead and format this a little bit. And now if I go ahead and rerun this, I get the explanation of this joke from the large language model. So as you can see, I've now been able to access the GPT‑4o mini model using Python. All right, so in this video we've looked at how we can access large language models via an API using a programming language like Python.

Contents