|
1 | | -# MongoDB VertexAI Groceries Agent |
| 1 | +# Workshop Instructions |
2 | 2 |
|
3 | | -This project provides an AI-powered agent for grocery shopping, leveraging MongoDB for data storage and Google Vertex AI for semantic search and embeddings. |
| 3 | +Welcome to the **Google ADK and MongoDB Atlas** workshop! <span aria-hidden="true">🎉</span> |
| 4 | +In this workshop, you’ll build a **Grocery Shopping AI agent** step by step. Each exercise combines theory with hands-on practice so you can learn concepts and immediately apply them. |
4 | 5 |
|
5 | | -Check out the [Medium tutorial](https://medium.com/google-cloud/build-a-python-ai-agent-in-15-minutes-with-google-adk-and-mongodb-atlas-vector-search-groceries-b6c4af017629) for more information. |
| 6 | +## Exercise 0: Browse the Database |
6 | 7 |
|
7 | | -## Features |
8 | | -- Semantic product search using MongoDB Atlas Vector Search and Vertex AI embeddings |
9 | | -- Add products to user carts in MongoDB |
| 8 | +1. On the left-hand sidebar, click on the green leaf icon to open the MongoDB extension. |
| 9 | +2. From the extension page, click on **Groceries Database** to connect to the MongoDB database. |
| 10 | +3. Explore the **grocery_store** database and the **inventory** collection provided. |
| 11 | +4. Open a few documents and notice their structure. |
10 | 12 |
|
11 | | -## Prerequisites |
12 | | -- Python 3.10+ |
13 | | -- Access to Google Cloud Gemini API |
14 | | -- Access to a MongoDB Atlas cluster (instructions below) |
15 | | -- Required Python packages (instructions below) |
16 | | -- Google ADK Python installed (instructions below) |
| 13 | +**<span aria-hidden="true">👉</span> Question to consider:** |
| 14 | +1. What information about the products stands out to you? |
| 15 | +2. How could this data be useful to a shopping agent? |
| 16 | +3. Are there any unusual fields in the documents? |
17 | 17 |
|
18 | | -## Loading the Dataset and Generating Embeddings |
| 18 | +## Exercise 1: Initialize the Agent |
19 | 19 |
|
20 | | -1. **Create a free MongoDB Atlas cluster** |
| 20 | +In this step, you’ll create your first AI Agent with ADK. At this stage, the agent won’t have any tools — which means it won’t be able to do much yet. This will demonstrate why tools are essential. |
21 | 21 |
|
22 | | -- Go to [MongoDB Atlas](https://mongodb.com/try?utm_campaign=devrel&utm_source=github&utm_medium=cta&utm_content=google-cloud-adk-grocery-agent&utm_term=stanimira.vlaeva) and sign up for a free account. |
23 | | -- Click "Build a Database" and choose the free tier (Shared, M0). |
24 | | -- Select your preferred cloud provider and region, then click "Create". |
25 | | -- Create a database user with a username and password. |
26 | | -- Add your IP address to the IP Access List (or allow access from anywhere for development). |
27 | | -- 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. |
| 22 | +1. Open the file `mongodb_groceries_agent/agent.py`. |
28 | 23 |
|
29 | | -2. **Clone the repository** |
| 24 | +1. You’ll see a few Python imports. You’ll use these later to implement the tools. You'll also see a placeholder for a passkey: |
30 | 25 |
|
31 | | -```bash |
32 | | -git clone https://github.com/mongodb-developer/MongoDB-ADK-Agents.git |
33 | | -cd MongoDB-VertexAI-ADK |
34 | | -``` |
| 26 | + ``` |
| 27 | + PASSKEY = "<ASK YOUR INSTRUCTOR FOR THE PASSKEY>" |
| 28 | + ``` |
35 | 29 |
|
36 | | -3. **Load the Dataset into MongoDB Atlas** |
| 30 | + Ask your instructor for the passkey and replace the placeholder with it. This passkey authenticates you to the Google API for this workshop, so you don’t need to provide your own API key—we’ve created one for you. |
37 | 31 |
|
38 | | -Import the provided dataset into your MongoDB database using the following command (replace placeholders as needed): |
| 32 | +1. With the API key in place, you’re ready to create your first agent. Add the following code to the file: |
39 | 33 |
|
40 | | -```bash |
41 | | -mongoimport --uri "$CONNECTION_STRING" --db "$DATABASE_NAME" --collection "$COLLECTION_NAME" --type csv --headerline --file mongodb_groceries_agent/dataset.csv |
42 | | -``` |
| 34 | + ```python |
| 35 | + root_agent = Agent( |
| 36 | + model="gemini-2.5-flash", # The LLM your agent will use |
| 37 | + name="grocery_shopping_agent",# A name for your agent |
| 38 | + instruction="", # You’ll define the agent’s instructions later |
| 39 | + tools=[ # Empty for now; you’ll add tools later |
| 40 | + # e.g. product search or add-to-cart |
| 41 | + ] |
| 42 | + ) |
| 43 | + ``` |
43 | 44 |
|
44 | | -4. **Generate Embeddings for the Inventory** |
| 45 | + Explanation of each field: |
45 | 46 |
|
46 | | -After loading the data, you need to generate vector embeddings for each product. Run the following script: |
| 47 | + * **model** — The LLM powering the agent (here, Gemini 2.5 Flash). |
| 48 | + * **name** → A unique identifier for your agent instance. |
| 49 | + * **instruction** → A system message that defines how the agent should behave (you’ll fill this in later). |
| 50 | + * **tools** → Python functions that the agent can call (currently empty). |
47 | 51 |
|
48 | | -```bash |
49 | | -python mongodb_groceries_agent/create-embeddings.py |
50 | | -``` |
| 52 | +1. Run the following command in the terminal to start the ADK development UI: |
51 | 53 |
|
52 | | -This will process all products in the collection and add/update the embedding field required for semantic search. |
| 54 | + ``` |
| 55 | + adk web |
| 56 | + ``` |
53 | 57 |
|
54 | | -5. **Build a Vector Search Index for the Inventory** |
| 58 | + You should see: |
55 | 59 |
|
56 | | -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: |
| 60 | + ``` |
| 61 | + INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) |
| 62 | + ``` |
57 | 63 |
|
58 | | -```bash |
59 | | -{ |
60 | | - "fields": [ |
61 | | - { |
62 | | - "numDimensions": 3072, |
63 | | - "path": "gemini_embedding", |
64 | | - "similarity": "cosine", |
65 | | - "type": "vector" |
66 | | - } |
67 | | - ] |
68 | | -} |
69 | | -``` |
| 64 | +1. Hold CMD (Mac) or CTRL (Windows/Linux) and click on the link: http://127.0.0.1:8000. |
70 | 65 |
|
71 | | -## Setup |
| 66 | + This opens the development UI where you can chat with your agent. |
72 | 67 |
|
73 | | -1. **Install the Python dependencies** |
| 68 | +1. Test your Agent |
74 | 69 |
|
75 | | -```bash |
76 | | -pip install -r requirements.txt |
77 | | -``` |
| 70 | + Try asking your agent: |
78 | 71 |
|
79 | | -2. **Install the ADK CLI** |
| 72 | + ``` |
| 73 | + Find me sourdough bread in the inventory. |
| 74 | + ``` |
80 | 75 |
|
81 | | -Follow the [official ADK installation instructions](https://google.github.io/adk-docs/get-started/installation/) or run: |
| 76 | + **What happens?** |
| 77 | + Since the agent doesn’t have any tools yet, it cannot actually access the database. Instead, it might: |
| 78 | + - Make up a product that doesn’t exist in the database. |
| 79 | + - Ask you follow up questions about which inventory you're referring to. |
| 80 | + - Attempt to search the web for an answer. |
82 | 81 |
|
83 | | -```bash |
84 | | -pip install google-adk |
85 | | -``` |
| 82 | + Neither of these behaviors is desirable — we want the agent to only use our inventory. |
86 | 83 |
|
87 | | -3. **Set environment variables** |
| 84 | + **<span aria-hidden="true">👉</span> Discussion point:** |
| 85 | + What risks do you see if an agent makes up products or fetches information from outside sources instead of the inventory? |
88 | 86 |
|
89 | | -Set the following environment variables in a `.env` file: |
90 | | - |
91 | | -```bash |
92 | | -GOOGLE_GENAI_USE_VERTEXAI=FALSE |
93 | | - |
94 | | -# Follow the guide: https://www.mongodb.com/docs/guides/atlas/connection-string/ |
95 | | -CONNECTION_STRING="Your MongoDB connection string" |
96 | | -# Follow the guide: https://cloud.google.com/api-keys/docs/create-manage-api-keys |
97 | | -GOOGLE_API_KEY="Your Google Cloud API key" |
98 | | -``` |
99 | | - |
100 | | -5. **Run the agent using ADK** |
101 | | - |
102 | | -Navigate to the `mongodb_groceries_agent` directory and run: |
103 | | - |
104 | | -```bash |
105 | | -adk web |
106 | | -``` |
107 | | - |
108 | | -6. Open the web server running at `http://127.0.0.1:8000` and start using the application! |
109 | | - |
110 | | -## Usage |
111 | | -- The agent will start and be ready to handle product search and cart operations. |
112 | | -- You can extend the agent with new tools or integrate it into a larger application. |
113 | | - |
114 | | -## Project Structure |
115 | | -- `mongodb_groceries_agent/agent.py`: Main agent logic |
116 | | -- `mongodb_groceries_agent/create-embeddings.py`: Utility for creating embeddings |
117 | | -- `mongodb_groceries_agent/dataset.csv`: Example dataset |
118 | | - |
119 | | -## Notes |
120 | | -- Ensure your Google Cloud and MongoDB credentials are valid and have the necessary permissions. |
121 | | -- For local development, you may want to use a virtual environment. |
122 | | -- The ADK CLI is required for running and managing agents. |
123 | | - |
124 | | -## License |
125 | | -See [LICENSE](LICENSE) for details. |
0 commit comments