- Python Lesson: APIs with Python
- Python Exercise: APIs with Python
- Mars Weather API
- Dog Pics API
- Sunrise and Sunset API
- Name Generator API
- Space Launch Data API
- Earthquake Data API
- Mars Rover Images API
- Plant Info API
- Special Numbers API
- UK Police Data API
- Nasa Space Image API
- Cocktail Database API
- Official Jokes API
- Chuck Norris Jokes API
- Electric Vehicle Charge Map
- Listing Available OpenAI Models
- ChatGPT API Tutorial
- GPT-4 API Tutorial
- OpenAI Whisper API Tutorial
Listing Available OpenAI Models – OpenAI API
OpenAI models are quite hot topic right now. OpenAI has a very useful API structure which you can access with Python and leverage this edge-cutting modern technology.
In this tutorial, we will demonstrate a few examples of OpenAI’s API using Python and the
2) Listing OpenAI models through the API with Python
When I started using OpenAI’s API, I wanted to first list all of the models so I can be aware of different options out there. This was a great little exercise because you also get familiar with the authorization of your account for making API calls.
You can simply replace the string in the first line with your OpenAI API key.
openai Python Library
First step is importing the openai library which we will use to access OpenAI’s API. It can be imported simply as below.
pip install openai
OpenAI API Authorization
openai.api_keyfor that purpose. Check out 2 lines below:
OPENAI_API_KEY = 'YOUR_OPENAI_KEY' openai.api_key = OPENAI_API_KEY
Now that we imported the openai library and authorized our session with the API key, we can execute the actual function which will list all of the available OpenAI models for us.
For that purpose, we will use list function of Model class from the openai library as
model_lst = openai.Model.list()
This will return a big dictionary with two keys: “data” and “object”. If we look into the “data” key we will see all the models listed with various parameters.
If you are curious object key just shows the structure of the data information which is in this case “list” showing it’s a Python list. We will mainly explore “data” part of the returned object.
Below is 1st sample element from the data. Inside that list, we see a nested dictionary with various keys and another list as the value of the permission key.
The keys are:
As you can see there are quite a few technical parameters. However, “id” key gives the name of the model. We can use this information to list the names of all of the available models by OpenAI.
model_lst = openai.Model.list() for i in model_lst['data']: print(i['id'])
This code will return all of the AI models that are available by OpenAI’s API including the popular gpt-3.5 turbo.
Full Python code so far
import openai OPENAI_API_KEY = 'YOUR_OPENAI_KEY' openai.api_key = OPENAI_API_KEY model_lst = openai.Model.list() for i in model_lst['data']: print(i['id'])
I think another crucial step is to serialize this data, so we can save the data permanently and refer to it whenever needed instead of making separate API calls which will unnecessarily use resources and spend our API credits.
In the next session we will demonstrate how to save the returned object as a json file locally.
3) How to serialize OpenAI API Data as Json
You can save the OpenAI model list as json so you don’t have to make a request each time you need to refer to this data. This makes the information permanently written in your disk and it’s a technique called serialization.
Below, you can find a Python code that can be used to save our data in model_list.json file.
# Save OponAI model list as json with open('model_list.json', 'w') as modelsjson: modelsjson.write(str(model_lst))
strfunction to convert the outermost dictionary object to a string and save it to our json file. You can read more about this in our Data Type Conversions Python tutorial.
In this Python Tutorial we’ve learned how to access OpenAI’s powerful Rest API, how to authorize our API session and how to make calls to it. Particularly we demonstrated a Rest API call that returns a list of all of the available AI models of OpenAI.
We have also went over various fundamental Python topics such as nested data structures, data type conversion (in this case from dictionary to string), json structure and saving data as a json file,
If you don’t have the openai library for Python we’ve also discussed how it can be installed using pip package manager of Python. For further instructions on using pip, you can refer to: Installing pip packages with Python tutorial.