This project provides an AI-powered agent for grocery shopping, leveraging MongoDB for data storage and Google Vertex AI for semantic search and embeddings.
Check out the Medium tutorial for more information.
- Semantic product search using MongoDB Atlas Vector Search and Vertex AI embeddings
- Add products to user carts in MongoDB
- Python 3.10+
- Access to Google Cloud Gemini API
- Access to a MongoDB Atlas cluster (instructions below)
- Required Python packages (instructions below)
- Google ADK Python installed (instructions below)
- Create a free MongoDB Atlas cluster
- Go to MongoDB Atlas and sign up for a free account.
- Click "Build a Database" and choose the free tier (Shared, M0).
- Select your preferred cloud provider and region, then click "Create".
- Create a database user with a username and password.
- Add your IP address to the IP Access List (or allow access from anywhere for development).
- Once the cluster is created, click "Connect" and choose "Connect your application" to get your connection string. Use this string for the
CONNECTION_STRING
environment variable in the next steps.
- Clone the repository
git clone https://github.com/mongodb-developer/MongoDB-ADK-Agents.git
cd MongoDB-VertexAI-ADK
- Load the Dataset into MongoDB Atlas
Import the provided dataset into your MongoDB database using the following command (replace placeholders as needed):
mongoimport --uri "$CONNECTION_STRING" --db "$DATABASE_NAME" --collection "$COLLECTION_NAME" --type csv --headerline --file mongodb-groceries-agent/dataset.csv
- Generate Embeddings for the Inventory
After loading the data, you need to generate vector embeddings for each product. Run the following script:
python mongodb-groceries-agent/create-embeddings.py
This will process all products in the collection and add/update the embedding field required for semantic search.
- Build a Vector Search Index for the Inventory
Open the Search and Vector Search tab in the left sidebar in Atlas and create a vector search index on the inventory collection with the following definition:
{
"fields": [
{
"numDimensions": 3072,
"path": "gemini_embedding",
"similarity": "cosine",
"type": "vector"
}
]
}
- Install the Python dependencies
pip install -r requirements.txt
- Install the ADK CLI
Follow the official ADK installation instructions or run:
pip install google-adk
- Set environment variables
Set the following environment variables in a .env
file:
GOOGLE_GENAI_USE_VERTEXAI=FALSE
# Follow the guide: https://www.mongodb.com/docs/guides/atlas/connection-string/
CONNECTION_STRING="Your MongoDB connection string"
# Follow the guide: https://cloud.google.com/api-keys/docs/create-manage-api-keys
GOOGLE_API_KEY="Your Google Cloud API key"
- Run the agent using ADK
Navigate to the mongodb-groceries-agent
directory and run:
adk web
- Open the web server running at
http://127.0.0.1:8000
and start using the application!
- The agent will start and be ready to handle product search and cart operations.
- You can extend the agent with new tools or integrate it into a larger application.
mongodb-groceries-agent/agent.py
: Main agent logicmongodb-groceries-agent/create-embeddings.py
: Utility for creating embeddingsmongodb-groceries-agent/dataset.csv
: Example dataset
- Ensure your Google Cloud and MongoDB credentials are valid and have the necessary permissions.
- For local development, you may want to use a virtual environment.
- The ADK CLI is required for running and managing agents.
See LICENSE for details.