OpenAI's Chat Completion API - Instruction

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Heads up... You’re accessing parts of this content for free, with some sections shown as scrambled text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

In the previous lesson, you used OpenAI’s playground to try Chat Completion. Now, you’ll use the API directly. This is useful for text generation in your own apps.

OpenAI Package

Open Jupyter Lab and navigate to your notebook. Then, add the following code:

%pip install openai

import os
import openai
openai.api_key = os.environ["OPENAI_API_KEY"]
from openai import OpenAI

client = OpenAI()

Chat Completion API

What if you want to generate code with a prompt? Like writing a hello world in Python. You can do that with the chat completion API.

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[
    {"role": "user", "content": "Write hello world in Python"}
  ]
)

print(response)

print(response.choices[0].message.content)

Learning the Roles

Earlier, you should’ve noticed the role field in the messages parameter of the completion API. The role user means that the message is coming from the user. Here are the other roles:

Looking at Parameters

You might recall from earlier, that there’s some randomness to the output of the completion API. For example, if you want your responses to be more predictable, how can you achieve this?

Handling Errors

Remember that you looked at your account limits in an earlier lesson. What if you reach those limits? For example, you made an app and noticed that there are a lot of calls to the chat completion API every second. That sounds like your app is getting popular! But, your users might see unexpected behavior afterward. You don’t want this, of course.

# 1
try:
  # 2
  response = client.chat.completions.create(
      model="gpt-4o-mini",
      messages=[
        {"role": "user", "content": "Write hello world in python"}
      ]
  )
  print(response.choices[0].message.content)
# 3
except openai.APIConnectionError as e:
  print(f"What do we want? Faster Internet! When do we want it? Now!")
What do we want? Faster Internet! When do we want it? Now!
# 1
try:
...
except openai.APIConnectionError as e:
  ...
# 2
except openai.RateLimitError as e:
  print(f"DON'T PANIC! slow down and try again later.")
# 3
except openai.APIError as e:
  print(f"Does not compute 🤖.")
See forum comments
Download course materials from Github
Previous: Basic Chat Completion with GPT-4o - Introduction Next: Streaming in Chat Completion - Demo