Chat roles and system messages

Working with the OpenAI API

James Chapman

Curriculum Manager, DataCamp

Chat Completions

 

Single-turn tasks

  • Text generation
  • Text transformation
  • Classification

A single-turn task with one prompt and one response.

Working with the OpenAI API

Chat Completions

 

Single-turn tasks

  • Text generation
  • Text transformation
  • Classification

 

Multi-turn conversations

→ Build on previous prompts and responses

A multi-turn task with multiple prompts and responses.

Working with the OpenAI API

Roles

 

  • System: controls assistant's behavior
  • User: instruct the assistant
  • Assistant: response to user instruction
    • Can also be written by the developer to provide examples

The three roles in a chat interaction: the system, the user, and the assistant.

Working with the OpenAI API

Request setup

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
Working with the OpenAI API

Prompt setup

messages=[{"role": "system",
           "content": "You are a Python programming tutor who speaks concisely."},
          {"role": "user",
           "content": "What is the difference between mutable and immutable objects?"}]
Working with the OpenAI API

Making a request

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": "You are a Python programming tutor who speaks concisely."},
            {"role": "user",
             "content": "What is the difference between mutable and immutable objects?"}]
)

print(response.choices[0].message.content)
Working with the OpenAI API

The response

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
Working with the OpenAI API

Mitigating misuse

 

  • System message: Can include guardrails
    • Restrictions on model outputs

guardrails.jpg

A financial tutor chatbot and a financial advisor chatbot.

Working with the OpenAI API

Mitigating misuse with system messages

sys_msg = """
You are finance education assistant that helps students study for exams.

If you are asked for specific, real-world financial advice with risk to their
finances, respond with:

I'm sorry, I am not allowed to provide financial advice.
"""
Working with the OpenAI API

Mitigating misuse with system messages

response = client.chat.completions.create(
  model="gpt-4o-mini",
  messages=[{"role": "system",
             "content": sys_msg},
            {"role": "user",
             "content": "Which stocks should I invest in?"}]
)

print(response.choices[0].message.content)
I'm sorry, I am not allowed to provide financial advice.
Working with the OpenAI API

Let's practice!

Working with the OpenAI API

Preparing Video For Download...