The OpenAI API

Let’s get started

The great thing about APIs is that we can start right away without too much preparation!

In this sprint, we will use the OpenAI API for completions and embeddings.

Resource: OpenAI API docs

Authentication

Typically, it’s as simple as this:

# setting up the client in Python
import os
from openai import OpenAI

client = OpenAI(
    api_key=os.environ.get("OPENAI_API_KEY")
)

Authentication for the seminar

For the sprint, we have a model in Azure.

import os
from llm_utils.client import get_openai_client

MODEL = "gpt-4o"

client = get_openai_client(
    model=MODEL,
    config_path=os.environ.get("CONFIG_PATH")
)

Creating a completion

chat_completion = client.chat.completions.create(
    messages=[
        {
            "role": "user",
            "content": "How old is the earth?",
        }
    ],
    model="gpt-4o" 
)

# check out the type of the response

print(f"Response: {type(chat_completion)}") # a ChatCompletion object
Response: <class 'openai.types.chat.chat_completion.ChatCompletion'>

Retrieving the response

# print the message we want
print(f"\nResponse message: {chat_completion.choices[0].message.content}")

# check the tokens used 
print(f"\nTotal tokens used: {chat_completion.usage.total_tokens}")

Response message: The Earth is approximately 4.54 billion years old. This estimate is based on radiometric age dating of the oldest rocks and meteorites found on Earth and in the solar system.

Total tokens used: 51