- 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 openai
library.
Contents
- Introduction
- Listing OpenAI models through the API with Python
openai
Python Library- OpenAI API Authorization
- openai.Model.list()
- Full Python code so far
- How to serialize OpenAI API Data as Json
- Summary
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.
import openai
pip install openai
OpenAI API Authorization
api_key
function as openai.api_key
for that purpose. Check out 2 lines below:OPENAI_API_KEY = 'YOUR_OPENAI_KEY'
openai.api_key = OPENAI_API_KEY
openai.Model.list()
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 openai.Model.list()
.
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:
- created
- id
- object
- owned_by
- parent
- permission
- root
{
“created”: 1649358449,
“id”: “babbage”,
“object”: “model”,
“owned_by”: “openai”,
“parent”: null,
“permission”: [
{
“allow_create_engine”: false,
“allow_fine_tuning”: false,
“allow_logprobs”: true,
“allow_sampling”: true,
“allow_search_indices”: false,
“allow_view”: true,
“created”: 1669085501,
“group”: null,
“id”: “modelperm-49FUp5v084tBB49tC4z8LPH5”,
“is_blocking”: false,
“object”: “model_permission”,
“organization”: “*”
}
],
“root”: “babbage”
},
{
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.
babbage
davinci
gpt-3.5-turbo-0301
text-davinci-003
babbage-code-search-code
text-similarity-babbage-001
text-davinci-001
ada
curie-instruct-beta
babbage-code-search-text
babbage-similarity
gpt-3.5-turbo
code-davinci-002
code-search-babbage-text-001
text-embedding-ada-002
code-cushman-001
whisper-1
code-search-babbage-code-001
audio-transcribe-deprecated
text-ada-001
text-similarity-ada-001
text-davinci-insert-002
ada-code-search-code
ada-similarity
code-search-ada-text-001
text-search-ada-query-001
text-curie-001
text-davinci-edit-001
davinci-search-document
ada-code-search-text
text-search-ada-doc-001
code-davinci-edit-001
davinci-instruct-beta
text-similarity-curie-001
code-search-ada-code-001
ada-search-query
text-search-davinci-query-001
curie-search-query
davinci-search-query
text-davinci-insert-001
babbage-search-document
ada-search-document
text-search-curie-query-001
text-search-babbage-doc-001
text-davinci-002
curie-search-document
text-search-curie-doc-001
babbage-search-query
text-babbage-001
text-search-davinci-doc-001
text-search-babbage-query-001
curie-similarity
curie
text-similarity-davinci-001
davinci-similarity
cushman:2020-05-03
ada:2020-05-03
babbage:2020-05-03
curie:2020-05-03
davinci:2020-05-03
if-davinci-v2
if-curie-v2
if-davinci:3.0.0
davinci-if:3.0.0
davinci-instruct-beta:2.0.0
text-ada:001
text-davinci:001
text-curie:001
text-babbage:001
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))
str
function 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.Summary
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.