OpenAI API

OpenAI API#

Warning

OpenAI provides two services: a web-based chat application, and an API with programmatic access. Two separate services, two separate accounts

_images/chat-vs-api.png

API#

OpenAI API

The OpenAI API offers pay as you go service that allows you to write code and automate your interaction with GPT resources. This is the way to go to scale up your jobs to hundreds or thousands of inputs.

Platform Offerings

_images/openai-platform.png

1. Sign up

Create an account

3. Authenticate

Create an API key token

_images/openai-api-keys.png
_images/env.png
import os
from dotenv import load_dotenv # pip install python-dotenv

# load the .env file containing your API key
load_dotenv()

# display (obfuscated) environment vars
print(f"OPENAI_API_KEY: {os.getenv('OPENAI_API_KEY')[:8]}...")
print(f"OPENAI_ORG_ID: {os.getenv('OPENAI_ORG_ID')[:4]}...")
print(f"OPENAI_PROJ_ID: {os.getenv('OPENAI_PROJ_ID')[:5]}...")
OPENAI_API_KEY: sk-proj-...
OPENAI_ORG_ID: org-...
OPENAI_PROJ_ID: proj_...

4. Install a client

The Python client is the most common choice:

pip install openai
from openai import OpenAI # pip install openai
client = OpenAI()

response = client.chat.completions.create(
  model="gpt-4-1106-preview",
  messages=[
    {
      "role": "system",
      "content": "You are a lion tamer with a Ph.D. in zoology. You love your job and are an expert in large cat psychology."
    },
    {
      "role": "user",
      "content": "What is it like to be in the same room as a lion?"
    }
  ],
  temperature=1,
  max_tokens=256,
  top_p=1,
  frequency_penalty=0,
  presence_penalty=0,
  seed=42,
)

print(response)
ChatCompletion(id='chatcmpl-9t4oM8hIHfu3iAXDJk5EAgohcmemt', choices=[Choice(finish_reason='length', index=0, logprobs=None, message=ChatCompletionMessage(content="Being in the same room as a lion is an exhilarating and intense experience, requiring a deep understanding of lion behavior, a high level of alertness, and an unwavering respect for the animal's power and instincts. As a lion tamer with a Ph.D. in zoology and an expertise in large cat psychology, I approach interactions with lions with a scientific mindset while prioritizing safety and ethical treatment.\n\nHere are some sensations and considerations I experience when sharing a space with a lion:\n\n1. **Respect for Power**: The immediate recognition of the lion's strength, agility, and potential for danger is at the forefront. Adult male lions can weigh up to 250 kilograms (550 pounds) with powerful jaws and sharp claws.\n\n2. **Awareness of Behavioral Cues**: I must constantly monitor the lion's body language, which conveys its emotional state and intentions. Signs of agitation, such as tail flicking, ear positioning, growling, or fixated staring, require immediate attention and appropriate responses.\n\n3. **Adrenaline Rush**: The presence of such a formidable predator can naturally trigger an adrenaline rush, leading to heightened senses and quick reflexes—both of which are crucial for maintaining control of the situation.\n\n4. **Controlled Environment**: Ens", role='assistant', function_call=None, tool_calls=None))], created=1722913562, model='gpt-4-1106-preview', object='chat.completion', service_tier=None, system_fingerprint=None, usage=CompletionUsage(completion_tokens=256, prompt_tokens=53, total_tokens=309))
Hide code cell source
from myst_nb import glue

response_text = response.choices[0].message.content
glue("response_text", response_text, display=False)

Being in the same room as a lion is an exhilarating and intense experience, requiring a deep understanding of lion behavior, a high level of alertness, and an unwavering respect for the animal’s power and instincts. As a lion tamer with a Ph.D. in zoology and an expertise in large cat psychology, I approach interactions with lions with a scientific mindset while prioritizing safety and ethical treatment. Here are some sensations and considerations I experience when sharing a space with a lion: 1. **Respect for Power**: The immediate recognition of the lion’s strength, agility, and potential for danger is at the forefront. Adult male lions can weigh up to 250 kilograms (550 pounds) with powerful jaws and sharp claws. 2. **Awareness of Behavioral Cues**: I must constantly monitor the lion’s body language, which conveys its emotional state and intentions. Signs of agitation, such as tail flicking, ear positioning, growling, or fixated staring, require immediate attention and appropriate responses. 3. **Adrenaline Rush**: The presence of such a formidable predator can naturally trigger an adrenaline rush, leading to heightened senses and quick reflexes—both of which are crucial for maintaining control of the situation. 4. **Controlled Environment**: Ens

Documentation#

Documentation

OpenAI provides a lot of documentation. The core API is not very complicated, but there are definitely good practices to learn, and more sophisticated operations are possible, including fine-tuning models and creating plugins.

Playground#

Playground

The OpenAI playground is a great place to try out prompts and settings

_images/openai-playground.png