A chat completion generates an assistant reply from a list of conversational messages. The list can contain earlier exchanges for context, or just one user prompt for a new task. section 3.4
In the OpenAI Python library, each message is a dictionary with:
- A role identifying its source: user, assistant, or system. A system message can provide general guidance, but is not required for the single-prompt examples. section 3.4
- Content containing the message itself. A text-only task can put its prompt here; an image question can use a list of typed text and image elements. section 3.4 section 6.2.1
import openai
client = openai.OpenAI()
response = client.chat.completions.create(
model='gpt-4o',
messages=[
{'role':'user', 'content':prompt}
]
)
This Python call sends a generated prompt as one user message to GPT-4o. The returned response contains the assistant’s answer, available at response.choices[0].message.content. section 4.2.3
Parameters can shape the result. For a short story, max_tokens limits length, stop names a stopping phrase, and temperature changes the degree of randomization. Other settings can discourage repetition or particular tokens. section 3.5.4 section 3.6
Ask about an image
To ask whether a pictured apple is a banana, send one user message whose content includes the question as text and the picture as an image URL. The expected answer is “No.” section 6.2.1
For many calls over a data set, plan for temporary connection failures. A calling function can wait and retry a limited number of times, then raise an error if the request still fails. section 4.2.3
