# Tutorial: Building a RAG Chat App with MongoDB Atlas Document Store

Learn how to build a chat system running on your data in MongoDB Atlas.

***

- **Level**: Beginner
- **Time to complete**: 20 minutes
- **Prerequisites**:
  - A basic knowledge of pipelines and indexes in <ProductShortName />. For more information, see [Pipelines](/docs/concepts/about-pipelines/about-pipelines.mdx) and [Indexes](/docs/concepts/indexes/indexes.mdx).
  - Understanding of document stores. For details, see [Document Stores](/docs/concepts/document-stores/document-stores.mdx). 
  - A workspace where you'll create the pipeline. For help, see [Quick Start Guide](/docs/getting-started/quick-start-guide.mdx).
  - OpenAI API key as this pipeline uses GPT-4o. For details on how to connect <ProductShortName /> to OpenAI, see [Use OpenAI Models](/docs/how-to-guides/designing-your-pipeline/use-hosted-models-and-services/use-openai-models.mdx).
  - A MongoDB account with a cluster containing the `mflix` sample dataset. For help, see MongoDB Atlas documentation:
    - [Create an account](https://www.mongodb.com/docs/atlas/tutorial/create-atlas-account/)
    - [Deploy a free cluster](https://www.mongodb.com/docs/atlas/tutorial/deploy-free-tier-cluster/)
    - [Create a connection string](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/quick-start/create-a-connection-string/)
    - [Load sample data](https://www.mongodb.com/docs/atlas/sample-data/)
    - [Sample mflix dataset](https://www.mongodb.com/docs/atlas/sample-data/sample-mflix/)
- **Goal**: After completing this tutorial, you will have built a chat application that runs on the sample movie dataset in MongoDB Atlas. It uses both full text search and semantic search to retrieve relevant documents.

***

## Connect <ProductShortName/> to MongoDB Atlas

You need the MongoDB Atlas connection string.

1. Log in to MongoDB Atlas and open your project overview.
2. In the _Application Development_ section click **Get connection string**, and copy the connection string in step 3. 
3. Go to <ProductName/>, click your profile icon in the top right corner, and choose _Settings_.

<ClickableImage
  src="/img/getting-started/settings_option.png"
  alt="The connections menu"
  size="large"
/>

4. Go to *Workspace>Integrations* and find MongoDB.
5. Click **Connect** next to MongoDB.
5. Paste your MongoDB connections string replacing `db_password` with your database password and click **Connect**.

**Result**: <ProductName/> is connected to your MongoDB Atlas database, which means that <ProductShortName /> can access data in your MongoDB Atlas database.

## Create a Search Index for Your MongoDB Collection

### Create Full Text Search Index

First, create a full text search index for keyword searches. For details and other methods of creating the index, see [Atlas Search](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/fundamentals/atlas-search/) in MongoDB Atlas documentation.

1. In MongoDB Atlas, open your cluster, and click the **Atlas Search** tab.
2. Click **Create Search Index**.

<ClickableImage
  src="/img/tutorials/mongodb_create_index.png"
  alt="The Create Index option in MongoDB Atlas UI."
  size="standard"
/>

3. Choose *Atlas Search* as the index type.
4. Type `text_index` as the index name.
5. Choose _embedded_movies_ as the collection.
6. Choose **Visual Editor** as the configuration method and click **Next**.
7. Click **Create Search Index**. 

**Result**: You have created a full text search index called `text_search`. It's listed under Atlas Search in your cluster view. You may need to wait a while until Atlas builds your index and its status changes to _Ready_.

<ClickableImage
  src="/img/tutorials/mongodb_text_index.png"
  alt="Text index in MongoDB Atlas UI"
  size="standard"
/>

### Add Field Mapping

1. On the _Atlas Search_ tab, click **More Actions** next to your text index and choose _Edit With Visual Editor_.
2. On the _Index Overview_ tab, scroll down to _Field Mappings_, and choose **Add Field Mapping**.

<ClickableImage
  src="/img/tutorials/mongodb_add_field_mapping.png"
  alt="The field mappings option in mongodb atlas ui"
  size="standard"
/>

3. Find _Field Name_, choose _fullplot_, and click **Add**.
4. Save your settings. 

**Result**: You added `fullplot` as your index field. It's now showing under Index Fields in the Atlas Search tab.

# Create a Vector Search Index

Now, you'll create a vector search index for semantic searches. You can also follow MongoDB Atlas documentation on [Atlas Vector Search](https://www.mongodb.com/docs/drivers/php/laravel-mongodb/current/fundamentals/vector-search/) to complete this step.

1. In MongoDB Atlas, on the _Atlas Search_ tab, click **CREATE SEARCH INDEX**.
2. Choose _Vector Search_ as its type.
3. Leave `vector_index` as the name.
4. Choose _embedded_movies_ as the collection.
5. Choose **Visual Editor **as the configuration method and click **Next**.
6. Make sure `plot_embedding` is chosen as the path of the Vector Field.

<ClickableImage
  src="/img/tutorials/mongodb_vector_field.png"
  alt="The path field with plot_embedding filled in"
  size="standard"
/>

7. Choose the similarity method and click **Next**.
8. Review your vector index configuration and click **Create Vector Search Index**.

**Result**: You have create a vector search index on the plot_embedding field. The index is listed under the Atlas Search tab. You may need to wait a while until Atlas builds it and the index is ready.

<ClickableImage
  src="/img/tutorials/mongodb_two_indexes.png"
  alt="The Alas dashboard with both indexes visible"
  size="standard"
/>

## Create a RAG Chat Application

1. In <ProductName/> go to Pipeline Templates.
2. Choose the _Conversational_ category and find the _MongoDB Atlas RAG Chat_ template. 
3. Hover over the template and click **Use Template**.
4. Leave the default name and click **Create Pipeline**. The template opens in Builder. You can see that it uses `MongoDBAtlasDocumentStore` as the document store. 
   :::tip Changing the Model
   This pipeline uses the gpt-4o model by default. You can change the model on the `qa_llm` component card. Click the **Model** parameter and choose a different model from the list. 
   <ClickableImage
     src="/img/tutorials/change-model.gif"
     alt="The model selection on the LLM component card"
     size="standard"
   />

   You can also update the prompt here.
   :::
5. In Builder, click **Deploy** and confirm. You can test a deployed pipeline in the Playground or share it with others to gather their feedback.

**Result**: Congratulations! You have created a RAG chat application that uses gpt-4o and runs on the sample dataset in your MongoDB Atlas database. You can now try it out in Playground. For instructions, see [Testing Your Pipeline](/docs/how-to-guides/searching-with-your-pipeline/run-a-search.mdx).

:::info Indexing status
Since MongoDB Atlas is an external document store, you won't be able to view indexing status or see which files are part of your index. However, you can access the logs for insights into the indexing process.
:::
