local-rag

For the complete MongoDB documentation index, see www.mongodb.com/docs/llms.txt

<!-- Tab options on this page. Append to the .md URL to filter: ?tabs=<id,...> select specific tabs (e.g. ?tabs=nodejs,shell) ?allTabs=true include every tab (no param) default: one tab per tabset

Available tabs: other tabs: dotnet-nine, dotnet-eight, local, cloud, maven, gradle, user-access-token, ssh -->

This tutorial demonstrates how to implement retrieval-augmented generation (RAG) locally, without the need for API (Application Programming Interface) keys or credits. To learn more about RAG (Retrieval-Augmented Generation), see [Retrieval-Augmented Generation (RAG) with MongoDB.](https://www.mongodb.com/docs/vector-search/tutorials/rag.md#std-label-ai-key-concepts

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.

Work with a runnable version of this tutorial as a Python notebook.

Note:

For a local RAG (Retrieval-Augmented Generation) implementation with LangChain, see [Build a Local RAG Implementation with MongoDB and LangChain.](https://www.mongodb.com/docs/atlas/ai-integrations/langchain/local-rag.md#std-label-langchain-local-rag

Work with a runnable version of this tutorial as a Python notebook.

Note:

For a local RAG (Retrieval-Augmented Generation) implementation with LangChain, see [Build a Local RAG Implementation with MongoDB and LangChain.](https://www.mongodb.com/docs/atlas/ai-integrations/langchain/local-rag.md#std-label-langchain-local-rag

About this Tutorial

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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed.

Note:

Local Atlas deployments are intended for testing only. For production environments, [deploy a cluster.](https://www.mongodb.com/docs/atlas/tutorial/create-new-cluster.md#std-label-create-new-cluster

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the open source models listed above to perform RAG (Retrieval-Augmented Generation) tasks.

This tutorial also uses the Microsoft.Extensions.AI.Ollama package to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names with their equivalents for your preferred setup.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the open source models listed above to perform RAG (Retrieval-Augmented Generation) tasks.

This tutorial also uses the Microsoft.Extensions.AI.Ollama package to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names with their equivalents for your preferred setup.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the open source models listed above to perform RAG (Retrieval-Augmented Generation) tasks.

This tutorial also uses the Go language port of LangChain, a popular open-source LLM framework, to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names or LangChain library components with their equivalents for your preferred setup.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the open source models listed above to perform RAG (Retrieval-Augmented Generation) tasks.

This tutorial also uses the Go language port of LangChain, a popular open-source LLM framework, to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names or LangChain library components with their equivalents for your preferred setup.

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the following open source models to perform RAG (Retrieval-Augmented Generation) tasks:

This tutorial also uses LangChain4j, a popular open-source LLM framework for Java, to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names or LangChain4j library components with their equivalents for your preferred setup.

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download Ollama and pull the following open source models to perform RAG (Retrieval-Augmented Generation) tasks:

This tutorial also uses LangChain4j, a popular open-source LLM framework for Java, to connect to these models and integrate them with MongoDB Vector Search. If you prefer different models or a different framework, you can adapt this tutorial by replacing the Ollama model names or LangChain4j library components with their equivalents for your preferred setup.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download the Mistral 7B model by using GPT4All, an open-source ecosystem for local LLM development.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download the Mistral 7B model by using GPT4All, an open-source ecosystem for local LLM development.

When working through this tutorial, you use an interactive Python notebook. This environment allows you to create and execute individual code blocks without running the entire file each time.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download the Mistral 7B model by using GPT4All, an open-source ecosystem for local LLM development.

When working through this tutorial, you use an interactive Python notebook. This environment allows you to create and execute individual code blocks without running the entire file each time.

You also use the following open-source models in this tutorial:

There are several ways to download and deploy LLM (Large Language Model)s locally. In this tutorial, you download the Mistral 7B model by using GPT4All, an open-source ecosystem for local LLM development.

Prerequisites

In addition to the [common prerequisites](https://www.mongodb.com/docs/vector-search/about/use-cases.md#std-label-avs-tutorials-prereqs, this tutorial requires the following:

Create a Local Deployment

For Python, you create the local deployment programmatically as part of the environment setup in the next section using the atlas-local-lib-py library. Skip to [Set Up the Environment.](https://www.mongodb.com/docs/vector-search/tutorials/local-rag.md#std-label-local-rag-set-up-environment

For Python, you create the local deployment programmatically as part of the environment setup in the next section using the atlas-local-lib-py library. Skip to [Set Up the Environment.](https://www.mongodb.com/docs/vector-search/tutorials/local-rag.md#std-label-local-rag-set-up-environment

In this section, you create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

text 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 create local Atlas deployment loaded with the [sample AirBnB listings](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb 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](https://www.mongodb.com/docs/search/self-managed/current/installation/linux.md#std-label-community-search-deploy installed, with the sample_airbnb.listingsAndReviews sample data loaded, you can skip this step.

  1. 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.](https://www.mongodb.com/docs/atlas/cli/current/atlas-cli-deploy-local/#create-a-local-atlas-deployment-1

  1. Load the sample data into your deployment.

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

text 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:

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

Note:

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

Set Up the Environment

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your .NET project.

Run the following commands in your terminal to create a new directory named MyCompany.RAG.Local and initialize your project:

console dotnet new console -o MyCompany.RAG.Local cd MyCompany.RAG.Local

  1. Install and import dependencies.

### .NET 9.x+

Run the following commands:

console dotnet add package MongoDB.Driver --version 3.1.0 dotnet add package Microsoft.Extensions.AI.Ollama --prerelease

  1. Set your connection string as an environment variable.

Export your connection string, set it in PowerShell, or use your IDE's environment variable manager to make the connection string available to your project.

shell export MONGODB_URI="<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

text export MONGODB_URI="mongodb://localhost:<port-number>/?directConnection=true"

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your .NET project.

Run the following commands in your terminal to create a new directory named MyCompany.RAG.Local and initialize your project:

console dotnet new console -o MyCompany.RAG.Local cd MyCompany.RAG.Local

  1. Install and import dependencies.

### .NET 9.x+

Run the following commands:

console dotnet add package MongoDB.Driver --version 3.1.0 dotnet add package Microsoft.Extensions.AI.Ollama --prerelease

  1. Set your connection string as an environment variable.

Export your connection string, set it in PowerShell, or use your IDE's environment variable manager to make the connection string available to your project.

shell export MONGODB_URI="<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

text export MONGODB_URI="mongodb://localhost:<port-number>/?directConnection=true"

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your Go project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb and initialize your project:

console mkdir local-rag-mongodb cd local-rag-mongodb go mod init local-rag-mongodb

  1. Install and import dependencies.

Run the following commands:

console go get github.com/joho/godotenv go get go.mongodb.org/mongo-driver/v2/mongo go get github.com/tmc/langchaingo/llms go get github.com/tmc/langchaingo/llms/ollama go get github.com/tmc/langchaingo/prompts go get github.com/tmc/langchaingo/vectorstores/mongovector

  1. Create a .env file.

In your project, create a .env file to store your connection string.

text MONGODB_URI = "<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

text MONGODB_URI = "mongodb://localhost:<port-number>/?directConnection=true"

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your Go project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb and initialize your project:

console mkdir local-rag-mongodb cd local-rag-mongodb go mod init local-rag-mongodb

  1. Install and import dependencies.

Run the following commands:

console go get github.com/joho/godotenv go get go.mongodb.org/mongo-driver/v2/mongo go get github.com/tmc/langchaingo/llms go get github.com/tmc/langchaingo/llms/ollama go get github.com/tmc/langchaingo/prompts go get github.com/tmc/langchaingo/vectorstores/mongovector

  1. Create a .env file.

In your project, create a .env file to store your connection string.

text MONGODB_URI = "<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

text MONGODB_URI = "mongodb://localhost:<port-number>/?directConnection=true"

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Create your Java project and install dependencies.

From your IDE, create a Java project named local-rag-mongodb using Maven or Gradle.

Add the following dependencies, depending on your package manager:

### Maven

If you are using Maven, add the following dependencies to the dependencies array in your project's pom.xml file:

xml <dependencies> <!-- MongoDB Java Sync Driver v5.2.0 or later --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> <!-- Java library for working with Ollama --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-ollama</artifactId> <version>0.35.0</version> </dependency> </dependencies> Run your package manager to install the dependencies to your project. 2. Set your environment variable. Note: This example sets the variable in the IDE. Production applications might manage environment variables through a deployment configuration, CI/CD pipeline, or secrets manager, but you can adapt the provided code to fit your use case. In your IDE, create a new configuration template and add the following variables to your project: - If you are using IntelliJ IDEA, create a new Application run configuration template, then add your variables as semicolon-separated values in the Environment variables field (for example, FOO=123;BAR=456). Apply the changes and click OK. To learn more, see the [Create a run/debug configuration from a template](https://www.jetbrains.com/help/idea/run-debug-configuration.html#createExplicitly section of the IntelliJ IDEA documentation.

  • If you are using Eclipse, create a new Java Application launch configuration, then add each variable as a new key-value pair in the Environment tab. Apply the changes and click OK.

    To learn more, see the Creating a Java application launch configuration section of the Eclipse IDE documentation.

### Local Deployment

Replace the <port-number> with the port for your local deployment.

Your connection string should follow the following format:

shell MONGODB_URI = “mongodb://localhost:<port-number>/?directConnection=true”

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Create your Java project and install dependencies.

From your IDE, create a Java project named local-rag-mongodb using Maven or Gradle.

Add the following dependencies, depending on your package manager:

### Maven

If you are using Maven, add the following dependencies to the dependencies array in your project's pom.xml file:

xml <dependencies> <!-- MongoDB Java Sync Driver v5.2.0 or later --> <dependency> <groupId>org.mongodb</groupId> <artifactId>mongodb-driver-sync</artifactId> <version>[5.2.0,)</version> </dependency> <!-- Java library for working with Ollama --> <dependency> <groupId>dev.langchain4j</groupId> <artifactId>langchain4j-ollama</artifactId> <version>0.35.0</version> </dependency> </dependencies> Run your package manager to install the dependencies to your project. 2. Set your environment variable. Note: This example sets the variable in the IDE. Production applications might manage environment variables through a deployment configuration, CI/CD pipeline, or secrets manager, but you can adapt the provided code to fit your use case. In your IDE, create a new configuration template and add the following variables to your project: - If you are using IntelliJ IDEA, create a new Application run configuration template, then add your variables as semicolon-separated values in the Environment variables field (for example, FOO=123;BAR=456). Apply the changes and click OK. To learn more, see the [Create a run/debug configuration from a template](https://www.jetbrains.com/help/idea/run-debug-configuration.html#createExplicitly section of the IntelliJ IDEA documentation.

  • If you are using Eclipse, create a new Java Application launch configuration, then add each variable as a new key-value pair in the Environment tab. Apply the changes and click OK.

    To learn more, see the Creating a Java application launch configuration section of the Eclipse IDE documentation.

### Local Deployment

Replace the <port-number> with the port for your local deployment.

Your connection string should follow the following format:

shell MONGODB_URI = "mongodb://localhost:<port-number>/?directConnection=true"

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your Node.js project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb and initialize your project:

console mkdir local-rag-mongodb cd local-rag-mongodb npm init -y

  1. Install and import dependencies.

Run the following command:

console npm install mongodb @xenova/transformers node-gyp gpt4all

  1. Update your package.json file.

In your project's package.json file, specify the type field as shown in the following example, and then save the file.

json { "name": "local-rag-mongodb", "type": "module", ... }

  1. Create a .env file.

In your project, create a .env file to store your connection string.

javascript MONGODB_URI = "<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

javascript MONGODB_URI = "mongodb://localhost:<port-number>/?directConnection=true";

Note: Minimum Node.js Version Requirements

Node.js v20.x introduced the --env-file option. If you are using an older version of Node.js, add the dotenv package to your project, or use a different method to manage your environment variables.

In this section, you set up the environment for this tutorial. Create a project, install the required packages, and define a connection string:

  1. Initialize your Node.js project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb and initialize your project:

console mkdir local-rag-mongodb cd local-rag-mongodb npm init -y

  1. Install and import dependencies.

Run the following command:

console npm install mongodb @xenova/transformers node-gyp gpt4all

  1. Update your package.json file.

In your project's package.json file, specify the type field as shown in the following example, and then save the file.

json { "name": "local-rag-mongodb", "type": "module", ... }

  1. Create a .env file.

In your project, create a .env file to store your connection string.

javascript MONGODB_URI = "<connection-string>"

Replace the <connection-string> placeholder value with your Atlas connection string.

### Local Deployment

If you're using a local Atlas deployment, your connection string follows this format, replacing <port-number> with the port for your local deployment.

javascript MONGODB_URI = "mongodb://localhost:<port-number>/?directConnection=true";

Note: Minimum Node.js Version Requirements

Node.js v20.x introduced the --env-file option. If you are using an older version of Node.js, add the dotenv package to your project, or use a different method to manage your environment variables.

In this section, you set up the environment for this tutorial.

  1. Create a directory to store your project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb.

console mkdir local-rag-mongodb cd local-rag-mongodb

  1. Create an interactive Python notebook.

The following command creates a notebook in the directory named local-rag.ipynb.

shell touch local-rag.ipynb

  1. Install and import dependencies.

Run the following command in your notebook:

python pip install --quiet --upgrade pymongo gpt4all sentence_transformers atlas-local-lib-py

  1. Create or reuse a local Atlas deployment.

Run the following code in your notebook to create a new local deployment or reuse an existing one with matching configuration. This also retrieves the connection string programmatically.

```python from atlas_local import LocalDeployment

deployment = LocalDeployment.get_or_create(name="local-atlas-deployment") MONGODB_URI = deployment.connection_string() ```

In this section, you set up the environment for this tutorial.

  1. Create a directory to store your project.

Run the following commands in your terminal to create a new directory named local-rag-mongodb.

console mkdir local-rag-mongodb cd local-rag-mongodb

  1. Create an interactive Python notebook.

The following command creates a notebook in the directory named local-rag.ipynb.

shell touch local-rag.ipynb

  1. Install and import dependencies.

Run the following command in your notebook:

python pip install --quiet --upgrade pymongo gpt4all sentence_transformers atlas-local-lib-py

  1. Create or reuse a local Atlas deployment.

Run the following code in your notebook to create a new local deployment or reuse an existing one with matching configuration. This also retrieves the connection string programmatically.

```python from atlas_local import LocalDeployment

deployment = LocalDeployment.get_or_create(name="local-atlas-deployment") MONGODB_URI = deployment.connection_string() ```

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](https://www.mongodb.com/docs/manual/sample-data/sample-airbnb.md#std-label-sample-airbnb database, which contains a collection called listingsAndReviews.

  1. Download the local embedding model.

This example uses the nomic-embed-text model from Ollama.

Run the following command to pull the embedding model:

console ollama pull nomic-embed-text

  1. Generate embeddings.

To encapsulate the logic for each piece of the implementation, create a few classes to coordinate and manage the services.

Create a file called OllamaAIService.cs, and paste the following code into it:

```csharp using Microsoft.Extensions.AI;

namespace MyCompany.RAG.Local;

public class OllamaAIService { private static readonly Uri OllamaUri = new("http://localhost:11434/"); private static readonly string EmbeddingModelName = "nomic-embed-text"; private static readonly OllamaEmbeddingGenerator EmbeddingGenerator = new OllamaEmbeddingGenerator(OllamaUri, EmbeddingModelName); private static readonly string ChatModelName = "mistral"; private static readonly OllamaChatClient ChatClient = new OllamaChatClient(OllamaUri, ChatModelName);

   public async Task&lt;float[]

(本文超出站点单文档体积上限,已截断)