Lesson 9 of 23 ยท Part 1: API Basics

System Prompts & Role Playing

Use system messages to control the AI's personality and behavior.

Last time: We sent one user message to gpt-4o-mini and printed the reply text.
Today: We add one "system" message that tells the AI who to be before it answers.
OpenAI
client
print()
create()
model
messages
๐Ÿค–
Plain AI (no role)
Source Code
1# 2_system_prompt_role_playing.py
2from openai import OpenAI
3client = OpenAI()
4
5print("Using a system prompt to set the AI's role...")
6
7response = client.chat.completions.create(
8    model="gpt-4o-mini",
9    messages=[
10        {"role": "system", "content": "You are a friendly Python tutor."},
11        {"role": "user", "content": "Explain what a list comprehension is with an example."}
12    ]
13)
14
15print("\nPython Tutor's Response:")
16print(response.choices[0].message.content)

No output yet...

โ† Prev
1/0
Next: Conv. Loop โ†’