liveBook · Manning

Rag-mongo template

As explained in MongoDB 8.0 in Action, Third Edition

The rag-mongo template is a LangChain project template for building retrieval-augmented generation applications backed by MongoDB Atlas.

The rag-mongo template bootstraps a Python LangChain application for retrieval-augmented generation backed by MongoDB Atlas and Atlas Vector Search. section 14.5 section 14.5.1

Create and expose the application with these steps:

  • Run `langchain app new mongodb-in-action --package rag-mongo`, optionally adding `--non-interactive` to suppress prompts. section 14.5.1
  • Install `langchain_openai`, then inspect the generated `app`, `packages/rag-mongo`, `ingest.py`, and `rag_mongo/chain.py` files. section 14.5.1
  • Import the chain into `server.py` and expose it at `/rag-mongo` with LangServe. section 14.5.1
  • Run `langchain serve` and open `/rag-mongo/playground/` on local port 8000 to test the chatbot. section 14.5.4

The generated package separates ingestion from answering: `ingest.py` loads data into MongoDB and interacts with Atlas Vector Search, while `chain.py` combines retrieved context with a language model. section 14.5.1

Trace a question through the chain

For “What indexes does MongoDB support?”, RunnableParallel prepares the question and context, the Retriever finds relevant Atlas Vector Search documents, RunnablePassthrough forwards context, ChatPromptTemplate structures the prompt, ChatOpenAI generates an answer, and StrOutputParser returns the text. section 14.6 section 14.5.4

Listing 14.1 Files and directories in the LangChain project
├── Dockerfile
├── README.md
├── app
│   ├── __init__.py
│   ├── __pycache__
│   │   ├── __init__.cpython-312.pyc
│   │   └── server.cpython-312.pyc
│   └── server.py
├── packages
│   ├── README.md
│   └── rag-mongo
│       ├── LICENSE
│       ├── README.md
│       ├── _images
│       │   ├── cluster.png
│       │   ├── collections.png
│       │   ├── connect.png
│       │   ├── create.png
│       │   ├── driver.png
│       │   ├── json.png
│       │   ├── json_editor.png
│       │   ├── search-indexes.png
│       │   └── uri.png
│       ├── ingest.py
│       ├── pyproject.toml
│       ├── rag_mongo
│       │   ├── __init__.py
│       │   └── chain.py
│       ├── rag_mongo.ipynb
│       └── tests
│           └── __init__.py
└── pyproject.toml

Inspect the generated project layout to locate the server, ingestion script, package metadata, chain implementation, notebook, and tests. section 14.5.1

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Explains setup commands, generated files, route wiring, and chain execution, while deferring broader deployment and operational concerns.
Examples
Uses a generated `mongodb-in-action` project, a `/rag-mongo` route, the LangServe playground, and an index-types question.
What it assumes
Assumes LangChain CLI and MongoDB Atlas workflow familiarity, while installing the generated project dependencies and wiring the server explicitly.
What you can do after
Build it from scratch as a LangChain project, expose its RAG chain over HTTP, and test responses through LangServe.
How it connects
Connects LangChain CLI, LangServe, FastAPI, MongoDB Atlas, Atlas Vector Search, embeddings, retrieval, and language-model generation.

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.