Lesson 15 OpenAI GPT Series
Theme:

Lab Guide — OpenAI GPT Series

Three exercises exploring the OpenAI API, side-by-side model comparison, and GPT vision.

Setup: Open lab.pdf in Jupyter. You need both an OPENAI_API_KEY and an ANTHROPIC_API_KEY in your .env file. Install the openai SDK: pip install openai.

📥 Lab Notebooks

⬇ lab.pdf ⬇ lab_answer.pdf

Exercise 1 — GPT-4o Basics

EX 1

Port a Claude prompt to GPT-4o

Take the prompt comparison pattern from Lesson 03 and adapt it for the OpenAI API.

  1. Set up the OpenAI client with from openai import OpenAI
  2. Send a prompt to gpt-4o using client.chat.completions.create()
  3. Extract the response text from response.choices[0].message.content
  4. Print the token usage from response.usage
  5. Compare the response structure to Anthropic’s response.content[0].text
# OpenAI response structure:
# response.choices[0].message.content  -> text
# response.usage.prompt_tokens         -> input tokens
# response.usage.completion_tokens     -> output tokens

Checkpoint: You can make a successful GPT-4o API call and extract the response text and usage stats.

Exercise 2 — Side-by-Side Comparison

EX 2

Run 5 prompts on both Claude and GPT, compare outputs

Build a comparison function that sends the same prompt to both models and formats the results.

  1. Define 5 test prompts covering: coding, creative writing, factual Q&A, reasoning, and summarisation
  2. Write a function that sends each prompt to both GPT-4o and Claude Sonnet
  3. Record: response text, token usage, and response time for each
  4. Display results in a comparison table
  5. Note which model gave a better response for each prompt (your judgement)

Checkpoint: A table with 5 rows showing both model responses, token counts, and timing for each prompt.

Exercise 3 — GPT-4o Vision

EX 3

Describe an image using GPT-4o vision

Use GPT-4o’s multimodal capabilities to analyse an image.

  1. Use the provided sample image URL (or any publicly accessible image)
  2. Send an image analysis request using the image_url content type
  3. Try different prompts: “Describe this image”, “What text is visible?”, “What emotions does this convey?”
  4. Compare the detail parameter: “low” vs “high” (check token usage difference)
# Image URL content block format:
{"type": "image_url", "image_url": {"url": "https://...", "detail": "high"}}

Checkpoint: GPT-4o accurately describes the image content. You observe the token cost difference between low and high detail.

Tip: If you do not have an OpenAI API key, you can still learn the API structure by reading the code and comparing it with the Anthropic equivalent. The lab answer notebook shows expected outputs.