# Build Smart Applications With Atlas Vector Search and Google Vertex AI

The application development landscape is evolving very rapidly. Today, users crave intuitive, context-aware experiences that understand their intent and deliver relevant results even when queries aren't perfectly phrased, putting an end to the keyword-based search practices. This is where MongoDB Atlas and Google Cloud Vertex AI can help users build and deploy scalable and resilient applications. 

MongoDB Atlas Vector Search is a cutting-edge tool that indexes and stores high-dimensional vectors, representing the essence of your data. It allows you to perform lightning-fast similarity searches, retrieving results based on meaning and context. Google Vertex AI is a comprehensive AI platform that houses an abundance of pre-trained models and tools, including the powerful Vertex AI PALM. This language model excels at extracting semantic representations from text data, generating those crucial vectors that fuel MongoDB Atlas Vector Search. 

Vector Search can be useful in a variety of contexts, such as natural language processing and recommendation systems. It is a powerful technique that can be used to find similar data based on its meaning.

In this tutorial, we will see how to get started with MongoDB Atlas and Vertex AI. If you are new to MongoDB Atlas, refer to the documentation to get set up from Google Cloud Marketplace or use the Atlas registration page. 

## Before we begin

Make sure that you have the below prerequisites set up before starting to test your application.

1. MongoDB Atlas access, either by the registration page or from Google Cloud Marketplace
2. Access to Google Cloud Project to deploy and create a Compute Engine instance

## How to get set up

Let us consider a use case where we are loading sample PDF documents to MongoDB Atlas as vectors and deploying an application on Google Cloud to perform a vector search on the PDF documents. 

We will start with the creation of MongoDB Atlas Vector Search Index on the collection to store and retrieve the vectors generated by the Google Vertex AI PALM model. To store and access vectors on MongoDB Atlas, we need to create an Atlas Search index. 

### Create an Atlas Search index 

1. Navigate to the **Database Deployments** page for your project.
2. Click on **Create Database.** Name your Database **vertexaiApp** and your collection **chat-vec**.
3. Click **Atlas Search** from the Services menu in the navigation bar.
4. Click **Create Search Index** and select **JSON Editor** under **Atlas Vector Search**. Then, click **Next.**
5. In the **Database and Collection** section, find the database **vertexaiApp**, and select the **chat-vec** collection.
6. Replace the default definition with the following index definition and then click **Next**. Click on **Create Search index** on the review page.

```json
{
    "fields": 
        {
            "type":"vector",
            "path":"vec",
            "numDimensions":768,
            "similarity": "cosine"
        }
    ]
}
```

### Create a Google Cloud Compute instance

We will create a [Google Cloud virtual machine instance to run and deploy the application. The Google Cloud VM can have all the default configurations. To begin, log into your Google Cloud Console and perform the following steps:

- In the Google Cloud console, click on **Navigation menu > Compute Engine.**
- Create a new VM instance with the below configurations:
    - **Name:** vertexai-chatapp
    - **Region**: region near your physical location
    - **Machine configurations:**
        - Machine type: High Memory, n1-standard-1
- Boot disk: Click on **CHANGE**
    - Increase the size to 100 GB.
    - Leave the other options to default (Debian).
- Access: Select **Allow full access** to all Cloud APIs.
- Firewall: Select all.
- Advanced options:
    - Networking: Expand the default network interface.
    - For External IP range: Expand the section and click on **RESERVE STATIC EXTERNAL IP ADDRESS**. This will help users to access the deployed application from the internet.
    - Name your IP and click on **Done**.
- Click on **CREATE** and the VM will be created in about two to three minutes.

### Deploy the application

Once the VM instance is created, SSH into the VM instance and clone the GitHub repository.

```
git clone https://github.com/mongodb-partners/MongoDB-VertexAI-Qwiklab.git
```

The repository contains a script to create and deploy a Streamlit application to transform and store PDFs in MongoDB Atlas, then search them lightning-fast with Atlas Vector Search. The app.py script in the repository uses Python and LangChain to leverage MongoDB Atlas as our data source and Google Vertex AI for generating embeddings. 

We start by setting up connections and then utilize LangChain’s ChatVertexAI and Google's Vertex AI embeddings to transform the PDF being loaded into searchable vectors. Finally, we constructed the Streamlit app structure, enabling users to input queries and view the top retrieved documents based on vector similarity.

Install the required dependencies on your virtual machine using the below commands:

```bash
sudo apt update
sudo apt install python3-pip
sudo apt install git
git --version
pip3 --version
cd MongoDB-VertexAI-Qwiklab
pip3 install -r requirements.txt
```

Once the requirements are installed, you can run the application using the below command. Open the application using the public IP of your VM and the port mentioned in the command output:

```bash
streamlit run app.py
```

 using our pay-as-you-go model and take advantage of our simplified billing.

  [1]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/blted03916b8de19681/65a7ede75d51352518fb89d6/1.png
  [2]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltacbaf40c14ecccbc/65a7ee01cdbb961f8ac4faa4/2.png
  [3]: https://images.contentstack.io/v3/assets/blt39790b633ee0d5a7/bltc7a3b3c7a2db880c/65a7ee187a1dd7b984e136ba/3.png