liveBook · Manning

Chat completion

As explained in Data Analysis with LLMs

A chat completion is an API operation that generates an assistant response from a sequence of conversational messages.

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

How Data Analysis with LLMs covers it

How deep it goes
Walks through message structure, Python calls, response access, parameters, and retries without explaining model internals.
Examples
Uses a story prompt, generated text-analysis prompts, and a yes-or-no question about an image.
What it assumes
Introduces message dictionaries and client calls from basics while assuming enough Python to read short examples.
What you can do after
Apply it via OpenAI’s Python library to submit messages, retrieve assistant text, and configure generation.
How it connects
Connects message roles, prompts, multimodal input, generation parameters, and retry handling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data Analysis with LLMs. The section links point to the parts of the book it draws on.