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.
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.
- Set up the OpenAI client with
from openai import OpenAI - Send a prompt to
gpt-4ousingclient.chat.completions.create() - Extract the response text from
response.choices[0].message.content - Print the token usage from
response.usage - 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.
- Define 5 test prompts covering: coding, creative writing, factual Q&A, reasoning, and summarisation
- Write a function that sends each prompt to both GPT-4o and Claude Sonnet
- Record: response text, token usage, and response time for each
- Display results in a comparison table
- 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.
- Use the provided sample image URL (or any publicly accessible image)
- Send an image analysis request using the
image_urlcontent type - Try different prompts: “Describe this image”, “What text is visible?”, “What emotions does this convey?”
- Compare the
detailparameter: “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.