Google ADK - Ecommerce Chatbot - Code
1. Create Folder Structure
pip install google-adk
adk create root_agent
Following directory structure will be creared.
root_agent/
agent.py # main agent code
.env # API keys or project IDs
__init__.py
Create Google API Key and update in .env file.
echo 'GOOGLE_API_KEY="YOUR_API_KEY"' > .env
Update your agent code.
Open agent.py and add following code.
from google.adk.agents.llm_agent import Agent
import requests
import uuid
BASE_URL = "https://dummyjson.com/products"
CART = {} # {product_id: {title, price, qty}}
ORDERS = {} # {order_id: {items, total, status}}
# --------------------------------------
# TOOL 1: Get All Products
# --------------------------------------
def get_all_products():
"""
Returns a list of all products.
This tool will provide the list list of products in below format.
Product ID
Product Name
Product Price
Product Description
break the line after every product.
"""
resp = requests.get(BASE_URL)
resp.raise_for_status()
return resp.json()["products"]
# --------------------------------------
# TOOL 2: Get Product Detail
# --------------------------------------
def get_product_detail(product_id: int):
resp = requests.get(f"{BASE_URL}/{product_id}")
resp.raise_for_status()
return resp.json()
# --------------------------------------
# TOOL 3: Search Products
# --------------------------------------
def search_products(query: str):
resp = requests.get(f"{BASE_URL}/search", params={"q": query})
resp.raise_for_status()
return resp.json()["products"]
# --------------------------------------
# TOOL 4: Add to Cart
# --------------------------------------
def add_to_cart(product_id: int, quantity: int = 1):
product = get_product_detail(product_id)
if product_id in CART:
CART[product_id]["quantity"] += quantity
else:
CART[product_id] = {
"title": product["title"],
"price": product["price"],
"quantity": quantity
}
return {"cart": CART, "message": f"Added {quantity} x {product['title']} to cart."}
# --------------------------------------
# TOOL 5: Remove from Cart
# --------------------------------------
def remove_from_cart(product_id: int):
if product_id in CART:
removed_item = CART.pop(product_id)
return {"cart": CART, "message": f"Removed {removed_item['title']} from cart."}
return {"message": "Item not found in cart."}
# --------------------------------------
# TOOL 6: Checkout
# --------------------------------------
def proceed_to_checkout():
if not CART:
return {"message": "Cart is empty. Add items before checkout."}
order_id = str(uuid.uuid4())[:8]
total = sum(item["price"] * item["quantity"] for item in CART.values())
ORDERS[order_id] = {
"items": CART.copy(),
"total": total,
"status": "Order Placed - Cash on Delivery"
}
CART.clear()
return {
"order_id": order_id,
"total_amount": total,
"message": "Your order has been placed successfully with COD."
}
# --------------------------------------
# TOOL 7: show cart
# --------------------------------------
def show_cart():
return {"cart": CART}
system_instruction = """
You are an expert shopping assistant for an ecommerce website.
Use these tools when necessary:
1) Get All Products → get_all_products()
2) Get Product by ID → get_product_detail()
3) Search Products → search_products() : display list of products in below format.
Product ID
Product Name
Product Price
Product Description
break the line after every product.
4) Add to Cart → add_to_cart()
5) Remove from Cart → remove_from_cart()
6) Proceed to Checkout → proceed_to_checkout()
7) Show Cart → show_cart()
When user asks something related to these actions,
decide if a tool call is required.
"""
root_agent = Agent(
model='gemini-2.0-flash',
name='root_agent',
description="You are an expert shopping assistant for an ecommerce website. Use the tools provided to assist the user. help user to place order",
instruction=system_instruction,
tools=[get_all_products, get_product_detail, search_products, add_to_cart, remove_from_cart, proceed_to_checkout, show_cart],
)
Run your Agent
Run with command-line interface¶
Run your agent using the adk run command-line tool.
Run with web interface¶
The ADK framework provides web interface you can use to test and interact with your agent. You can start the web interface using the following command:
Start chatting with web / console interface
Comments (1)
What is Agent : Learn More
Leave a Comment