For AI agents: a documentation index is available at https://www.mongodb.com/docs/llms.txt — markdown versions of all pages are available by appending .md to any URL path.
Docs Menu

Use Vertex AI Extensions for Natural Language MongoDB Queries

Note

Vertex AI Extensions are in preview and subject to change. Contact your Google Cloud representative to learn how to access this feature.

In addition to using Vertex AI with MongoDB Vector Search to implement RAG, you can use Vertex AI Extensions to further customize how you use Vertex AI models to interact with Atlas. In this tutorial, you create a Vertex AI Extension that allows you to query your data in Atlas in real-time by using natural language.

Diagram of workflow with Vertex AI Extensions and MongoDB Atlas

This tutorial uses the following components to enable natural language querying with Atlas:

  • Google Cloud Vertex AI SDK to manage AI models and enable custom extensions for Vertex AI. This tutorial uses the Gemini 1.5 Pro model.

  • Google Cloud Run to deploy a function that serves as an API endpoint between Vertex AI and Atlas.

  • OpenAPI 3 Specification for MongoDB API to define how natural language queries map to MongoDB operations. To learn more, see OpenAPI Specification.

  • Vertex AI Extensions to enable real-time interaction with Atlas from Vertex AI and configure how natural language queries are processed.

  • Google Cloud Secrets Manager to store your MongoDB API keys.

Note

For detailed code and set-up instructions, see the GitHub repository for this example.

Before you start, you must have the following:

In this section, you create a Google Cloud Run function that serves as an API endpoint between Vertex AI Extension and your Atlas cluster. The function handles authentication, connects to your Atlas cluster, and performs database operations based on the requests from Vertex AI.

1

In the Google Cloud console, open the Cloud Run page and click Write a function.

2
  1. Specify a function name and Google Cloud region where you want to deploy your function.

  2. Select the latest Python version available as a Runtime.

  3. In the Authentication section, select Allow unauthenticated invocations.

  4. Use the default values for the remaining settings, and then click Next.

For detailed configuration steps, refer to the Cloud Run documentation.

3

Paste the following code into their respective files:

4
  1. Rename the Entry Point as mongodb_crud.

  2. Click Deploy to deploy the function.

  3. Copy and store the HTTPS Endpoint for triggering the Cloud Function locally.

  4. Navigate to the Details page for the function and copy and store the service account name used by the function.

In this section, you create a Vertex AI Extension that enables natural language querying on your data in Atlas by using the Gemini 1.5 Pro model. This extension uses an OpenAPI specification and the Cloud Run function you created to map natural language to database operations and query your data in Atlas.

To implement this extension, you use an interactive Python notebook, which allows you to run Python code snippets individually. For this tutorial, you create a notebook named mongodb-vertex-ai-extension.ipynb in an Colab Enterprise environment.

In Vertex AI, click Extensions in the left navigation menu. Your new extension named MongoDB Vertex API Interpreter appears in the list of extensions.

The following examples demonstrates two different natural language queries you can use to query your data in Atlas: