Upload 4 files
Browse files
automotive_mal_agent/agent.py
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Automotive Dealership Multi-Agent Example using Google ADK
|
| 2 |
+
|
| 3 |
+
import os
|
| 4 |
+
import json
|
| 5 |
+
import asyncio
|
| 6 |
+
from dotenv import load_dotenv
|
| 7 |
+
import sqlite3
|
| 8 |
+
from google.adk.agents import Agent
|
| 9 |
+
from google.adk.code_executors import BuiltInCodeExecutor
|
| 10 |
+
from google.adk.tools.retrieval.vertex_ai_rag_retrieval import VertexAiRagRetrieval
|
| 11 |
+
from google.adk.tools import AgentTool
|
| 12 |
+
from vertexai.preview import rag
|
| 13 |
+
from google.adk.sessions import InMemorySessionService
|
| 14 |
+
from google.adk.runners import Runner
|
| 15 |
+
from google.genai import types
|
| 16 |
+
from util.py import inventory_agent
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
load_dotenv()
|
| 20 |
+
|
| 21 |
+
# ------------------------------------------------------------------------------
|
| 22 |
+
# Tools
|
| 23 |
+
# ------------------------------------------------------------------------------
|
| 24 |
+
|
| 25 |
+
# --- 1. RAG Retrieval Tool for Brochures / Vehicle Info ---
|
| 26 |
+
ask_vehicle_docs = VertexAiRagRetrieval(
|
| 27 |
+
name="vehicle_rag_retriever",
|
| 28 |
+
description="Retrieves product brochures, specifications, and dealership documentation.",
|
| 29 |
+
rag_resources=[
|
| 30 |
+
rag.RagResource(
|
| 31 |
+
rag_corpus=get_rag_corpus_from_config() or os.environ.get("RAG_CORPUS")
|
| 32 |
+
)
|
| 33 |
+
],
|
| 34 |
+
similarity_top_k=10,
|
| 35 |
+
vector_distance_threshold=0.6,
|
| 36 |
+
)
|
| 37 |
+
|
| 38 |
+
# ------------------------------------------------------------------------------
|
| 39 |
+
# Sub-Agents (Specialist Agents)
|
| 40 |
+
# ------------------------------------------------------------------------------
|
| 41 |
+
|
| 42 |
+
# --- Customer Sales Agent ---
|
| 43 |
+
customer_sales_agent = Agent(
|
| 44 |
+
name="customer_sales_agent",
|
| 45 |
+
model="gemini-2.5-flash",
|
| 46 |
+
description="Provides information about vehicles, trims, features, and general pricing.",
|
| 47 |
+
instruction="""
|
| 48 |
+
You are the Customer Sales Agent at an automotive dealership.
|
| 49 |
+
Your job is to answer questions about vehicle models, features, and pricing.
|
| 50 |
+
Use the RAG retrieval tool to find relevant information from brochures or documents.
|
| 51 |
+
Keep responses friendly and concise.
|
| 52 |
+
Never send empty response to the user. you have all documents at your disposal.
|
| 53 |
+
""",
|
| 54 |
+
tools=[ask_vehicle_docs],
|
| 55 |
+
)
|
| 56 |
+
customer_sales_agent2 = Agent(
|
| 57 |
+
name="customer_sales_agent",
|
| 58 |
+
model="gemini-2.5-flash",
|
| 59 |
+
description="Provides information about vehicles, trims, features, and general pricing.",
|
| 60 |
+
instruction="""
|
| 61 |
+
You are the Customer Sales Agent at an automotive dealership.
|
| 62 |
+
Your job is to answer questions about vehicle models, features, and pricing.
|
| 63 |
+
Use the RAG retrieval tool to find relevant information from brochures or documents.
|
| 64 |
+
Keep responses friendly and concise.
|
| 65 |
+
Never send empty response to the user. you have all documents at your disposal.
|
| 66 |
+
""",
|
| 67 |
+
tools=[ask_vehicle_docs],
|
| 68 |
+
)
|
| 69 |
+
|
| 70 |
+
# --- Finance Agent ---
|
| 71 |
+
finance_agent = Agent(
|
| 72 |
+
name="finance_agent",
|
| 73 |
+
model="gemini-2.5-flash",
|
| 74 |
+
description="Handles all financing-related questions, including EMI and total cost calculations.",
|
| 75 |
+
instruction="""
|
| 76 |
+
You are the Finance Agent.
|
| 77 |
+
When given information like price, down payment, interest rate, and loan term,
|
| 78 |
+
you will write and execute Python code using your code execution tool
|
| 79 |
+
to calculate EMI and total payable amount.
|
| 80 |
+
Provide responses with clear numeric results (USD), formatted neatly.
|
| 81 |
+
""",
|
| 82 |
+
tools=[BuiltInCodeExecutor()],
|
| 83 |
+
#code_executor=BuiltInCodeExecutor(),
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
# ------------------------------------------------------------------------------
|
| 87 |
+
# Root Agent (Orchestrator) - Using AgentTool pattern
|
| 88 |
+
# ------------------------------------------------------------------------------
|
| 89 |
+
|
| 90 |
+
root_agent = Agent(
|
| 91 |
+
name="root_dealership_agent",
|
| 92 |
+
model="gemini-2.5-flash",
|
| 93 |
+
description="Root orchestrator agent that delegates customer queries to specialized subagents.",
|
| 94 |
+
instruction="""
|
| 95 |
+
You are the Root Automotive Dealership Assistant.
|
| 96 |
+
Your job is to understand user intent and delegate tasks to the appropriate specialist agents:
|
| 97 |
+
|
| 98 |
+
- For questions about vehicle features, specifications, or brochures, delegate to the `customer_sales_agent`.
|
| 99 |
+
- For questions about financing, EMIs, or loan calculations, delegate to the `finance_agent`.
|
| 100 |
+
- For questions about stock availability and inventory, delegate to the `inventory_agent`.
|
| 101 |
+
|
| 102 |
+
You can delegate to multiple tools if the user's query requires information from different specialists.
|
| 103 |
+
Always provide a courteous and consolidated summary of the information you receive from the specialist agents.
|
| 104 |
+
""",
|
| 105 |
+
tools=[
|
| 106 |
+
AgentTool(customer_sales_agent),
|
| 107 |
+
AgentTool(finance_agent),
|
| 108 |
+
AgentTool(inventory_agent),
|
| 109 |
+
],
|
| 110 |
+
)
|
automotive_mal_agent/data/inventory.db
ADDED
|
Binary file (12.3 kB). View file
|
|
|
automotive_mal_agent/rag_config.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"corpus_name": "projects/2984842/locations/87955182"
|
| 3 |
+
}
|
automotive_mal_agent/util.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# --- Database Tool for Inventory ---
|
| 2 |
+
def query_inventory_database(query_text: str) -> dict:
|
| 3 |
+
"""
|
| 4 |
+
Execute SQL query on the dealership inventory database.
|
| 5 |
+
|
| 6 |
+
Args:
|
| 7 |
+
query_text (str): The SQL query to execute on the inventory database.
|
| 8 |
+
|
| 9 |
+
Returns:
|
| 10 |
+
dict: Query results with status and data or error message.
|
| 11 |
+
"""
|
| 12 |
+
db_path = "data/inventory.db"
|
| 13 |
+
try:
|
| 14 |
+
conn = sqlite3.connect(db_path)
|
| 15 |
+
cursor = conn.cursor()
|
| 16 |
+
cursor.execute(query_text)
|
| 17 |
+
rows = cursor.fetchall()
|
| 18 |
+
conn.close()
|
| 19 |
+
return {
|
| 20 |
+
"status": "success",
|
| 21 |
+
"data": rows,
|
| 22 |
+
"message": f"Found {len(rows)} results",
|
| 23 |
+
}
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return {"status": "error", "error_message": f"Database error: {str(e)}"}
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
# --- Inventory Agent ---
|
| 29 |
+
inventory_agent = Agent(
|
| 30 |
+
name="inventory_agent",
|
| 31 |
+
model="gemini-2.5-flash",
|
| 32 |
+
description="Checks current stock availability for models, trims, and colors.",
|
| 33 |
+
instruction="""
|
| 34 |
+
You are the Inventory Agent for the dealership.
|
| 35 |
+
Query the connected database to check if a vehicle model, color, or configuration is available.
|
| 36 |
+
Always return results in a structured way: Model | Trim | Color | Quantity.
|
| 37 |
+
If nothing matches, say 'Out of Stock'.
|
| 38 |
+
If you have less arguments in the query, assume model/color/trim but never send an empty response.
|
| 39 |
+
""",
|
| 40 |
+
tools=[query_inventory_database],
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
def get_rag_corpus_from_config(config_path="rag_config.json"):
|
| 44 |
+
"""Reads the RAG corpus name from a JSON config file."""
|
| 45 |
+
with open(config_path, 'r') as f:
|
| 46 |
+
config = json.load(f)
|
| 47 |
+
return config.get("corpus_name")
|