Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 4

OpenAI Chat Completions: System & User Roles

Recommended resources

Welcome back! In our previous lesson, we established a crucial link between your Python scripts and the web by learning how to make authenticated HTTP requests and parse JSON data. You practiced this by fetching your user profile from the GitHub API, a perfect warm-up for what's next.

Today, we're taking that foundational skill and applying it to the core of this course: interacting with a Large Language Model. We will make our first call to the OpenAI API using their dedicated Python library. The key learning outcome is to make a chat completion call to the OpenAI API, structuring prompts with system and user roles. Understanding this structure is the first and most fundamental step in prompt engineering and directing an AI's behavior, which is essential for building the agentic systems you aim to architect.

Part 1: Setting Up Your OpenAI Environment

Just like with the GitHub API, the OpenAI API requires an authentication key. Instead of using the generic requests library, we'll use OpenAI's official Python library, which acts as a Software Development Kit (SDK). An SDK simplifies the process by providing convenient functions that handle the underlying HTTP requests and authentication for us.

First, let's get you set up with an account and API key.

The OpenAI (Python) API | Introduction & Example Code

This video by Shaw Talebi provides a clear, visual walkthrough of navigating the OpenAI platform to get everything you need.

Please watch the segment from account and key setup. This will guide you through: Creating an account (or logging in if you have one). Adding a payment method. API calls are very inexpensive for personal use—often just a few cents for hundreds of calls—but a payment method is required. Setting usage limits, which is a great practice to avoid unexpected costs. Creating and copying your new secret API key. Remember to copy it immediately, as you will not be able to see it again.

With your API key in hand, the next step is to install the openai library and configure your project to use the key securely. We'll use the same environment variable approach we discussed in the last lesson.

OpenAI API Python Tutorial – A Complete Crash Course

This guide from MachineLearningPlus will walk you through the setup in your Python environment.

Please read the section titled "How Do You Set Up the OpenAI Python SDK?". Focus on the following steps: Install the library using pip. Follow the instructions in Step 3 to create a .env file in your project folder and store your API key there. You'll also need to install the python-dotenv package (pip install python-dotenv) to load it. Run the quick test call. This is a simple way to confirm that your API key is correctly configured before moving on.

If the test call successfully prints a model ID (like gpt-4o-2024-08-06), your environment is correctly configured, and you're ready to communicate with the model.


Part 2: Structuring Your First API Call

The primary endpoint for conversational models is the Chat Completions API. When you use ChatGPT, you're interacting with a user interface built on top of this API. As developers, we can call it directly.

The fundamental concept to grasp is the messages parameter. You don't just send a single string of text; you send a list of message objects. Each object is a Python dictionary containing two keys: role and content. This structure allows for rich, multi-turn conversations.

There are three primary roles:

  • system: This is a high-level instruction that sets the context and persona for the AI. It steers the model's behavior, personality, and constraints throughout the conversation. For example: "You are a helpful assistant that translates technical jargon into simple English."
  • user: This represents the input from the end-user. It's the question or command you want the AI to respond to.
  • assistant: This is where the AI's previous responses are stored. When you want the model to remember the conversation's history, you include the past user and assistant messages in your subsequent API calls.

From your perspective as a system architect, the system message is one of your most powerful tools for defining an agent's core function and boundaries.

The image below shows what this messages array looks like inside an actual API request payload. The openai library builds this structure for you based on your Python code.

A JSON payload for an OpenAI API call. The `messages` array contains a history of turns, including a `system` message that defines the AI's persona as Charles Darwin.

Now, let's see how this translates to Python code.

OpenAI API Python Tutorial – A Complete Crash Course

Let's return to the MachineLearningPlus guide to see the simplest possible API call.

Read the section Making Your First API Call. Pay close attention to the code block. Notice how the messages parameter is a list containing a single dictionary with the role set to "user". This is the most basic form of a prompt.

To reinforce this crucial concept, here is another excellent breakdown of the roles.

Build an AI Chatbot with Python and the OpenAI API

This Dataquest article provides a very clear, focused explanation of the message roles.

In the article, find the subsection "Understanding the Code." Read the three bullet points that define the System, User, and Assistant roles.

By combining a system message to define the persona and a user message to provide the immediate query, you gain significant control over the model's output.


Your Turn: Eliciting an Opinionated Response

Now it's time for you to apply these concepts. Your task is to write a script that prompts the AI to adopt a specific persona and answer a question from that point of view. This is a classic prompt engineering technique and a direct application of what we've just covered.

Task: Write a Python script that leverages the system role to make the AI act as an opinionated senior software developer.

Steps:

  1. Create a new Python file (e.g., first_call.py).
  2. Make sure your .env file with your OPENAI_API_KEY is in the same directory.
  3. Write the script to perform the following:
    • Import the os and openai libraries.
    • Load the environment variables using dotenv.
    • Instantiate the OpenAI client. It will automatically find the API key in your environment.
    • Construct a messages list that includes:
      • A system message defining the persona. For example: "You are a cynical senior PHP developer with 20 years of experience. You have a deep-seated dislike for JavaScript frameworks."
      • A user message asking a relevant question. For example: "What is your opinion on using Next.js for a new e-commerce platform?"
    • Call client.chat.completions.create() with the gpt-4o-mini model and your messages list.
    • Extract the content from the response. The response object is a complex dictionary; the text you want is located at response.choices[0].message.content.
    • Print the AI's response to the console.
Click here to see the completed solution
import os
from openai import OpenAI
from dotenv import load_dotenv




# Load environment variables from .env file
load_dotenv()




# The OpenAI client automatically looks for the OPENAI_API_KEY
# environment variable.
try:
    client = OpenAI()
except Exception as e:
    print(f"Error initializing OpenAI client: {e}")
    print("Please ensure your OPENAI_API_KEY is set correctly in a .env file.")
    exit()




# 1. Define the persona with a system message.
system_prompt = (
    "You are a cynical senior PHP developer with 20 years of experience. "
    "You believe in server-side rendering and have a deep-seated "
    "dislike for JavaScript frameworks, which you see as overly "
    "complex and unnecessary."
)




# 2. Define the user's question.
user_question = "What is your opinion on using Next.js for a new e-commerce platform?"




# 3. Construct the messages list.
messages = [
    {"role": "system", "content": system_prompt},
    {"role": "user", "content": user_question}
]

print("--- Sending Request to OpenAI ---")
print(f"System Prompt: {system_prompt}")
print(f"User Question: {user_question}")
print("---------------------------------")

try:



    # 4. Make the API call.
    response = client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages
    )




    # 5. Extract and print the response content.
    assistant_response = response.choices[0].message.content
    
    print("\n--- AI Response ---\n")
    print(assistant_response)
    print("\n-------------------\n")

except Exception as e:
    print(f"An error occurred during the API call: {e}")


Conclusion

Congratulations on making your first successful API call to a large language model! This is a significant milestone. You've gone from the theory of HTTP requests to practical, hands-on interaction with a state-of-the-art AI.

Key Takeaways:

  • The openai Python library provides a convenient SDK that simplifies API calls.
  • API keys should always be stored securely as environment variables, not hardcoded in your scripts.
  • The Chat Completions API is the standard for interacting with conversational models.
  • Prompts are structured as a messages list, with each message having a role (system, user, or assistant) and content.
  • The system role is a powerful tool for defining an AI's persona, rules, and context, which is fundamental to designing predictable agentic systems.

You now have the core skill needed to send instructions to an LLM. In our next lesson, we will refine this skill by learning to control the LLM's generation with parameters like temperature and max_tokens. This will allow you to fine-tune the creativity, length, and determinism of the AI's responses, giving you even more control as you start to build more complex applications.