liveBook · Manning

Dialogue

As explained in Grokking AI Applications

A record of earlier messages exchanged between a user and an agent or model.

A conversation thread is the running record of earlier user and agent messages. Because each model call starts without prior context, the application resends relevant history so the next response can interpret follow-up questions. section 1.4

A simple thread follows this loop:

  • Start with an empty history. section 3.4
  • Append each new user message and agent response. section 3.4
  • Inject the accumulated history into the next prompt before calling the model. section 3.4

Remembering a name

After a user says, “Hi, I’m Andrea,” the application includes that exchange when the user later asks, “What’s my name?” The model can then answer “Andrea” because the prior turn accompanies the new request. section 1.4

import os
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI    #A
from langchain_core.prompts import PromptTemplate    #B
from langchain_core.messages import SystemMessage, HumanMessage    #C

load_dotenv()     #D
os.environ["OPENAI_API_KEY"] = "REPLACE_WITH_YOUR_KEY"

if not os.getenv("OPENAI_API_KEY"):     #E
   raise RuntimeError("Set OPENAI_API_KEY before running this script.")

llm = ChatOpenAI(model="gpt-4o-mini", temperature=0.1)    #F
system_msg = SystemMessage(content="Always reply in Pirate English.")    #G

prompt = PromptTemplate(
   input_variables=["message", "history"],
   template="{history}\nUser: {message}\nAI: ",
)     #H
def chat_loop() -> None:    #I
   """Chat loop with manual history injection, matching Langflow’s wiring."""

   history_lines: list[str] = []

   try:
       while True:
           history_block = "\n".join(history_lines)
           user_input = input("User: ")
           rendered_prompt = prompt.format(message=user_input, history=history_block)
   response = llm.invoke(
            [system_msg, HumanMessage(content=rendered_prompt)]
       )    #J
       print("AI:", response.content)    #K

       history_lines.extend(
            [f"User: {user_input}", f"AI: {response.content}"]
           )    #L
   except KeyboardInterrupt:
       print("\nGoodbye, matey!")

if __name__ == "__main__":     #M
   chat_loop()

A minimal Python chat loop demonstrates a list-based history, prompt-template injection, and repeated model calls. section 3.4

How Grokking AI Applications covers it

How deep it goes
Explains application-managed history and demonstrates list-based reinjection, without detailing storage or long-thread limitations.
Examples
Uses an Andrea name-recall exchange and a Python chat loop with history_lines.
What it assumes
Assumes basic prompt and model-call concepts while introducing memory as repeated forwarding of prior messages.
What you can do after
Build it from scratch by appending turns and injecting the resulting history into successive prompts.
How it connects
Connects conversation history to prompt templates, language models, and memory.

Also explained in

Compare how 10 books explain dialogue →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking AI Applications. The section links point to the parts of the book it draws on.