For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Build a Local RAG Implementation with MongoDB Vector Search

This tutorial demonstrates how to implement retrieval-augmented generation (RAG) locally, without the need for API keys or credits. To learn more about RAG, see Retrieval-Augmented Generation (RAG) with MongoDB.

Specifically, you perform the following actions:

  1. Create a local Atlas deployment.

  2. Set up the environment.

  3. Use a local embedding model to generate vector embeddings.

  4. Create a MongoDB Vector Search index on your data.

  5. Use a local LLM to answer questions on your data.

In this tutorial, you create a local Atlas deployment using Python and Docker through the atlas-local-lib-py library. This library simplifies deployment management by automatically handling Docker containers and providing programmatic access to connection strings. To learn more, see the atlas-local-lib-py repository.

You can also use a MongoDB Community or Enterprise cluster with Search and Vector Search installed.

Note

Local Atlas deployments are intended for testing only. For production environments, deploy a cluster.

In addition to the common prerequisites, this tutorial requires the following:

In this section, you load an embedding model locally and generate vector embeddings by using data from the sample_airbnb database, which contains a collection called listingsAndReviews.

This code might take several minutes to run. After it's finished, you can view your vector embeddings by connecting to your local deployment from mongosh or your application using your deployment's connection string. Then you can run read operations on the sample_airbnb.listingsAndReviews collection.

Tip

You can convert the embeddings in the sample data to BSON vectors for efficient storage and ingestion of vectors in Atlas. To learn more, see how to convert native embeddings to BSON vectors.

To enable vector search on the sample_airbnb.listingsAndReviews collection, create a MongoDB Vector Search index.