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:
Create a local Atlas deployment.
Set up the environment.
Use a local embedding model to generate vector embeddings.
Create a MongoDB Vector Search index on your data.
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.
Prerequisites
In addition to the common prerequisites, this tutorial requires the following:
Create a Local Deployment
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.
Create a local deployment by using the Atlas CLI.
Run atlas deployments setup and follow the prompts to create a
local deployment.
For detailed instructions, see Create a Local Atlas Deployment.
Load the sample data into your deployment.
Run the following command in your terminal to download the sample data:
curl https://atlas-education.s3.amazonaws.com/sampledata.archive -o sampledata.archive 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
mongorestorecommand.
Set Up the Environment
Generate Embeddings with a Local Model
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.
Create the MongoDB Vector Search Index
To enable vector search on the sample_airbnb.listingsAndReviews
collection, create a MongoDB Vector Search index.