Chat roles and system messages

Lavorare con l'API di OpenAI

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.

Lavorare con l'API di OpenAI

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.

Lavorare con l'API di OpenAI

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.

Lavorare con l'API di OpenAI

Request setup

response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[{"role": "user", "content": prompt}]
)
Lavorare con l'API di OpenAI

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?"}]
Lavorare con l'API di OpenAI

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)
Lavorare con l'API di OpenAI

The response

Mutable objects can be changed after creation, while immutable objects cannot be
modified once they are created.
Lavorare con l'API di OpenAI

Mitigating misuse

 

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

guardrails.jpg

A financial tutor chatbot and a financial advisor chatbot.

Lavorare con l'API di OpenAI

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.
"""
Lavorare con l'API di OpenAI

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.
Lavorare con l'API di OpenAI

Let's practice!

Lavorare con l'API di OpenAI

Preparing Video For Download...