Lesson 10 of 33 ยท Part 1: API Basics

Basic API Call

Send your first prompt to OpenAI and get a response back.

Last time: We installed Python, made an API key, hid it safely in .env, and checked our setup.
Today: We write real Python: send one question to OpenAI and print the answer it sends back.
1. Set up
2. Pack the request
3. Send
4. AI writes
5. Unpack the reply
6. Print
Your laptop
โ–ถ
โ—€
OpenAI
Source Code
1# part1/basic_api.py
2from openai import OpenAI
3client = OpenAI()
4
5print("Sending a basic prompt to the AI...")
6
7response = client.chat.completions.create(
8    model="gpt-4o-mini",
9    messages=[
10        {"role": "user", "content": "Write a funny 3-line poem about AI and pizza."}
11    ]
12)
13
14print("\nAI's Response:")
15print(response.choices[0].message.content)

No output yet...

โ† Prev
1/0
Next: Streaming โ†’