You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
What risks do you see if an agent makes up products or fetches information from outside sources instead of the inventory?
@@ -91,11 +92,13 @@ In this exercise, you’ll add a tool that lets the agent find products relevant
91
92
### Step 1: Generate Embeddings
92
93
93
94
1. Open the MongoDB extension by clicking the green MongoDB leaf in the sidebar.
94
-
2. Expand the **Groceries Database** connection, then the **grocery_store** database and finally, the **inventory** collection.
95
-
3. Open any document. Notice the **gemini_embedding** field: it already contains the product’s vector embedding.
95
+
2. Expand the **Local MongoDB Atlas** connection, then the **grocery_store** database and finally, the **inventory** collection.
96
+
3. Open any MongoDB document from the **inventory** collection. Notice the **gemini_embedding** field: it already contains the product’s vector embedding.
96
97
97
98
We pre-generated these embeddings to save time and resources. Otherwise, every workshop attendee would need to re-run the same embedding process—producing identical vectors at unnecessary cost. For this exercise, you can work directly with the stored vectors.
98
99
100
+
***Hint***: Don't close the document—you'll need it for the next steps.
101
+
99
102
### Step 2: Create a Vector Search Index
100
103
101
104
A vector search index is a special data structure optimized for similarity searches. It allows MongoDB to efficiently compare vectors and return the closest matches.
@@ -112,29 +115,31 @@ Open **`mongodb_groceries_agent/create-vector-search-index.py`** and fill in the
112
115
113
116
1. `<DATABASE_NAME>`: the name of the database that you explored through the MongoDB extension
114
117
2. `<COLLECTION_NAME>`: the collection with grocery products
115
-
3. `<VECTOR_FIELD_IN_THE_DOCUMENT>`: the field storing the embedding (the numeric array)
116
-
4. `<LENGTH_OF_THE_VECTOR>`: the size of the embedding array
118
+
3. `<VECTOR_FIELD_IN_THE_DOCUMENT>`: the name of the field storing the vector embedding
119
+
4. `<LENGTH_OF_THE_VECTOR>`: the size of the embedding array.
120
+
- ***Hint***: The size is one of 128, 256, 512, 768, 1536, or 2048. Check the number of lines in the MongoDB document you opened earlier.
117
121
5. `<VECTOR_SEARCH_DEFINITION>`: use the predefined variable in the script and pass it into the method
118
122
119
-
In the bottom panel, open a new terminal tab and run the script to create the index:
123
+
Stop the running process in the terminal by pressing CTRL+C. Then, execute the vector creation script:
Creating the index takes only a few seconds for the 5000 documents you have in the dataset. After a 10-second timeout, the script will display the collection’s search indexes. At that point, your vector search index is ready and can be used by the agent to find similar products.
129
+
After a 10-second timeout, the script will display the collection’s search indexes. Pay attention to the status of the index you just created—it may show as ***BUILDING*** or ***READY***. The collection has only 5000 documents, so by the time you start using the index, it will be fully built with status ***READY***.
126
130
127
-
### Step 3: Implement the Vector Search Tool
131
+
### Step 3: Implement the Vector Search Tool
128
132
129
-
With the index in place, let’s wire up the agent so it can actually use it.
133
+
With the index in place, let’s implement the search tool that the agent will use to find similar products.
130
134
131
135
In this step, you’ll:
132
-
- Configure placeholders for your database and collection.
133
-
- Define a helper to generate embeddings with Gemini.
134
-
- Implement the `find_similar_products` tool that performs a vector search against MongoDB.
135
-
- Register the tool with the agent so it becomes part of the shopping workflow.
136
+
- Define a helper function to generate embeddings with Gemini. The function will be used to transform the user questions into vector embeddings.
137
+
- Implement the `find_similar_products` tool that performs a vector search against the MongoDB `inventory` collection.
138
+
- Register the tool with the agent so it becomes part of the shopping workflow.
139
+
140
+
Open **`mongodb_groceries_agent/agent.py`** and replace the `root_agent` variable with the following code.
136
141
137
-
Open **`mongodb_groceries_agent/agent.py`** and add the following code:
142
+
**<spanaria-hidden="true">️⚠️</span> Important**: Don't delete the imports or the `GOOGLE_API_KEY` variable!
138
143
139
144
```python
140
145
# Initialize the GenAI client to vectorize the user queries
@@ -201,19 +206,12 @@ Your role is to guide customers through their shopping experience.
201
206
What you can do:
202
207
- Help users discover and explore products in the store.
203
208
- Suggest alternatives when the exact item is not available.
204
-
- Add products to the user’s shopping cart.
205
-
- Answer product-related questions in a clear and concise way.
206
-
- Return the total in the user’s shopping cart.
207
209
208
210
Available tools:
209
211
1. **find_similar_products**: Search for products with names semantically similar to the user’s request.
210
-
2. **add_to_cart**: Add a product to the user’s cart in MongoDB. Pass only the product name (as it appears in the inventory collection) and the user’s username.
211
-
3. **calculate_cart_total**: Sum the total of all products in a user's cart and return it. Pass the user’s username.
212
-
213
212
Core guidelines:
214
-
- **Always search first**: If a user asks for a product, call `find_similar_products` before attempting to add it to the cart.
213
+
- **Always search first**: If a user asks for a product, call `find_similar_products`.
215
214
- **Handle missing products**: If the requested product is not in the inventory, suggest similar items returned by the search.
216
-
- **Parallel tool use**: You may call multiple tools in parallel when appropriate (e.g., searching for several items at once).
217
215
- **Clarify only when necessary**: Ask for more details if the request is unclear and you cannot perform a search.
218
216
- Keep your tone positive, approachable, and customer-focused throughout the interaction.
219
217
@@ -222,9 +220,8 @@ Additional important instructions:
222
220
- **Respect exact names**: When using `add_to_cart`, pass the product name exactly as stored in the inventory collection.
223
221
- **Multi-item requests**: If the user asks for several items in one message, search for all items together and suggest results before adding to the cart.
224
222
- **Quantity requests**: If the user specifies a quantity, repeat it back to confirm and ensure it is respected when adding to the cart.
225
-
- **Cart confirmation**: After adding items, confirm with the user that they have been successfully added.
226
223
- **Fallback behavior**: If no results are found, apologize politely, and encourage the user to try a different product or category.
227
-
- **Stay focused**: Only handle product discovery, shopping, and cart management tasks. Politely decline requests unrelated to groceries.
224
+
- **Stay focused**: Only handle product discovery. Politely decline requests unrelated to groceries.
228
225
- **Answering product questions**: If the question is about a product (e.g., "Is this organic?" or "How much does it cost?"), use the search results to answer. If the information is not available, respond transparently that you don’t have that detail.
229
226
230
227
Remember: you are a professional yet friendly shopping assistant whose goal is to make the user’s grocery shopping smooth, efficient, and enjoyable.
@@ -234,14 +231,20 @@ Remember: you are a professional yet friendly shopping assistant whose goal is t
234
231
root_agent = Agent(
235
232
model="gemini-2.5-flash",
236
233
name="grocery_shopping_agent",
237
-
instruction=instruction
234
+
instruction=instruction,
238
235
tools=[
239
-
find_similar_products
236
+
<TOOL_FUNCTION_NAME># <-- 4. Replace with the product search function declared above.
240
237
]
241
238
)
242
239
```
243
240
244
-
Finally, restart the agent with the following command:
241
+
Replace any of the placeholders with the correct values:
242
+
1. `<OUTPUT_VECTOR_SIZE>` - Replace with the desired size of the vector. This should match the vector size in the document.
243
+
2. `<VECTOR_FIELD_IN_THE_DOCUMENT` — Replace with the document field that holds the vector embedding
244
+
3. `<VECTOR_FIELD_IN_THE_DOCUMENT>` (again) — Replace with the document field the holds the embedding. This will reduce the network traffic and the tokens the agent needs to include in LLM prompt.
245
+
4. `<TOOL_FUNCTION_NAME>` - Replace with the product search function declared above.
246
+
247
+
Finally, start the agent development server again with the following command:
0 commit comments