liveBook · Manning

Message

As explained in Starting Data Analytics with Generative AI and Python

A structured record containing a conversational role and the corresponding message content.

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

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines roles, content, ordering, and context retention, but does not descend into model internals.
Examples
Uses a resonance dialogue, Portuguese sentiment keywords, and a Python chat-completion request.
What it assumes
Assumes basic Python and API-key setup while teaching message roles and context management.
What you can do after
Apply it via the OpenAI API by assembling role-content records and resending conversation history.
How it connects
Connects message objects with language-model APIs, prompting, conversation history, and system, user, and assistant roles.

Also explained in

Compare how 7 books explain message →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Starting Data Analytics with Generative AI and Python. The section links point to the parts of the book it draws on.