A dialogue is a sequence of user and assistant messages that gives a model context for follow-up requests. Send the relevant exchange in each request: the model does not remember past requests by default. Messages use roles such as system, user, and assistant; a system message can set the assistant’s behavior. 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 a Python API request that includes a system instruction, an earlier exchange about resonance, and a follow-up question. section 1.3.3









