Explore Developer Center's New Chatbot! MongoDB AI Chatbot can be accessed at the top of your navigation to answer all your MongoDB questions.

Join us at AWS re:Invent 2024! Learn how to use MongoDB for AI use cases.
MongoDB Developer
Atlas
plus
Sign in to follow topics
MongoDB Developer Centerchevron-right
Developer Topicschevron-right
Productschevron-right
Atlaschevron-right

Build Smart Applications With Atlas Vector Search and Google Vertex AI

Venkatesh Shanbhag, Ashwin Gangadhar4 min read • Published Sep 18, 2024 • Updated Sep 18, 2024
Google CloudVector SearchPythonAtlas
Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
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.
1{
2 "fields": [
3 {
4 "type":"vector",
5 "path":"vec",
6 "numDimensions":768,
7 "similarity": "cosine"
8 }
9 ]
10}

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.
1git 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:
1sudo apt update
2sudo apt install python3-pip
3sudo apt install git
4git --version
5pip3 --version
6cd MongoDB-VertexAI-Qwiklab
7pip3 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:
1streamlit run app.py
Screenshot of the application for uploading the documents
The application uses the PdfReader library to read the PDF file from the user and chunks the data into batches. Each chunk is converted into vectors using LangChain’s VertexAIEmbeddings libraries. The uploaded PDF is written into MongoDB Atlas in chunks, along with the vectors. The data can be viewed in vertexaiApp.chat-vec namespace documents.
Screenshot of the vectors being stored into MongoDB Atlas
To perform a search on the data, navigate to the Q&A tab on your application. Type in your query and press enter. The application will perform a vector search on MongoDB documents using the index created earlier. The application uses the chat-bison model to converse with the user.
Screenshot of the application for searching the PDF data from MongoDB using Atlas Vector Search

Conclusion

In this tutorial, you learned how to build a simple application using MongoDB Atlas and Google Cloud Vertex AI using LangChain to access MongoDB Atlas. Also, we have seen how to deploy the application on Google Cloud Compute instance and access it using the public static IP address allocated.
Try MongoDB Atlas for free in Google Cloud Marketplace using our pay-as-you-go model and take advantage of our simplified billing.
Top Comments in Forums
There are no comments on this article yet.
Start the Conversation

Facebook Icontwitter iconlinkedin icon
Rate this tutorial
star-empty
star-empty
star-empty
star-empty
star-empty
Related
Tutorial

Getting Started with MongoDB Atlas and Azure Functions using Node.js


Feb 03, 2023 | 8 min read
Quickstart

MongoDB With Bedrock Agent: Quick Tutorial


Jul 01, 2024 | 6 min read
News & Announcements

Transform Your AI Development Skills with the RAG to Riches Developer Quest!


Jul 10, 2024 | 1 min read
Article

Deploying MongoDB Atlas With Terraform with Azure


Jun 18, 2024 | 7 min read
Table of Contents
  • Before we begin