liveBook · Manning

Dialogue

As explained in Starting Data Analytics with Generative AI and Python

An interaction pattern in which multiple successive user and assistant messages form a single contextual conversation.

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

How Starting Data Analytics with Generative AI and Python covers it

How deep it goes
Defines message roles and history, and notes that requests must supply context because past requests are not remembered.
Examples
Uses a Python API exchange about explaining resonance to children, followed by a question about where resonance is used.
What it assumes
Assumes an API key and Python setup, while explaining message roles and context requirements.
What you can do after
Apply dialogue by sending a message array containing prior user and assistant turns with each API request.
How it connects
Connects message history with system, user, and assistant roles and API requests.

Also explained in

Compare how 10 books explain dialogue →

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.