Lesson 8 of 23 · Part 1: API Basics

Basic API Call

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

Last time: An agent is an LLM plus tools plus a loop: the AI decides, your code acts.
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# 1_basic_prompt.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: System Prompts →