Open In App

OpenAI Python API – Complete Guide

Improve
Improve
Like Article
Like
Save
Share
Report

OpenAI is the leading company in the field of AI. With the public release of software like ChatGPT, DALL-E, GPT-3, and Whisper, the company has taken the entire AI industry by storm. Everyone has incorporated ChatGPT to do their work more efficiently and those who failed to do so have lost their jobs. The age of AI has started and people not adapting to AI could introduce some difficulties for them. 

In this article, we will be discussing how you can leverage the power of AI and make your day-to-day tasks a lot easier by using the OpenAI APIs (Application Programming Interface) that allow developers to easily access their AI models and Integrate them into their own applications using Python.

OpenAI-Python-Tutorial-.webp

What is OpenAI?

OpenAI is a Leading Company in the field of Artificial Intelligence(AI). It was originally founded in 2015 by Sam Altman and Elon Musk as a Non-profit Organization. They primarily focus on AI-based Software products Such as ChatGPT 3, ChatGPT 4 and DALL-E etc. They develop next-generation AI products holding incredible capabilities, for example, OpenAIs GPT-3 which is a Content filtering model that allows you to implement advanced text classification, outline, question-answering, and other chatbot applications.

To learn more about APIs, you can refer to this article.

What is OpenAI API?

OpenAI API is a powerful cloud-based platform, hosted on Microsoft’s Azure, designed to provide developers with seamless access to state-of-the-art, pre-trained artificial intelligence models. This API empowers developers to effortlessly integrate cutting-edge AI capabilities into their applications, regardless of the programming language they choose to work with. By leveraging the OpenAI Python API, developers can unlock advanced AI functionalities and enhance the intelligence and performance of their software solutions.

Generate OpenAI API key

For you to use OpenAI’s models in your Python environment, you must first generate an API key. You can follow the below steps to generate the API key:

Step 1: After creating an OpenAI account, log in.

Step 2: After login in, choose Personal from the top-right menu, then choose “View API keys,”.

Step 3: The button “Create new secret key” is present on the page that contains API keys once step 2 has been finished. Clicking on that generates a secret key, which you should copy and save somewhere else because it will be required in further steps.

Installation of OpenAI package

Step 1: Now open a text editor of your choosing or an online notebook like Google Colab or Jupyter Notebook. Here, we’re using a Google Colab notebook to run the command indicated below in order to install the Open AI library in Python.

!pip install -q openai

Step 2: Now import the OpenAI library in your Python environment and add your API key to the environment by executing the following lines of code in your text editor.

Python3




# importing openai module into your openai environment
import openai
  
# assigning API KEY to initialize openai environment
openai.api_key = '<API_KEY>'


Now you are all set to use OpenAI in your python environment. You can follow the below tutorials to try out OpenAI’s latest models.

Prompt Engineering

Giving the AI brain a unique set of instructions to increase its intelligence and responsiveness is what AI prompt engineering entails. To comprehend what we want from AI models like ChatGPT or GPT-4, they need to be gently nudged in the right direction. Prompt engineering can help with it. The finest answers from the AI may be ensured by carefully structuring the prompts. Now, prompt engineering doesn’t only happen once. The process of adjusting and experimenting is continuing. When we ask the AI a question, we experiment with varied wording and the addition of unique rules. We seem to be concocting a miraculous concoction of instructions! Let’s take a look at some rules to construct good prompts to generate accurate results for AI.

Some Rules of Prompt Engineering

Rule 1: Use latest AI models for your tasks

It is important to use latest AI models, because AI models do not tend to give accurate results when they are not updated with recent data. For example, ChatGPT was trained on data dated till September 2021 so it won’t be able to provide you with information after September 2021. Whereas, GPT-4 (or ChatGPT plus) is trained on latest data and is able to generate the latest information.

Rule 2: Start your prompt with Instructions, and use “”” or ### to separate your input from your instruction.

For example,

Translate the below text to Hindi.
Text: """
I work at Geeks for Geeks
"""

Rule 3: Be as precise, illustrative, and thorough as you can when describing the context, result, length, format, style, and other desired characteristics.

For example,

Write a short inspiring and funny poem about Geeks for Geeks, focusing on their recent launch of Full-stack development course in the style of Sandeep Jain.

Rule 4: Try including examples to illustrate the desired output format.

For example,

Create a list of datasets that can be used to train a logistic regression model. 
Ensure that the datasets are available in CSV format. The objective is to use this dataset to learn about Supervised Learning. 
Also, provide links to the dataset if possible.
Desired Output format:
Dataset name | URL | dataset description
             |     |

Rule 5: Start with zero-shot learning, then few-shot learning. If neither of them worked, then fine-tune.

Zero-shot: The ability to complete a task without being trained for it.

Few-shot: The ability to complete a task after being trained with a limited number of examples.

Fine-tune: By fine-tuning GPT-3 using training data, you can increase its performance on particular tasks. When the model is being fine-tuned, it reads text tokens from the training set and predicts which token will come next. The model’s internal weights are updated when it predicts incorrectly to increase the likelihood that it will forecast correctly the following time. The model will eventually learn to generate the pattern of tokens shown in your training data after receiving sufficient training.

For example,

Zero-Shot:

Find the odd one out from the following:
Text: """Fish, Octopus, Crab, Lion"""
Answer: 

Few-Shot:

Find the odd one out from the following:
Text: """Fish, Octopus, Crab, Lion"""
Answer: Lion
Text: """Ostrich, Cheetah, Turtle"""
Answer: Turtle
Text: """square, rectangle, rhombus, circle"""
Answer:

Fine-Tune: You can see the best practices for fine-tuning here.

Rule 6: Don’t add imprecise descriptions to your prompt

For example,

Write a paragraph of 3 to 5 lines explaining apple.

Rule 7: Instead of mentioning “what not to do”, try including “what to do” to your prompt

For example,

Write a conversation between a father and a son, the conversation should be about future goals and refrain from involving any personal information. 
Instead of involving personal information, try directing the conversation towards college.

Rule 8: Specifically for code generation: Use “leading words” to guide the model towards a certain pattern.

For example,

# Write a simple python function that
# 1. Ask me for an integer number 
# 2. Generate a fibonacci series till the input number using top-down approach
import

The “import” instruction tells the model in the code snippet below to begin writing in Python. (Likewise, “SELECT” is a good starting point for a SQL statement.)

You can use these rules to design some prompts and execute it on ChatGPT. You can see how different the results are from your regular prompts.

Text

For performing any text-specific tasks you can define the following function and execute it with your desired prompts.

Python3




# function that takes in string argument as parameter
def comp(PROMPT, MaxToken=50, outputs=3):
    # using OpenAI's Completion module that helps execute 
    # any tasks involving text 
    response = openai.Completion.create(
        # model name used here is text-davinci-003
        # there are many other models available under the 
        # umbrella of GPT-3
        model="text-davinci-003",
        # passing the user input 
        prompt=PROMPT,
        # generated output can have "max_tokens" number of tokens 
        max_tokens=MaxToken,
        # number of outputs generated in one call
        n=outputs
    )
    # creating a list to store all the outputs
    output = list()
    for k in response['choices']:
        output.append(k['text'].strip())
    return output


Here we generated text for the user prompt using the Completions module from the OpenAI library. These are the crucial variables related to the Completions module:

  • model [required]: The following openai command can be used to determine the ID of the model to openai.Model.list().data with the model name as the value of the ‘id’ field. We must choose a model that will work for us.
  • prompt: The prompt(s) to generate completions for, encoded as a string, array of strings, array of tokens, or array of token arrays. Note that <|endoftext|> is the document separator that the model sees during training, so if a prompt is not specified the model will generate as if from the beginning of a new document.
  • max_tokens: The maximum number of tokens that the completion will generate. 16 is the default value for the parameter.
  • temperature: The range of the sampling temperature is 0 to 2. In contrast to lower values like 0.2, higher values like 0.8 will result in a more focused and deterministic output.
  • n: The number of answers to produce for each prompt.

Example prompts for Text Generation

Now let’s try some prompts for Text Generation using Completions module(Chat GPT with Python).

Prompt 1:

Python3




PROMPT = """Write a story to inspire greatness, take the antagonist as a Rabbit and protagnist as turtle. 
Let antagonist and protagnist compete against each other for a common goal. 
Story should atmost have 3 lines."""
comp(PROMPT, MaxToken=3000, outputs=3)


Output:

['Once upon a time, there lived a Rabbit and a Turtle. Both wanted to be the fastest, so they decided to have a race. 
The Turtle, despite being slow, never gave up and came first, surprising everyone, leaving the Rabbit behind.',
 'Once upon a time, a rabbit and a turtle competed against each other for a goal. 
 Both wanted to cross the same track in the shortest amount of time possible. 
 By the end, the turtle had proved that no matter how fast or slow one may seem, true greatness is in the persistence to never give up.',
 'Once upon a time, a Rabbit and Turtle were racing to the finish line for an important goal. 
 While the Rabbit was fast, the Turtle was determined and never gave up. In the end, the Turtle proved that slow and steady wins the race.']

Prompt 2:

Python3




PROMPT = """Write a short conversation between client and businessman about a wine bottle purchase. 
Client is not happy with the purchase and the businessman is not accepting his mistake.
Make the conversation sarcastic. 
Each Response should have atmost 2 lines.
The client should talk like Kevin Hart and businessman should  talk like Shakespeare.
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['Client: "Oh taste and see that this wine I purchased is foul!"\nBusinessman: "Verily, mine eyes see naught of thine discontent." \nClient: "Man, what I\'m sayin\' is this is some trash wine!" \nBusinessman: "Careful thy words, the wisdom in silence is stout."']

Example prompts for Text Completion

Now let’s try some prompts for Text Completion using Completions module.

Prompt 1:

Python3




PROMPT = """Complete the below conversation between a client and a worker.
Make the conversation have a wholesome plot twist.
Conversation : ###
Client: I want a water bottle.
Worker: I don't have any water botlles.
Client: But I want water bottles.
Worker:
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['Well, what about a cup? A lot of our customers use cups to drink water and it can be a great alternative to a water bottle!']

Prompt 2:

Python3




PROMPT = """
Complete the below story.
Story should focus on a moral value like Greed, Wrath, Pride, Arrogance, Sloth or Envy.
  
Story:###
Once upon a time, there was a Greek King, Midas.
He was very rich and had lots of Gold. He had a daughter, who he loved a lot.
One day, Midas found an angel in need of help. He helped her and in return she agreed to grant a wish. 
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['Midas, blinded by his greed, wished for anything he touched to turn to gold. \nThe angel granted his wish and Midas went to try it out on his daughter. As soon as he touched her, she immediately turned to gold. He was filled with horror and shame, realizing that his greed had robbed him of his daughter. \nHe pleaded for the angel to reverse the spell, and she reluctantly agreed. Midas learned his lesson well; never let greed overtake judgment and reason.']

Example prompts for Translation

Now let’s try some prompts for Translation using Completions module.

Prompt 1:

Python3




PROMPT = """
Translate the below text in Japanese and Hindi.
Text:###
Geeks for Geeks Data Structures Course is the best.
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['Japanese:\nジークス・フォー・ジークスのデータ構造コースが最高です。\n\nHindi:\nजीक्स फॉर जीक्स डेटा स्ट्रक्चर्स कोर्स सबसे अच्छा है।']

Prompt 2:

Python3




PROMPT = """
Translate the below text in German and French.
Text:###
Hey I am Geeks for Geeks chat bot.
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['German:\nHallo, ich bin Geeks for Geeks Chatbot.\n\nFrench:\nSalut, je suis le bot de chat Geeks for Geeks.']

Example prompts for Summarization

Now let’s try some prompts for Summarization using Completions module.

Prompt 1:

Python3




PROMPT = """
Summarize the below text and extract the key points.
Text:###
This is an extremely popular story about a hare and a tortoise.
The hare is an animal that is known to move quickly, while a tortoise is one to move slowly.
One day, the hare challenged the tortoise to a race simply to prove that he was the best. The tortoise agreed.
Once the race began the hare was easily able to get a head start. Upon realizing that the tortoise is far behind. The overconfident hare decided to take a nap.
Meanwhile the tortoise, who was extremely determined and dedicated to the race was slowly nearing the finish line.
The tortoise won the race while the hare napped. Most importantly he did it with humility and without arrogance.
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

["The well-known story of the hare and the tortoise tells of the hare's overconfidence and the tortoise's determination and dedication which saw the tortoise win the race. The hare had gotten a head start but when he realized that the tortoise was behind, he took a nap which allowed the tortoise to reach the finish line first. The tortoise emerged victorious as the moral of the story is that hard work pays off and should be done with humility."]

Prompt 2:

Python3




PROMPT = """
Summarize the below text in bullet points.
Text:###
A farmer asked his son to take their herd of sheep grazing every day.
While the boy watched over the sheep, he got bored and decided to have some fun.
So, he shouted, “Wolf! Wolf!”. Upon hearing this the villagers ran to help him chase the Wolf away.
As they reached him, they realized that there was no Wolf and he was just kidding. 
The villagers were furious and they yelled at the boy for creating chaos and panic.
On the next day and the boy shouted “Wolf!” again and once again the villagers came to help him and saw that there was no wolf. 
This made them very angry again.
On the same day, the boy saw an actual Wolf that has terrorizing the sheep. 
The boy cried “Wolf! Wolf! please help me” and no villagers showed up as they believed that the boy was joking again.
###
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['- Farmer asked son to take herd of sheep grazing\n- Boy got bored and started shouting “Wolf! Wolf!” to which the villagers ran to help him\n- Villagers realized there was no wolf and were furious\n- Boy shouted again the next day and villagers again realized there was no wolf, making them extremely angry\n- On same day, boy spotted an actual Wolf, but this time the villagers did not come to help as they thought he was playing a joke again']

Example prompts for Retrieving factual information

Now let’s try some prompts for Retrieving factual information using Completions module.

Prompt 1:

Python3




PROMPT = """
What all states and union teritories are in India?
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['India is divided up into 28 different states and 8 union territories: \n\nStates: Andhra Pradesh, Arunachal Pradesh, Assam, Bihar, Chhattisgarh, Goa, Gujarat, Haryana, Himachal Pradesh, Jammu & Kashmir, Jharkhand, Karnataka, Kerala, Madhya Pradesh, Maharashtra, Manipur, Meghalaya, Mizoram, Nagaland, Odisha, Punjab, Rajasthan, Sikkim, Tamil Nadu, Telangana, Tripura, Uttar Pradesh, Uttarakhand, and West Bengal. \n\nUnion Territories: Andaman & Nicobar Islands, Chandigarh, Dadra & Nagar Haveli, Daman & Diu, Delhi, Lakshadweep, Ladakh, Puducherry.']

Prompt 2:

Python3




PROMPT = """
Who is the highest paid actor in the world?
"""
comp(PROMPT, MaxToken=3000, outputs=1)


Output:

['According to Forbes, the highest paid actor in the world as of 2021 is Dwayne Johnson, with an estimated 2020 earnings of $87.5 million.']

Example prompts for Text Conversion

Now let’s try some prompts for Text Conversion using Completions module.

Prompt 1:

Python3




PROMPT = """
Convert the below movie titles in emojis
Movies List:###
1. Wizard of Oz
2. The imitation game
3. Ghosted
###
"""
print(comp(PROMPT, MaxToken=3000, outputs=1)[0])


Output:

1. ????‍♂️????
2. ⏳????
3. ????????

Prompt 2:

Python3




PROMPT = """
Encode the below texts in only special characters
Text:###
1. Geeks
2. for
3. Geeks
###
"""
print(comp(PROMPT, MaxToken=3000, outputs=1)[0])


Output:

1. Ɣ€€Ƙ§
2. Ƒöř
3. Ɣ€€Ƙ§

Chat

For performing any chat specific tasks you can define the following function and execute it with your desired prompts.

Python3




# function that takes in string argument as parameter
def chat(MSGS, MaxToken=50, outputs=3):
    # We use the Chat Completion endpoint for chat like inputs
    response = openai.ChatCompletion.create(
    # model used here is ChatGPT
    # You can use all these models for this endpoint: 
    # gpt-4, gpt-4-0314, gpt-4-32k, gpt-4-32k-0314, 
    # gpt-3.5-turbo, gpt-3.5-turbo-0301
    model="gpt-3.5-turbo",
    messages=MSGS,
    # max_tokens generated by the AI model
    # maximu value can be 4096 tokens for "gpt-3.5-turbo" 
    max_tokens = MaxToken,
    # number of output variations to be generated by AI model
    n = outputs,
    )
    return response.choices[0].message
  
# Messages must consist of a collection of message objects, 
# each of which includes a role (either "system," "user," or "assistant") 
# and a content field for the message's actual text. 
# Conversations might last only 1 message or span several pages.
MSGS = [
        {"role": "system", "content": "<message generated by system>"},
        {"role": "user", "content": "<message generated by user>"},
        {"role": "assistant", "content": "<message generated by assistant>"}
    ]


Here, we have used the Chat Completion module from OpenAI library to execute chat specific tasks using ChatGPT model. Here are the important parameters involved with Chat Completion module:

  • model [required]: ID of the appropriate model. For information on which models are compatible with the Chat API, go to the model endpoint compatibility table (https://platform.openai.com/docs/models/model-endpoint-compatibility).
  • message: A chronological list of the conversation’s messages. The list of objects must contain dictionary with the following parameters only:
    • Role: The role of the author of this message. It can be either of “system”, “user”, or “assistant”.
    • Content: The contents of the message.
  • max_tokens: The maximum number of tokens to generate in the completion. The default value is 16.
  • temperature: Sampling temperature ranges between 0 and 2. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic.
  • n: number of completions to generate for each prompt.

Examples for Chat Completion

Now Let’s see some examples to gain a better understanding of the Chat Completion API

Prompt 1:

Python3




ch = [
        {"role": "user", "content": "When did India win the world cup"}
    ]
  
chat(ch, MaxToken=500, outputs=1)


Output:

<OpenAIObject at 0x7fb078845710> JSON: {
  "content": "India has won the Cricket World Cup twice. The first time was in 1983 and the second time was in 2011.",
  "role": "assistant"
}

Prompt 2:

Python3




ch = [
        {"role": "user", "content": "Do you live with Snow White?"},
      {"role": "assistant", "content": "No, I live with Willy Wonka in the chocoalte factory."},
      {"role": "user", "content": "Can you tell me some good chocolate names?"}
  
    ]
  
chat(ch, MaxToken=500, outputs=1)


Output:

<OpenAIObject at 0x7fb078847c90> JSON: {
  "content": "Sure, here are some popular chocolate names:\n\n1. Lindt\n2. Ghirardelli\n3. Ferrero Rocher\n4. Toblerone\n5. Godiva\n6. Hershey's\n7. Cadbury\n8. Nestle\n9. Lindor \n10. Milka",
  "role": "assistant"
}

Image

We can perform Image generation and Image editing using DALL-E model of OpenAI. Before we begin, let’s import some image-processing libraries.

Python3




# importing other libraries
import requests
from PIL import Image
from io import BytesIO


Now we construct a function to produce an image using the DALL E API’s “create” endpoint.

Python3




# function for text-to-image generation 
# using create endpoint of DALL-E API
# function takes in a string argument
def generate(text):
  res = openai.Image.create(
    # text describing the generated image
    prompt=text,
    # number of images to generate 
    n=1,
    # size of each generated image
    size="256x256",
  )
  # returning the URL of one image as 
  # we are generating only one image
  return res["data"][0]["url"]


An argument string is passed to the API endpoint by the aforementioned function. Other parameters are n (the “number of images generated using that prompt”) and size (the “size of the image generated”). Either Base64 or a URL can be used by the API to generate an image. As output, API provides the created image’s URL.

Note: The size of the generated images must be one of 256×256, 512×512, or 1024×1024.

Examples for Image Generation

Now Let’s see some examples to gain a better understanding of Image Generation using DALL-E

Prompt 1:

Python3




# prompt describing the desired image
text = "batman art in red and blue color"
# calling the custom function "generate"
# saving the output in "url1"
url1 = generate(text)
# using requests library to get the image in bytes
response = requests.get(url1, stream=True)
# using the Image module from PIL library to view the image
Image.open(response.raw)


Output:

Image Generation using OpenAI

batman art in red and blue color

Prompt 2:

Python3




# text prompt describing my desired image
text = "a scenic view of moon shining light on a yacht"
# generate function uses DALL-E API to generate image
# it returns a temporary URL of the image
url1 = generate(text)
# We use the requests library to fetch the image from URL
response = requests.get(url1, stream=True)
# We use the Image Class from PIL library to open the image
Image.open(response.raw)


Output:

Screenshot-2023-06-02-105118.png

A scenic view of moon shining light on a yacht

Examples for Image Editing

For editing the image we use the create edit endpoint of the DALL-E API. In this section, a mask will be uploaded and a text prompt will be supplied in order to change an image. Where the image should be altered is indicated by the transparent portions of the mask, and the prompt should describe the entire new image rather than just the area that was erased.

Make sure your image and mask are of the same size (square PNG) and less than 4MB in size before passing them as arguments to API. For this we use the below code.

Python3




response = requests.get(url1)
# saving the image in PNG format
with open("img.png", "wb") as f:
  f.write(response.content)
# opening the saved image and converting it into "RGBA" format
# converted image is saved in result
result = Image.open('img.png').convert('RGBA')
# saving the new image in PNG format
result.save('img_rgba.png','PNG')


Also, write a prompt such that it describes the full new image not just the transparent area that needs to be replaced. Use the following lines of code to edit the image.

Let’s see the example to gain a better understanding of Image Editing using DALL-E.

Image 1:

We will be using the below image as input:

Input Image for Create Edit endpoint

Below is the code for editing the image and generating 3 images as output.

Python3




# using create_edit endpoint of the DALL - E API
response = openai.Image.create_edit(
  # opening original image in read mode
  image=open("img_rgba.png", "rb"),
  # opening mask image in read mode 
  mask=open("mask.png", "rb"),
  # text prompt describing the new image
  prompt="gotham city skyline behind batman",
  # number of images to be generated
  n=1,
  #size of each image generated in pixels
  size="256x256"
)
  
# saving the URLs of all image in new variable "res"
res = response['data']
  
# loop to save and display images
for i in range(len(res)):
  # saving URL of image in res
  image_url = res[i]['url']
  # extracting image from URL in bytes form
  response = requests.get(image_url, stream=True)
  # opening the image
  k = Image.open(response.raw)
  # displaying the image
  k.show()
  # saving the image
  with open(f"img_mask_edit_{i}.png", "wb") as f:
    f.write(response.content)


Output:

Audio

There are 2 modules available for Whisper module:

1. Transcribe: This module transcribes your audio file into the input language. Model parameters for this module are:

  • file [required]: The audio file to transcribe, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
  • model [required]: ID of the model to use. Only whisper-1 is currently available.
  • prompt [optional]: An optional text to guide the model’s style or continue a previous audio segment. The prompt should match the audio language.
  • response_format [optional]: The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
  • temperature [optional]: The sampling temperature, between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.
  • language [optional]: The language of the input audio. Supplying the input language in ISO-639-1 format will improve accuracy and latency.
# opening the audio file in read mode
audio_file = open("FILE LOCATION", "rb")
# calling the module using this line and passing the model name and audio file
# there is only one model available for speech-to-text conversion
transcript = openai.Audio.transcribe(file="audio file", model="whisper-1")
transcript

2. Translate: This module translates your audio file into English language. Model parameters for this module are:

  • file [required]: The audio file to translate, in one of these formats: mp3, mp4, mpeg, mpga, m4a, wav, or webm.
  • model [required]: Model name which you wish to use. Only whisper-1 is currently available.
  • prompt [optional]: An optional text to guide the model’s style or continue a previous audio segment. The prompt should be in English.
  • response_format [optional]: The format of the transcript output, in one of these options: json, text, srt, verbose_json, or vtt.
  • temperature [optional]: The sampling temperature, is between 0 and 1. Higher values like 0.8 will make the output more random, while lower values like 0.2 will make it more focused and deterministic. If set to 0, the model will use log probability to automatically increase the temperature until certain thresholds are hit.
# opening the audio file in read mode
audio_file = open("FILE LOCATION", "rb")
# calling the module using this line and passing the model name and audio file
# there is only one model available for speech-to-text conversion
transcript = openai.Audio.translate(file="audio file", model="whisper-1")
transcript

Note: Audio file size should not be larger then 25 MB. If the file size is greater than 25 MB then you should break the file into smaller chunks.

Examples for Audio Transcribing

Audio we will be using for trying out Transcribe module:

You can download the audio file from here.

We will execute the below code for transcribing this audio.

Python3




# opening the audio file in read mode
audio_file = open("y2mate.com - Best Language for DSA GeeksforGeeks.mp3", "rb")
# transcribing the audio file using Whisper-1 model
transcript = openai.Audio.transcribe("whisper-1", audio_file)
# printing the transcript
transcript['text']


Output:

Which language is best for learning DSA? There are different types of people. If you are a patient programmer who is a patient person, with a lot of patients, then probably Java is the best language. Because in Java you can't write bad code and Python would even be better than Java. But if you really want to quickly compile and run and see the output of the program, C++ is good. So if we talk about the software industry, most of the places they use Java. And some of the places have been shifted to Python or shifting to Python. But still most of the industry runs on Java.

Examples for Audio Translation

Audio we will be using for trying out Translate module:

You can download the audio file from here.

We will execute the below code for translating this audio.

Python3




# opening the audio file in read mode
audio_file= open("audio1_gfg.mp3", "rb")
# the input audio is in hindi language
# translate module translates the input audio in English language
transcript = openai.Audio.translate("whisper-1", audio_file)
# print the translated transcript
transcript['text']


Output:

Prompt engineering is a word that you must have heard somewhere. But do you know what is its exact use? And where is it used exactly in the software industry? If not, then let's know. Number 1, Rapid innovation. So any company wants to develop and deploy its new product as soon as possible. And give new services to its customers as soon as possible. So that it remains competitive in its entire tech market. So here prompt engineering comes in a lot of use. Number 2 is cost saving. So prompt engineering allows any company to save its total time and cost. Apart from this, the entire development process streamlines it. Due to which the time to develop the product is reduced and its cost is reduced. Number 3 is demand for automation. So whatever you see in your environment today, everyone wants their entire process to be automated. And prompt engineering allows this. It allows to make such systems that totally automate the process that is going on in your company. So now you know the importance of prompt engineering. If you know more important things than this, then quickly comment below.

Embeddings

The embeddings module is used to obtain a vector representation of a given input that machine learning models and algorithms can quickly consume. We will write a function to generate these vectors using OpenAI API. Our API employs the “text-embedding-ada-002” model, a member of OpenAI’s second generation of embedding models. This model produces embeddings that are 1536 bytes long.

Python3




# function to generate vector for a string
def get_embedding(text, model="text-embedding-ada-002"):
   return openai.Embedding.create(input = , model=model)['data'][0]['embedding']


Let’s try this out on some sample text.

Prompt 1:

Python3




PROMPT = "This is Geeks for Geeks office. How can I help you?"
get_embedding(PROMPT)


Output:

[0.002064391039311886,
 -0.006849951110780239,
 -0.0022784520406275988,
 -0.034846529364585876,
 -0.010696562007069588,
 0.01778651960194111,
 -0.0037395802792161703,
 -0.009561389684677124,
 -0.0055493684485554695,
 -0.006862924434244633,
 -0.02698465622961521,
 -0.015438336879014969,
 0.008653252385556698,
 -0.010417633689939976,
 ...]

As we can see, we have obtained a vector generated by OpenAI Embeddings module. You can use this vector with any machine learning or deep learning algorithm.

Fine-Tuning

Open AI allows developers to fine-tune their models for specific purposes using their fine-tunes API endpoint. Below listed are the models you can fine-tune using this API:

  • davinci
  • curie
  • babbage
  • ada

For fine-tuning a model, you first need to make sure that your dataset is in JSONL format and if you are working on free trial, your training bill should not exceed 5 dollars. To prepare your dataset, you can execute the below command in your jupyter notebook:

!openai tools fine_tunes.prepare_data -f <dataset location>

With the exception of a prompt and a completion column/key, this tool accepts a variety of forms. You can instruct it to store the output into a JSONL file suitable for fine-tuning by passing it a CSV, TSV, XLSX, JSON, or JSONL file. It will walk you through the process of proposed adjustments.

Now, you can fine-tune a model by executing the below code in python environment:

Python3




# FineTune Endpoint of OpenAI is used to fine-tune models
# for a specific task
res = openai.FineTune.create("training_file"="<training-file-id>",
    "validation_file"="<validation-file-id>",
    "model"="<model-name>",
    "n_epochs"="<number of epochs/iterations>[integer]",
    "batch_size"="<batch size for model to train on>[integer]",
    "learning_rate_multiplier"="""<The pretraining learning rate multiplied by this 
    number yields the fine-tuning learning rate.>[integer]""")
# storing the fine-tuning Job ID 
jobID = res["id"]
# storing the status of fine-tuning
# initially it will show pending that means your task is in the queue
status = res["status"]
  
print(f'Fine-tunning model with jobID: {jobID}.')
print(f"Training Response: {res}")
print(f"Training Status: {status}")


Example of Fine-tuning

We will be taking the first 100 rows of Spam Classification dataset here. You can download the dataset from here.

Note: We are using only 100 rows here because the training cost for further rows was not supported in the free trial.

Step 1: First prepare the dataset using OpenAI’s in-built function.

!openai tools fine_tunes.prepare_data -f "spam.csv"

Output:

Analyzing...
- Based on your file extension, your file is formatted as a CSV file
- Your file contains 100 prompt-completion pairs
- The `prompt` column/key should be lowercase
- The `completion` column/key should be lowercase
- The input file should contain exactly two columns/keys per row. Additional columns/keys present are: ['Unnamed: 0']
  WARNING: Some of the additional columns/keys contain `Unnamed: 0` in their name. These will be ignored, and the column/key `Unnamed: 0` will be used instead. This could also result from a duplicate column/key in the provided file.
- Based on your data it seems like you're trying to fine-tune a model for classification
- For classification, we recommend you try one of the faster and cheaper models, such as `ada`
- For classification, you can estimate the expected model performance by keeping a held out dataset, which is not used for training
- Your data does not contain a common separator at the end of your prompts. Having a separator string appended to the end of the prompt makes it clearer to the fine-tuned model where the completion should begin. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more detail and examples. If you intend to do open-ended generation, then you should leave the prompts empty
- The completion should start with a whitespace character (` `). This tends to produce better results due to the tokenization we use. See https://platform.openai.com/docs/guides/fine-tuning/preparing-your-dataset for more details
Based on the analysis we will perform the following actions:
- [Necessary] Your format `CSV` will be converted to `JSONL`
- [Necessary] Lower case column name to `prompt`
- [Necessary] Lower case column name to `completion`
- [Necessary] Remove additional columns/keys: ['Unnamed: 0']
- [Recommended] Add a suffix separator ` ->` to all prompts [Y/n]: n
- [Recommended] Add a whitespace character to the beginning of the completion [Y/n]: n
- [Recommended] Would you like to split into training and validation set? [Y/n]: y
Your data will be written to a new JSONL file. Proceed [Y/n]: y
Wrote modified files to `/content/clean_spam_prepared_train (1).jsonl` and `/content/clean_spam_prepared_valid (1).jsonl`
Feel free to take a look!
Now use that file when fine-tuning:
> openai api fine_tunes.create -t "/content/clean_spam_prepared_train (1).jsonl" -v "/content/clean_spam_prepared_valid (1).jsonl" --compute_classification_metrics --classification_positive_class "ham"
 Make sure to include `stop=["am"]` so that the generated texts ends at the expected place.
Once your model starts training, it'll approximately take 4.73 minutes to train a `curie` model, and less for `ada` and `babbage`. Queue will approximately take half an hour per job ahead of you.

Step 2: Upload your training and validation dataset to OpenAI using the following lines of code in python

Python3




# uploading the training dataset
openai.File.create(
  file=open("spam_prepared_train.jsonl", "rb"),
  purpose='fine-tune'
)


Output:

<File file id=file-jTiNyDdGWLAvjP3RPN6RqumP at 0x7fefd161f150> JSON: {
  "bytes": 9822,
  "created_at": 1685693212,
  "filename": "file",
  "id": "file-jTiNyDdGWLAvjP3RPN6RqumP",
  "object": "file",
  "purpose": "fine-tune",
  "status": "uploaded",
  "status_details": null
}

Python3




# uploading the validation dataset
openai.File.create(
  file=open("spam_prepared_valid.jsonl", "rb"),
  purpose='fine-tune'
)


Output:

<File file id=file-u6qLHlRivL52CuEj7qnnREpS at 0x7fefd0d6ba10> JSON: {
  "bytes": 2211,
  "created_at": 1685693224,
  "filename": "file",
  "id": "file-u6qLHlRivL52CuEj7qnnREpS",
  "object": "file",
  "purpose": "fine-tune",
  "status": "uploaded",
  "status_details": null
}

Note: The Id in the outputs are to be passed in the next step of fine-tuning, so do save it.

Step 3: Now we will fine-tune Davinci model.

Python3




# call the FineTune API endpoint
res = openai.FineTune.create(
  # training file ID
  "training_file" ="file-jTiNyDdGWLAvjP3RPN6RqumP",
    # validation file ID
  "validation_file"="file-u6qLHlRivL52CuEj7qnnREpS",
    # model ID
  "model"= "davinci",
    # number of epochs
  "n_epochs" =10,
  # batch size to process
    "batch_size"= 4,
  # learning rate multiplier
    "learning_rate_multiplier"= 0.133)
  
# storing the job_id of the process
jobID = res["id"]
# storing the status of the process
status = res["status"]
  
print(f'Fine-tunning model with jobID: {jobID}.')
print(f"Training Response: {res}")
print(f"Training Status: {status}")


Output:

Fine-tunning model with jobID: ft-nPNCod70qnSWBXjnQ5kbZXK2.
Training Response: {
  "created_at": 1685693249,
  "events": [
    {
      "created_at": 1685693249,
      "level": "info",
      "message": "Created fine-tune: ft-nPNCod70qnSWBXjnQ5kbZXK2",
      "object": "fine-tune-event"
    }
  ],
  "fine_tuned_model": null,
  "hyperparams": {
    "batch_size": 4,
    "learning_rate_multiplier": 0.133,
    "n_epochs": 10,
    "prompt_loss_weight": 0.01
  },
  "id": "ft-nPNCod70qnSWBXjnQ5kbZXK2",
  "model": "davinci",
  "object": "fine-tune",
  "organization_id": "org-Sb3Fliwt5e4Qvq5kjbBtmpd8",
  "result_files": [],
  "status": "pending",
  "training_files": [
    {
      "bytes": 9822,
      "created_at": 1685693212,
      "filename": "file",
      "id": "file-jTiNyDdGWLAvjP3RPN6RqumP",
      "object": "file",
      "purpose": "fine-tune",
      "status": "processed",
      "status_details": null
    }
  ],
  "updated_at": 1685693249,
  "validation_files": [
    {
      "bytes": 2211,
      "created_at": 1685693224,
      "filename": "file",
      "id": "file-u6qLHlRivL52CuEj7qnnREpS",
      "object": "file",
      "purpose": "fine-tune",
      "status": "processed",
      "status_details": null
    }
  ]
}
Training Status: pending

As you can see the training status is pending, to know the status of your fine-tuning task you can execute the below code in your jupyter notebook.

Python3




import time
# retrieve the status of fine-tuning from OpenAI
status = openai.FineTune.retrieve(id=jobID)["status"]
# check if status is not "succeeded" or "failed" 
if status not in ["succeeded", "failed"]:
  print(f'Job not in terminal status: {status}. Waiting.')
  # loop keeps checking for the status
  while status not in ["succeeded", "failed"]:
    time.sleep(2)
    status = openai.FineTune.retrieve(id=jobID)["status"]
    print(f'Status: {status}')
# if status is "succeeded" or "failed" then we print the status
else:
  print(f'Finetune job {jobID} finished with status: {status}')
# also print all the fine-tuning jobs
print('Checking other finetune jobs in the subscription.')
result = openai.FineTune.list()
print(f'Found {len(result.data)} finetune jobs.')


Output:

Finetune job ft-nPNCod70qnSWBXjnQ5kbZXK2 finished with status: succeeded
Checking other finetune jobs in the subscription.
Found 2 finetune jobs.

Once the fine-tuning process is finished, move to the next step.

Step 4: Retrieve the model

By executing the below code, you can retrieve the model and save it in a variable.

Python3




result = openai.FineTune.retrieve(id=jobID)
fine_tuned_model  = result.fine_tuned_model
print(fine_tuned_model)


Output:

davinci:ft-personal-2023-06-02-08-17-42

Step 5: Use your custom model with any API endpoint.

We are using the Completion API endpoint of OpenAI here to check whether a message is spam or not.

Python3




# text prompt
new_prompt = f"""Classify the following message as spam or not spam:\n\n###Congratulations! 
You've Won a $1000 gift card from walmart. 
Go to https://bit.ly to claim your reward.###\n\nAnswer:"""
# calling the Completion API
answer = openai.Completion.create(
  # model ID collected from previous step is supposed to be passed here
  model=fine_tuned_model,
  # text input is passed here
  prompt=new_prompt
)
# displaying the result
print(answer['choices'][0]['text'])


Output:

Spam

API Error Codes

This guide includes an overview on error codes you might see from both the API and our official Python library. Each error code mentioned in the overview has a dedicated section with further guidance.

API errors

401 – Invalid Authentication

Cause: Invalid Authentication
Solution: Ensure the correct API key and requesting organization are being used.

401 – Incorrect API key provided

Cause: The requesting API key is not correct.
Solution: Ensure the API key used is correct, clear your browser cache, or generate a new one.

401 – You must be a member of an organization to use the API

Cause: Your account is not part of an organization.
Solution: Contact us to get added to a new organization or ask your organization manager to invite you to an organization.

429 – Rate limit reached for requests

Cause: You are sending requests too quickly.
Solution: Pace your requests. Read the Rate limit guide.

429 – You exceeded your current quota, please check your plan and billing details

Cause: You have hit your maximum monthly spend (hard limit) which you can view in the account billing section.
Solution: Apply for a quota increase.

429 – The engine is currently overloaded, please try again later

Cause: Our servers are experiencing high traffic.
Solution: Please retry your requests after a brief wait.

500 – The server had an error while processing your request

Cause: Issue on our servers.
Solution: Retry your request after a brief wait and contact us if the issue persists. Check the status page.

Conclusion

In this article, we discussed all the major features provided by OpenAI API’s and their implementation with examples. Now you are very much capable of using OpenAI’s services effectively in your applications, day-to-day tasks and so much more. As always, there is a lot more to discover about this subject than we are able to discuss in a single blog post. Check out the official OpenAI documentation and community forums if you’re interested in learning more and getting support.

Thank you for reading, Happy coding!

OpenAI Python API – FAQs

1. What is OpenAI and what is its goal?

OpenAI is an artificial intelligence research laboratory and company. Its goal is to ensure that artificial general intelligence (AGI) benefits all of humanity. OpenAI’s goal is to develop and promote friendly AI, focusing on AGI that is safe, beneficial, and aligned with human values.

2. What are some potential use cases for OpenAI API?

The OpenAI API offers a wide range of potential use cases. Some examples are:-
1. Natural language processing: Building chatbots, virtual assistants, or language translation systems.
2. Content generation: Creating human-like text, generating creative writing, or producing product descriptions.
3. Sentiment analysis: Analyzing customer feedback, social media posts, or user reviews.
4. Text summarization: Automatically generating summaries for articles, documents, or long-form content.
5. Question answering: Developing systems that can provide accurate answers to user queries based on a given context.

3. How does the pricing for OpenAI models work?

OpenAI offers different pricing models depending on the specific service or model being used. The exact details of pricing can be found on the OpenAI website. It typically involves a combination of factors such as the number of API calls, the amount of data processed, and the level of usage or subscription plan.



Like Article
Save Article
Share your thoughts in the comments
Similar Reads