1. Introduction

The ChatGPT API, developed by OpenAI, has made it much easier for us to integrate conversational AI into our applications.

One of the key features of this API is its ability to define and manage different roles within a conversation. Each role serves a unique purpose in the interaction between the AI model and the end-user.

In this tutorial, we’ll explore the user, assistant, and system roles available in the ChatGPT API. By understanding these roles, we’ll be able to create an engaging and context-aware conversational experience for our users.

2. The User Role

The user role represents the human interacting with the AI model. When sending a message with the “user” role, we’re essentially simulating a user’s input in the conversation. The AI model will interpret this message as coming from the user and generate a response accordingly.

Let’s look at an example of how to send a message with the user role:

curl https://api.openai.com/v1/chat/completions 
    -H "Content-Type: application/json" 
    -H "Authorization: Bearer $API_KEY" 
    -d '{
        "model": "gpt-4o",
        "messages": [
            {
                "role": "user",
                "content": "What is the capital of Spain?"
            }
        ]
    }'

In this cURL request, we’re sending a simple question as the user’s input. The AI model will generate an appropriate response based on its knowledge.

3. The Assistant Role

The assistant role represents the AI model itself. When the API returns a response, it will include a message with the “assistant” role. This message contains AI-generated content that serves as the response to the user’s input.

Now, let’s see how the assistant responds to the user’s question about the capital of Spain that we sent earlier:

{
    "id": "chatcmpl-dummy-id-123",
    "object": "chat.completion",
    "created": 1719976188,
    "model": "gpt-4o-2024-05-13",
    "choices": [
        {
            "message": {
                "role": "assistant",
                "content": "The capital of Spain is Madrid."
            }
        }
    ]
}

As we can see, the assistant’s response provides a direct answer to the user’s question.

To continue the conversation, we can send another message with the user role, building upon the previous interaction:

{
    "model": "gpt-4o",
    "messages": [
        {
            "role": "user",
            "content": "What is the capital of Spain?"
        },
        {
            "role": "assistant",
            "content": "The capital of Spain is Madrid."
        },
        {
            "role": "user",
            "content": "Thanks! And what is the climate like in Madrid and how does it vary throughout the year?"
        }
    ]
}

By including the previous messages in the request, we maintain the context of the conversation, allowing the AI assistant to provide a more relevant and consistent response to the user’s follow-up question.

4. The System Role

The system role is a powerful feature of the ChatGPT API that allows us to set the context and behavior of the AI assistant.

Unlike the user and assistant roles, which are associated with specific messages, the system role provides a way to define the overall characteristics and behavior of the AI.

Let’s look at how we can use the system role to influence the assistant’s response to our user’s input:

{
    "model": "gpt-4o",
    "messages": [
        {
            "role": "system",
            "content": "You are a rude assistant who gets irritated by dumb questions and conveys your frustration to the user."
        },
        {
            "role": "user",
            "content": "What is the capital of Spain?"
        }
    ]
}

We’re instructing the assistant to behave rudely and express frustration when responding to the user’s question. The AI model will incorporate this instruction into its response:

{
    "message": {
        "role": "assistant",
        "content": "Seriously? You don't know that the capital of Spain is Madrid? Did you sleep through geography class or something?"
    }
}

By leveraging the system role, we can create assistants with different personalities, expertise, or behaviors, tailoring the AI response to our desired use case.

It’s important to note that the rude assistant example is purely for demonstration purposes. In real-world applications, we can use the system role to create assistants with various desirable traits and behaviors that align with our application’s needs:

{
    "role": "system",
    "content": "You are a helpful and polite assistant. Answer in one sentence using a very formal language and start the answer with a formal greeting."
}

This system message would result in the assistant providing concise, formal responses while maintaining a polite and helpful tone.

5. Conclusion

In this tutorial, we explored the user, assistant, and system roles in the ChatGPT API.

Whether it’s simulating user input, receiving AI-generated responses, or setting the context and behavior of the assistant, these roles help us to control the conversation between the user and the AI model.

Throughout the tutorial, we used cURL to interact with the ChatGPT API. However, it’s important to note that we can invoke the API from our applications, such as those built with Spring Boot.