Simplify RAG application with MongoDB Atlas and Amazon Bedrock
Tags: database, tutorial, machinelearning, mongodb
Published: 2024-05-30 03:54:44

By fetching data from the organization’s internal or proprietary sources, Retrieval Augmented Generation (RAG) extends the capabilities of FMs to specific domains, without needing to retrain the model. It is a cost-effective approach to improving model output so it remains relevant, accurate, and useful in various contexts.

[Knowledge Bases for Amazon Bedrock](https://aws.amazon.com/bedrock/knowledge-bases/) is a fully managed capability that helps you implement the entire RAG workflow from ingestion to retrieval and prompt augmentation without having to build custom integrations to data sources and manage data flows. With MongoDB Atlas vector store integration, you can build RAG solutions to securely connect your organization’s private data sources to FMs in Amazon Bedrock. 

Let's see how the MongDB Atlas integration with Knowledge Bases can simplify the process of building RAG applications.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dfd8og9reoyppi81rib5.png)

## Configure MongoDB Atlas

MongoDB Atlas cluster creation on [AWS](https://www.mongodb.com/docs/atlas/reference/amazon-aws/) process is [well documented](https://www.mongodb.com/docs/atlas/tutorial/create-new-cluster/). Here are the high level steps:

- This integration requires an [Atlas cluster tier of at least M10](https://www.mongodb.com/docs/manual/reference/operator/aggregation/listSearchIndexes/). During cluster creation, choose an **M10 dedicated** cluster tier. 
- Create a [database](https://www.mongodb.com/docs/atlas/atlas-ui/databases/#create-a-database) and [collection](https://www.mongodb.com/docs/atlas/atlas-ui/collections/#create-a-collection). 
- For authentication, [create a database user](https://www.mongodb.com/docs/atlas/security-add-mongodb-users/#add-database-users). Select **Password** as the **Authentication** Method. Grant the [Read and write to any database](https://www.mongodb.com/docs/atlas/mongodb-users-roles-and-privileges/#mongodb-atlasrole-Read-and-write-to-any-database) role to the user.
- Modify the **IP Access List** – add IP address `0.0.0.0/0` to allow access from anywhere. For production deployments, [AWS PrivateLink](https://docs.aws.amazon.com/vpc/latest/privatelink/what-is-privatelink.html) is the recommended way to have Amazon Bedrock establish a [secure connection to your MongoDB Atlas cluster](https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base-setup.html).

## Create the Vector Search index in MongoDB Atlas

Use the below definition to [create a Vector Search index](https://www.mongodb.com/docs/atlas/atlas-vector-search/create-index/#std-label-avs-create-index). 

```json
{
  "fields": [
    {
      "numDimensions": 1536,
      "path": "AMAZON_BEDROCK_CHUNK_VECTOR",
      "similarity": "cosine",
      "type": "vector"
    },
    {
      "path": "AMAZON_BEDROCK_METADATA",
      "type": "filter"
    },
    {
      "path": "AMAZON_BEDROCK_TEXT_CHUNK",
      "type": "filter"
    }
  ]
}
```
- `AMAZON_BEDROCK_TEXT_CHUNK` – Contains the raw text for each data chunk. We are using `cosine` similarity and embeddings of size `1536` (we will choose the embedding model accordingly - in the the upcoming steps).
- `AMAZON_BEDROCK_CHUNK_VECTOR` – Contains the vector embedding for the data chunk.
- `AMAZON_BEDROCK_METADATA` – Contains additional data for source attribution and rich query capabilities.

## Configure the Knowledge Base in Amazon Bedrock

Create an [AWS Secrets Manager secret](https://docs.aws.amazon.com/secretsmanager/latest/userguide/create_secret.html) to securely store the MongoDB Atlas database user credentials.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/whqxwpl0h2nzu6qng6nr.png)

[Create an Amazon Simple Storage Service (Amazon S3) storage bucket](https://docs.aws.amazon.com/AmazonS3/latest/userguide/creating-bucket.html) and upload any document(s) of your choice - [Knowledge Base supports multiple file formats](https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base-ds.html) (including text, HTML, and CSV). Later, you will use the knowledge base to ask questions about the contents of these documents. 

Navigate to the [Amazon Bedrock console](https://console.aws.amazon.com/bedrock?trk=24163938-a3b4-48b3-b5c2-be66044dcad6&sc_channel=el) and start configuring the knowledge base. In step 2, choose the S3 bucket you created earlier:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4i02bwek9wyk5bvixv2i.png)

Select **Titan Embeddings G1 – Text** embedding model MongoDB Atlas as the vector database.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkasm0qcvqyiej7wmogh.jpeg)

Enter the basic information for the MongoDB Atlas cluster along with the `ARN` of the AWS Secrets Manager secret you had created earlier. In the **Metadata** field mapping attributes, enter the vector store specific details. They should match the vector search index definition you used earlier.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/22yi9yww07zgf5mbq0aw.jpeg)

Once the knowledge base is created, you need to [synchronise the data source](https://docs.aws.amazon.com/bedrock/latest/userguide/knowledge-base-ingest.html) (S3 bucket data) with the MongoDB Atlas vector search index.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lkrbl1adt5o7g9qx2nc9.png)

Once that's done, you can check the MongoDB Atlas collection to verify the data. As per index definition, the vector embeddings have been stored in `AMAZON_BEDROCK_CHUNK_VECTOR` along with the text chunk and metadata in `AMAZON_BEDROCK_TEXT_CHUNK` and `AMAZON_BEDROCK_METADATA`, respectively.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xsgldowfbnuiaricl23s.jpg)

## Query the knowledge base

You can now ask question about your documents by querying the knowledge base - select **Show source details** to see the chunks cited for each footnote.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7sgxlw05a3q4t0ducp64.jpeg)

You can also change the foundation model. For example, I switched to Claude 3 Sonnet. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3bfqzpqnyo1gq79qkpwc.png)

### Use Retrieval APIs to integrate knowledge base with applications

To build RAG applications on top of Knowledge Bases for Amazon Bedrock, you can use the [RetrieveAndGenerate API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_RetrieveAndGenerate.html) which allows you to query the knowledge base and get a response.

If you want to further customize your RAG solutions, consider using the [Retrieve API](https://docs.aws.amazon.com/bedrock/latest/APIReference/API_agent-runtime_Retrieve.html), which returns the semantic search responses that you can use for the remaining part of the RAG workflow.

### More configurations

You can further customize your knowledge base queries using a different search type, additional filter, different prompt, etc.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4s4oy7xbx9hh4dxso429.png)

## Conclusion

Thanks to the MongoDB Atlas integration with Knowledge Bases for Amazon Bedrock, most of the heavy lifting is taken care of. Once the vector search index and knowledge base are configured, you can incorporate RAG into your applications. Behind the scenes, Amazon Bedrock will convert your input (prompt) into embeddings, query the knowledge base, augment the FM prompt with the search results as contextual information and return the generated response.

Happy building!