# Launch a Fully Managed RAG Workflow With MongoDB Atlas and Amazon Bedrock

## Introduction

MongoDB Atlas is now natively integrated with Amazon Bedrock Knowledge Base, making it even easier to build generative AI applications backed by enterprise data. 

Amazon Bedrock, Amazon Web Services’ (AWS) managed cloud service for generative AI, empowers developers to build applications on top of powerful foundation models like Anthropic's Claude, Cohere Embed, and Amazon Titan. By integrating with Atlas Vector Search, Amazon Bedrock enables customers to leverage the vector database capabilities of Atlas to bring up-to-date context to Foundational Model outputs using proprietary data. 

With the click of a button (see below), Amazon Bedrock now integrates MongoDB Atlas as a vector database into its fully managed, end-to-end retrieval-augmented generation (RAG) workflow, negating the need to build custom integrations to data sources or manage data flows. 

Companies using MongoDB Atlas and Amazon Bedrock can now rapidly deploy and scale generative AI apps grounded in the latest up-to-date and accurate enterprise data. For enterprises with the most demanding privacy requirements, this capability is also available via AWS PrivateLink (more details at the bottom of this article).

## What is retrieval-augmented generation?

One of the biggest challenges when working with generative AI is trying to avoid hallucinations, or erroneous results returned by the foundation model (FM) being used. The FMs are trained on public information that gets outdated quickly and the models cannot take advantage of the proprietary information that enterprises possess.

One way to tackle hallucinating FMs is to supplement a query with your own data using a workflow known as retrieval-augmented generation, or RAG. In a RAG workflow, the FM will seek specific data — for instance, a customer's previous purchase history — from a designated database that acts as a “source of truth” to augment the results returned by the FM. For a generative AI FM to search for, locate, and augment its responses, the relevant data needs to be turned into a vector and stored in a vector database.

## How does the Knowledge Base integration work?

Within Amazon Bedrock, developers can now “click to add” MongoDB Atlas as a knowledge base for their vector data store to power RAG.

In the workflow, a customer chooses two different models: an embedding model and a generative model. These models are then orchestrated and used by Bedrock Agents during the interaction with the knowledge base — in this case, MongoDB Atlas.

Bedrock reads your text data from an S3 bucket, chunks the data, and then uses the embedding model chosen by the user to create the vector embeddings, storing these text chunks, embeddings, and related metadata in MongoDB Atlas’ vector database. An Atlas vector search index is also created as part of the setup for querying the vector embeddings.

 combines operational, vector, and metadata in a single platform, making it an ideal knowledge base for Amazon Bedrock users who want to augment their generative AI experiences while also simplifying their generative AI stack.

In addition, MongoDB Atlas gives developers the ability to set up dedicated infrastructure for search and vector search workloads, optimizing compute resources to scale search and database independently. 

## Solution architecture

 to populate our knowledge base. Please download the PDF (by clicking on “Read Whitepaper” or “Email me the PDF”). Alternatively, you can download it from the GitHub repository. Once you have the PDF, upload it into an S3 bucket for hosting. (Note the bucket name as we will use it later in the article.) 

## Prerequisites

* MongoDB Atlas account
* AWS account 

## Implementation steps

### Atlas Cluster and Database Setup 

* Login or Signup][3] to MongoDB Atlas 
* [Setup][4] the MongoDB Atlas cluster with a M10 or greater configuration. *Note M0 or free cluster will not support this setup.*
* Setup the [database user][5] and [Network access][6].
* Copy the [connection string][7].
* [Create][8] a database and collection

![The screenshot shows the navigation of creating a database in MongoDB Atlas.][9]

### Atlas Vector Search index

Before we create an Amazon Bedrock knowledge base (using MongoDB Atlas), we need to create an Atlas Vector Search index.

* In the MongoDB Atlas Console, navigate to your cluster and select the _Atlas Search_ tab. 

![Atlas console navigation to create the search index][10]

* Select _Create Search Index_, select _Atlas Vector Search_, and select _Next_.

![The screenshot shows the MongoDB Atlas Search Index navigation.][11]

* Select the database and the collection where the embeddings are stored.

![MongoDB Atlas Search Index navigation][12]    

*  Supply the following JSON in the index definition and click _Next_, confirming and creating the index on the next page.

    ```
   {
      "fields": [
        {
          "numDimensions": 1536,
          "path": "bedrock_embedding",
          "similarity": "cosine",
          "type": "vector"
        },
        {
          "path": "bedrock_metadata",
          "type": "filter"
        },
        {
          "path": "bedrock_text_chunk",
          "type": "filter"
        }
      ]
    }
    ```
![The screenshot shows the MongoDB Atlas Search Index navigation][13]

Note: The fields in the JSON are customizable but should match the fields we configure in the Amazon Bedrock AWS console. If your source content contains [filter metadata, the fields need to be included in the JSON array above in the same format: `{"path": "<attribute_name>","type":"filter"}`.

### Amazon Bedrock Knowledge Base 

* In the AWS console, navigate to Amazon Bedrock, and then click _Get started_.

 orchestrate interactions between foundation models, data sources, software applications, and user conversations. In addition, agents automatically call APIs to take actions and invoke knowledge bases to supplement information for these actions

* In the AWS Bedrock console, create an Agent. 

* AWS docs about MongoDB Bedrock integration
* MongoDB Vector Search
* Bedrock User Guide
* MongoDB Atlas on AWS Marketplace

  [1]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt511ed709f8f6d72c/66323fa5ba17b0c937cb77a0/1_image.png
  [2]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt33b9187a37c516d8/66323fa65319a05c071f59a6/2_image.png
  [3]: https://www.mongodb.com/docs/guides/atlas/account/
  [4]: https://www.mongodb.com/docs/guides/atlas/cluster/
  [5]: https://www.mongodb.com/docs/guides/atlas/db-user/
  [6]: https://www.mongodb.com/docs/guides/atlas/network-connections/
  [7]: https://www.mongodb.com/docs/guides/atlas/connection-string/
  [8]: https://www.mongodb.com/basics/create-database#using-the-mongodb-atlas-ui
  [9]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt6fe6d13267c2898e/663bb48445868a5510839ee6/27_bedrock.png
  [10]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt81882c0004f72351/66323fa5368bfca5faff012c/3_image.png
  [11]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt7f47b6bde3a5c6e0/66323fa6714a1b552cb74ee7/4_image12.png
  [12]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt2a5dcee8c6491baf/66323fa63c98e044b720dd9f/5_image.png
  [13]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt9fec5112de72ed56/663bb5552ff97d53f17030ad/28_bedrock.png
  [14]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt1fd932610875a23b/66323fa65b8ef39b7025bd85/7_image.png
  [15]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltbc8d532d6dcd3cc5/66323fa6ba17b06b7ecb77a8/8_image.png
  [16]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt0a24ae8725a51d07/66323fa6e664765138d445ee/9_image.png
  [17]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt88d2e77e437da43f/66323fa6e664767b99d445ea/10_image.png
  [18]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltad5b66cb713d14f7/66323fa63c98e0b22420dd97/11_image.png
  [19]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt0da5374bed3cdfc4/66323fa6e66476284ed445e6/12_image.png
  [20]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt1a5f530f0d77cb25/66323fa686ffea3e4a8e4d1e/13_image.png
  [21]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt3c5a3bbfd93fc797/66323fa6ba17b003bccb77a4/14_image.png
  [22]: https://github.com/mongodb-partners/mongodb_atlas_as_aws_bedrock_knowledge_base
  [23]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt17ef52eaa92859b7/66323fa6f5bf2dff3c36e840/15_image.png
  [24]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt039df3ec479a51b3/66323fa6dafc457afab1d9ca/16_image18.png
  [25]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltff47aa531f588800/66323fa65319a08f491f59aa/17_image.png
  [26]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltb886edc28380eaad/66323fa63c98e0f4ca20dda1/18_image.png
  [27]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt94659b32f1eedb41/66323fa657623318c954d39d/19_image.png
  [28]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltce5243d13db956ac/66323fa6599d112fcc850538/20_image.png
  [29]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt5cac340ac53e5630/66323fa6d63d2215d9b8ce1e/21_image.png
  [30]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltecb09d22b3b99731/66323fa586ffea4e788e4d1a/22_image.png
  [31]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltcc59300cd4b46845/66323fa6deafa962708fcb0c/23_image.png
  [32]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blt381ef4a7a68c7b40/66323fa54124a57222a6c45d/24_image.png
  [33]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blte3d2b90f86472bf1/66323fa5ba17b0c29fcb779c/25_image.png