Ai Engineering Core / Build First LLM Product

Running First OpenAI Api Call

πŸ“ First Things First β€” Set Up Your File Structure

Before we dive in, let's get organized:

  1. In your project root, create a new folder called level1

  2. Inside that folder, create a new notebook file called part1.ipynb

Your structure should look like this:

llm_engineering/
└── level1/
    └── part1.ipynb

βœ… Make sure your Kernel is pointed to the correct virtual environment before running anything! 🐍


🧩 Step 1 β€” Import the Necessary Packages

▢️ Create a new code cell and run it:

import os
from dotenv import load_dotenv
from IPython.display import Markdown, display
from openai import OpenAI

⚠️ Common Issue: Kernel Problems

If the cell hangs forever, throws import errors, or simply won't execute, the culprit is usually the Python kernel. Double check the top-right corner of your notebook β€” it should be pointing to your local .venv virtual environment, not the system Python.


πŸ”‘ Step 2 β€” Load Your Environment Variables

▢️ Create another code cell and run it:

# Load environment variables from .env file
load_dotenv(override=True)
api_key = os.getenv('OPENAI_API_KEY')

# Validate the key
if not api_key:
    print("No API key was found - please head over to the troubleshooting notebook in this folder to identify & fix!")
elif not api_key.startswith("sk-proj-"):
    print("An API key was found, but it doesn't start sk-proj-; please check you're using the right key - see troubleshooting notebook")
elif api_key.strip() != api_key:
    print("An API key was found, but it looks like it might have space or tab characters at the start or end - please remove them - see troubleshooting notebook")
else:
    print("API key found and looks good so far!")

πŸ” What's happening under the hood?

  1. πŸ“‚ Reads your .env file

  2. πŸ“₯ Loads the environment variables into memory

  3. πŸ”Ž Fetches the OPENAI_API_KEY value

  4. βœ… Validates the key format and structure

Expected output:

API key found and looks good so far!

🌩️ Step 3 β€” Your First Cloud LLM Call

Now comes the exciting part β€” your very first real request to OpenAI! πŸŽ‰

Unlike local models like Ollama that run on your machine, this sends a request to OpenAI's cloud-hosted models and gets a response back in real time.

▢️ Create a new code cell and run it:

# Preview: this is all it takes to send a message to OpenAI!
message = "Hello, GPT! This is my first ever message to you! Hi!"
messages = [{"role": "user", "content": message}]
messages

This simply prepares your message in the format OpenAI expects. βœ‰οΈ

▢️ Now create another code cell and run it:

openai = OpenAI()

response = openai.chat.completions.create(model="gpt-4o-mini", messages=messages)
response.choices[0].message.content

This actually sends the request to OpenAI and prints back the model's reply.

openai = OpenAI()

Creates the OpenAI client using your API key so your Python app can communicate with OpenAI.


response = openai.chat.completions.create(
    model="gpt-4o-mini",
    messages=messages
)

Sends your prompt (messages) to the gpt-4o-mini model and asks it to generate a response.

  • chat β†’ chat-based interaction

  • completions β†’ generate an answer

  • create() β†’ send the request


response.choices[0].message.content

Extracts the actual text reply from the response object.

Flow:

Your prompt β†’ OpenAI API β†’ AI generates answer β†’ extract response text

Expected Output

You should see a friendly response from GPT printed below the cell β€” if you do, congratulations! You've just made your first ever LLM API call! 🎊


βœ… Special Note

πŸ“Œ Important Reminders β€” Keep These in Mind!

These two points are crucial β€” remember them for all upcoming lessons:

  1. πŸ–₯️ Always select your virtual environment as the kernel before running any notebook. You can check this in the top-right corner of your notebook.

  2. ▢️ Always create a separate code cell for each step and run it immediately. Don't skip running a cell before moving to the next one β€” skipping this is one of the most common causes of errors!

πŸ’‘ Get into the habit of these two practices from the start and you'll save yourself a lot of headaches down the road! πŸ™Œ