Generative AI applications are rapidly moving beyond single-turn conversational chatbots toward Autonomous Multi-Tool AI Agents. Instead of just generating static text, modern agents evaluate user prompts, make routing decisions, select specialised external tools, and fetch dynamic real-time data before returning a grounded response.
In this article, we will break down the end-to-end architecture and implementation of an autonomous agent built using Vertex AI, Python, and Google Cloud infrastructure.
## High-Level System Architecture
The solution uses a three-tier agentic architecture designed for low latency, modularity, and strict session isolation:
- User Interaction Layer: A frontend built with Streamlit and deployed on Cloud Run, managing contextual chat turns via st.session_state`.
- Orchestration Layer: Gemini models hosted on Vertex AI acting as the reasoning engine to determine tool execution plans.
- Tool Execution Layer: Connectors to Firestore vector stores, BigQuery datasets, and external REST APIs to provide real-time grounding.
1. Setting Up Google Cloud Environment
To start, configure your Google Cloud project and enable the necessary service APIs in Cloud Shell:
`bash
Set project configuration
export PROJECT_ID=$(gcloud config get-value project)
export REGION="us-central1"
Enable required Google Cloud APIs
gcloud services enable \
aiplatform.googleapis.com \
run.googleapis.com \
cloudbuild.googleapis.com \
firestore.googleapis.com
1. Defining Agent Tools and Schema Declarations
`
import vertexai
from vertexai.generative_models import GenerativeModel, FunctionDeclaration, Tool
Initialize Vertex AI
vertexai.init(project="YOUR_PROJECT_ID", location="us-central1")
Define a tool for inventory lookups
inventory_func = FunctionDeclaration(
name="query_inventory",
description="Look up product stock, availability, and unit pricing dynamically.",
parameters={
"type": "object",
"properties": {
"item_name": {
"type": "string",
"description": "The specific item or product name to search"
},
"category": {
"type": "string",
"description": "Item category, e.g., beverages, snacks, merchandise"
}
},
"required": ["item_name"]
},
)
agent_tools = Tool(function_declarations=[inventory_func])
plaintext
**2. Implementing the Orchestration Logic**
def query_inventory(item_name: str, category: str = None) -> dict:
# Simulated database lookup or Firestore Vector retrieval
return {
"item": item_name,
"in_stock": True,
"quantity": 42,
"price_usd": 4.50
}
Instantiate the model with tool definitions
model = GenerativeModel(
model_name="gemini-1.5-flash-001",
tools=[agent_tools]
)
chat = model.start_chat()
response = chat.send_message("Do we have any Cold Brew in stock?")
Parse function calls if triggered
for part in response.candidates[0].content.parts:
if part.function_call:
fn_name = part.function_call.name
fn_args = dict(part.function_call.args)
if fn_name == "query_inventory":
tool_result = query_inventory(**fn_args)
# Return tool output back to the model for final synthesis
final_response = chat.send_message(
vertexai.generative_models.Part.from_function_response(
name=fn_name,
response={"content": tool_result}
)
)
print(final_response.text)
plaintext
**3. Packaging and Deploying to Google Cloud Run**
Dockerfile
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
EXPOSE 8080
CMD ["streamlit", "run", "app.py", "--server.port=8080", "--server.address=0.0.0.0"]
`shell
Deploy directly using the Google Cloud CLI:
`
gcloud run deploy genai-agent-service \
--source . \
--region us-central1 \
--allow-unauthenticated
`
Top comments (0)