A message is a structured record with a role and content. Together, message records form the conversation context sent to a language model. section 1.3.3
Use these roles to shape the exchange:
- system sets the assistant’s behavior, background, tone, or audience. section 1.3.3
- user supplies the request or follow-up question. section 1.3.3
- assistant records an earlier model response so later messages retain the exchange context. section 1.3.3
A continuing exchange
A system message asks for explanations aimed at schoolchildren, a user asks about resonance, an assistant response is included, and a later user asks where resonance is used. Supplying the complete array lets the model relate the follow-up to earlier messages. section 1.3.3
Listing 1.1 Interacting with ChatGPT through the API
from openai import OpenAI
prompt_text = """Generate a list of 20 keywords indicating positive
sentiment to be used for searching customer reviews in
Portuguese."""
client = OpenAI(
api_key=<<your-API-key-here>>,
)
chat_completion = client.chat.completions.create(
messages=[
{"role": "system", "content": "You are a scientific assistant, skilled
at explaining science to schoolchildren."},
{"role": "user", "content": "Explain resonance to first-graders?"},
{"role": "assistant", "content": "Alright, kids, today we're going to
talk about something really cool called resonance! (...)."},
# Shortened from the full model response
{"role": "user", "content": "Where can we use it?"}
],
model="gpt-4-0125-preview",
temperature=0.7,
)
print(chat_completion.choices[0].message.content)
Inspect the Python request to see message objects assembled in a system-user-assistant-user sequence. section 1.3.3






