LangChain is a framework for applications that connect large language models (LLMs) to external data and tools. In a retrieval-augmented generation (RAG) chatbot, it coordinates document processing, embeddings, vector search, and the context supplied to the model. That coordination avoids wiring each interaction by hand. LangChain is available for Python and JavaScript; the workflow below uses Python. section 14.2 section 14.4.1
Build a MongoDB-backed chatbot in three stages:
- Create the application with `langchain app new mongodb-in-action --package rag-mongo`, accept the template installation prompt, and install `langchain_openai`. The template separates ingestion in `ingest.py`, question answering in `rag_mongo/chain.py`, and API setup in `app/server.py`. section 14.5.1
- Configure `ingest.py` with the PDF URL, database, collection, and Atlas Vector Search index. Its loader reads the PDF, a text splitter creates chunks, OpenAI embeddings turn chunks into vectors, and `MongoDBAtlasVectorSearch` stores them. Keep the database settings consistent with `chain.py` and create the named search index in Atlas. section 14.5.2
- Set `OPENAI_API_KEY` and `MONGO_URI`, then run `python3 ingest.py`. Register the question-answering chain with `add_routes(app, rag_mongo_chain, path="/rag-mongo")` to expose it through the application's API. section 14.5.2 section 14.5.1
Documentation-backed answers
For a chatbot answering questions about MongoDB Atlas practices, a MongoDB documentation PDF supplies the source data. After ingestion, inspect the collection to check that documents contain text and an `embedding` field. The chain can then retrieve relevant content to provide context for an answer, rather than relying on the model alone. section 14.5.2 section 14.2
Figure 14.2 Embedding LangChain in the RAG pattern. Data from sources like databases, PDFs, and JSON files is converted to vector embeddings using tools like OpenAI, Cohere, and Hugging Face. These embeddings are stored and indexed in a database. Developer frameworks like LlamaIndex and LangChain manage the processing, storing, and indexing of these embeddings in databases, like MongoDB Atlas, along with metadata and context chunks. When a natural-language input is received, the system retrieves relevant data from the vector index and prompts the LLMs; then the generated output is delivered to the application. Also visible are Atlas Triggers, which react to new documents in the database by automatically using tools like OpenAI to convert them to embeddings and store them in the vector database. (Image © MongoDB 2024 CC BY-NC-SA 3.0)
Trace both paths in the RAG workflow: source documents become indexed embeddings, while a user query retrieves relevant data before the LLM generates an answer. section 14.2


















