liveBook · Manning

LangChain

As explained in MongoDB 8.0 in Action, Third Edition

LangChain is a software framework for building applications that compose large language models with prompts, tools, memory, and retrieval components.

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)
A diagram of a software

Description automatically generated

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

How MongoDB 8.0 in Action, Third Edition covers it

How deep it goes
Names framework packages and walks through template setup and PDF ingestion, without detailing LangChain internals.
Examples
Uses a `rag-mongo` application template and a MongoDB documentation PDF to populate a vector-search collection.
What it assumes
Assumes access to MongoDB Atlas and an OpenAI API key; supplies project-creation and ingestion steps.
What you can do after
Apply it via the LangChain CLI and `rag-mongo` template to ingest PDF embeddings and expose a chatbot chain.
How it connects
Connects retrieval-augmented generation, OpenAI embeddings, MongoDB Atlas Vector Search, LangServe, and FastAPI.

Also explained in

Compare how 19 books explain LangChain →

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.