liveBook · Manning

ChatPromptTemplate

As explained in MongoDB 8.0 in Action, Third Edition

ChatPromptTemplate is a LangChain component that constructs a formatted chat prompt from message templates and input values.

ChatPromptTemplate constructs a structured chat prompt by combining a user's question with retrieved document content and metadata before a chat model generates an answer. section 14.5.4 section 14.5.2

Use it as the prompt-building stage in a retrieval-augmented generation pipeline:

  • Receive the question and context produced by the retrieval chain. section 14.5.4
  • Combine document content with metadata such as source, page number, and embeddings. section 14.5.4
  • Format the combined context and question into an organized prompt for ChatOpenAI. section 14.5.4 section 14.5.2

Index question in a RAG chain

For the question “What indexes does MongoDB support?”, the template combines retrieved document content and metadata into a message instructing the model to answer only from the supplied context. ChatOpenAI then generates the response, and StrOutputParser returns the answer as text. section 14.5.4

import os
from langchain_openai import ChatOpenAI
from langchain_community.document_loaders import PyPDFLoader
from langchain_openai import OpenAIEmbeddings
from langchain_community.vectorstores import MongoDBAtlasVectorSearch
from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.pydantic_v1 import BaseModel
from langchain_core.runnables import (
    RunnableLambda,
    RunnableParallel,
    RunnablePassthrough,
)
from langchain_text_splitters import RecursiveCharacterTextSplitter
from pymongo import MongoClient

# Set DB
if os.environ.get("MONGO_URI", None) is None:
    raise Exception("Missing `MONGO_URI` environment variable.")
MONGO_URI = os.environ["MONGO_URI"]

DB_NAME = "langchain"
COLLECTION_NAME = "mongodb"
ATLAS_VECTOR_SEARCH_INDEX_NAME = "default"

client = MongoClient(MONGO_URI)
db = client[DB_NAME]
MONGODB_COLLECTION = db[COLLECTION_NAME]

# Read from MongoDB Atlas Vector Search
vectorstore = MongoDBAtlasVectorSearch.from_connection_string(
    MONGO_URI,
    DB_NAME + "." + COLLECTION_NAME,
    OpenAIEmbeddings(disallowed_special=()),
    index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
)
retriever = vectorstore.as_retriever(search_kwargs={"k": 1})

# RAG prompt
template = """Answer the question based only on the following context:
{context}
Question: {question}
"""
prompt = ChatPromptTemplate.from_template(template)

# RAG
model = ChatOpenAI()
chain = (
   RunnableParallel({"context": retriever,
➥"question": RunnablePassthrough()})
    | prompt
    | model
    | StrOutputParser()
)


# Add typing for input
class Question(BaseModel):
    __root__: str


chain = chain.with_types(input_type=Question)


def _ingest(url: str) -> dict:
    loader = PyPDFLoader(url)
    data = loader.load()

    # Split docs
    text_splitter = RecursiveCharacterTextSplitter
➥(chunk_size=500, chunk_overlap=0)
    docs = text_splitter.split_documents(data)

    # Insert the documents in MongoDB Atlas Vector Search
    _ = MongoDBAtlasVectorSearch.from_documents(
        documents=docs,
        embedding=OpenAIEmbeddings(disallowed_special=()),
        collection=MONGODB_COLLECTION,
        index_name=ATLAS_VECTOR_SEARCH_INDEX_NAME,
    )
    return {}

ingest = RunnableLambda(_ingest)

Inspect the complete Python chain showing ChatPromptTemplate alongside the retriever, ChatOpenAI, runnable components, and output parser. section 14.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains its pipeline role and inputs but stops short of detailing template syntax or internal implementation.
Examples
Uses retrieved document content, source and page metadata, embeddings, and the question “What indexes does MongoDB support?” as concrete prompt inputs.
What you can do after
Apply it via LangChain to format retrieved context and user questions for a chat model in a RAG application.
How it connects
Connects retrieval-augmented generation with vector search, embeddings, ChatOpenAI, RunnableParallel, RunnablePassthrough, and StrOutputParser.

Also explained in

Compare how 2 books explain ChatPromptTemplate →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of MongoDB 8.0 in Action, Third Edition. The section links point to the parts of the book it draws on.