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.
Make the MongoDB docs better! We value your opinion. Share your feedback for a chance to win $100.
MongoDB Branding Shape
Click here >
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 by using the Atlas CLI. The Atlas CLI is the command-line interface for MongoDB Atlas, and you can use the Atlas CLI to interact with Atlas from the terminal for various tasks, including creating local Atlas deployments. To learn more, see Manage Local and Cloud Deployments from the Atlas CLI.

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 create local Atlas deployment loaded with the sample AirBnB listings dataset to use as a vector database.

Note

If you already have an existing local deployment or a MongoDB Community or Enterprise cluster with Search and Vector Search installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

1

Run atlas deployments setup and follow the prompts to create a local deployment.

For detailed instructions, see Create a Local Atlas Deployment.

2
  1. Run the following command in your terminal to download the sample data:

    curl https://atlas-education.s3.amazonaws.com/sampledata.archive -o sampledata.archive
  2. Run the following command to load the data into your deployment, replacing <port-number> with the port where you're hosting the deployment:

    mongorestore --archive=sampledata.archive --port=<port-number>

    Note

    You must install MongoDB Command Line Database Tools to access the mongorestore command.

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.