liveBook · Manning

Chat API

As explained in AI Agents and Applications

A chat API is an interface for exchanging conversational messages with a language model and receiving responses.

A chat API lets an application exchange conversational messages with a language model. Role-based messages—system, user, and assistant—separate behavior instructions from user requests and replies. Clear instructions help keep responses useful and safe. section 1.1.2

For a Q&A chatbot, send the question with relevant document context, then let the user ask a follow-up to refine the answer. Retaining earlier turns helps the chatbot respond coherently, but conversation history consumes context-window space and may need summarizing. section 6.1.1 section 1.1.2

A chat API does not itself supply access to a company's documents. For a larger knowledge base, retrieve relevant content rather than putting everything into the prompt. The LangChain setup below sends retrieved context and a question to ChatOpenAI; it is an initial RAG chain, before message history is added. section 6.1.2 section 7.4

Listing 7.3 Initial RAG setup before incorporating message history
from langchain_core.runnables import RunnablePassthrough
from langchain_core.prompts import PromptTemplate
from langchain_openai import ChatOpenAI

rag_prompt_template = """Use the following pieces of context
to answer the question at the end. 
If you don't know the answer, just say that you don't know, 
don't try to make up an answer.
Use three sentences maximum and keep the 
answer as concise as possible.
{context}
Question: {question}
Helpful Answer:"""

rag_prompt = PromptTemplate.from_template(rag_prompt_template)

retriever = vector_db.as_retriever()

question_feeder = RunnablePassthrough()

chatbot = ChatOpenAI(openai_api_key=OPENAI_API_KEY, 
                     model_name="gpt-5-nano")

rag_chain = {"context": retriever, 
             "question": question_feeder}|rag_prompt|chatbot

def execute_chain(chain, question):
    answer = rag_chain.invoke(question)
    return answer

The listing shows where a ChatOpenAI model fits after retrieval and prompt construction, and why this starting chain does not yet remember earlier messages. section 7.4

How AI Agents and Applications covers it

How deep it goes
Defines role-based messaging and shows a LangChain model integration, but stops short of showing raw chat API requests.
Examples
Uses a document-questioning exchange with follow-ups and a Python RAG chain built around ChatOpenAI.
What it assumes
Starts with a no-code chatbot interaction before showing a Python and LangChain setup.
What you can do after
Apply it via LangChain’s ChatOpenAI component in a retrieval-based Q&A chain.
How it connects
Connects chat APIs to prompt design, conversation memory, context windows, retrieval, and retrieval-augmented generation.

Also explained in

Compare how 2 books explain chat API →

Related concepts

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