liveBook · Manning

ChatOpenAI

As explained in MongoDB 8.0 in Action, Third Edition

ChatOpenAI is a LangChain chat-model integration for invoking OpenAI conversational language models.

ChatOpenAI is a LangChain chat-model integration that sends structured prompts to OpenAI conversational language models and returns generated responses. In a retrieval-augmented generation pipeline, it answers questions using context retrieved from MongoDB Atlas Vector Search. section 14.5.4 section 14.5.2

A typical ChatOpenAI pipeline connects these stages:

  • RunnableParallel prepares the question and retrieval context. section 14.5.4
  • The retriever obtains relevant documents from an Atlas Vector Search index. section 14.5.4
  • ChatPromptTemplate combines document content, metadata, and the user’s question into a structured prompt. section 14.5.4
  • ChatOpenAI generates an answer, while StrOutputParser extracts the response text for presentation. section 14.5.4

Answering a retrieved question

A LangServe chatbot can retrieve documents for the question “What indexes does MongoDB support?”, pass their content and metadata through ChatPromptTemplate, and use ChatOpenAI to produce an answer. The resulting generation includes a stop reason and the model name; StrOutputParser returns the usable 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 chain definition showing ChatOpenAI alongside document loading, embeddings, MongoDB Atlas Vector Search, prompt formatting, runnable stages, and output parsing. section 14.5.2

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains its pipeline position and response metadata, but does not detail model internals or configuration options.
Examples
Uses a LangServe chatbot that answers a MongoDB indexing question from retrieved document context.
What it assumes
Assumes an OpenAI API key, LangChain components, and configured MongoDB connection settings for the surrounding pipeline.
What you can do after
Apply it via LangChain to generate and parse answers in a retrieval-augmented chatbot.
How it connects
Connects ChatPromptTemplate, RunnableParallel, RunnablePassthrough, StrOutputParser, OpenAIEmbeddings, and MongoDBAtlasVectorSearch.

Also explained in

Compare how 2 books explain ChatOpenAI →

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.