How to Build a Financial AI Agent Using Google ADK: A Complete Tutorial
This tutorial guides you through building a financial AI agent using Google ADK, integrating real-time financial data with Alpha Vantage API for company overviews and earnings analysis.
Introduction to Google Agent Development Kit (ADK)
The Agent Development Kit (ADK) is an open-source Python framework designed to help developers build, manage, and deploy multi-agent systems. Its modular and flexible architecture makes it suitable for both simple and complex agent-based applications.
Creating a Simple Financial AI Agent
In this tutorial, we create a simple AI agent equipped with two financial data tools:
get_company_overviewget_earnings
Step 1: Setting up Dependencies
Google API Key
To use Google's AI services, obtain an API key by visiting Google AI Studio. Sign in, generate your key, and store it securely.
AlphaVantage API Key
To access financial data, get a free API key from Alpha Vantage. Register with your email and save the key securely.
Python Libraries
Install the required package:
pip install google-adkStep 2: Project Folder Structure
Organize your project as follows:
parent_folder/
│
└───multi_agent/
├── __init__.py
├── agent.py
└── .env
__init__.pyimports the agent module:
from . import agent.envfile stores API keys:
GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY="<YOUR_GOOGLE_API_KEY>"
ALPHA_VANTAGE_API_KEY="<YOUR_ALPHA_VANTAGE_KEY>"
Replace placeholders with your actual keys.
Step 3: Defining Agent Tools in agent.py
The agent.py script defines two functions to fetch financial data using the Alpha Vantage API.
from google.adk.agents import Agent
import requests
import os
ALPHA_VANTAGE_API_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")
def get_company_overview(symbol: str) -> dict:
"""
Get comprehensive company information and financial metrics
"""
if not ALPHA_VANTAGE_API_KEY:
return {"status": "error", "error": "Missing API key"}
base_url = "https://www.alphavantage.co/query"
params = {
"function": "OVERVIEW",
"symbol": symbol,
"apikey": ALPHA_VANTAGE_API_KEY
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
if "Error Message" in data:
return {"status": "error", "error": data["Error Message"]}
key_metrics = {
"Description": data.get("Description"),
"Sector": data.get("Sector"),
"MarketCap": data.get("MarketCapitalization"),
"PERatio": data.get("PERatio"),
"ProfitMargin": data.get("ProfitMargin"),
"52WeekHigh": data.get("52WeekHigh"),
"52WeekLow": data.get("52WeekLow")
}
return {
"status": "success",
"symbol": symbol,
"overview": key_metrics
}
except Exception as e:
return {"status": "error", "error": str(e)}
def get_earnings(symbol: str) -> dict:
"""
Get annual and quarterly earnings (EPS) data with analyst estimates and surprises
"""
if not ALPHA_VANTAGE_API_KEY:
return {"status": "error", "error": "Missing API key"}
base_url = "https://www.alphavantage.co/query"
params = {
"function": "EARNINGS",
"symbol": symbol,
"apikey": ALPHA_VANTAGE_API_KEY
}
try:
response = requests.get(base_url, params=params)
response.raise_for_status()
data = response.json()
if "Error Message" in data:
return {"status": "error", "error": data["Error Message"]}
annual_earnings = data.get("annualEarnings", [])[:5]
quarterly_earnings = data.get("quarterlyEarnings", [])[:4]
for q in quarterly_earnings:
if "surprisePercentage" in q:
q["surprise"] = f"{q['surprisePercentage']}%"
return {
"status": "success",
"symbol": symbol,
"annual_earnings": annual_earnings,
"quarterly_earnings": quarterly_earnings,
"metrics": {
"latest_eps": quarterly_earnings[0]["reportedEPS"] if quarterly_earnings else None
}
}
except Exception as e:
return {"status": "error", "error": str(e)}
root_agent = Agent(
name="Financial_analyst_agent",
model="gemini-2.0-flash",
description="Agent to give company overviews with key financial metrics.",
instruction="You are a helpful AI agent that provides company overviews and earnings information",
tools=[get_company_overview, get_earnings],
)This agent uses the Google ADK Agent class and the two tools to answer queries related to company financial information.
Step 4: Running the Agent
Navigate to the parent directory:
cd parent_folder/
Run the agent web interface:
adk web
Open the provided URL (usually http://localhost:8000) in your browser to interact with the agent via a chat interface. You can view each step of the agent's reasoning, including tool calls, inputs/outputs, and generated responses.
Additional Resources
The full code and folder structure are available on GitHub: https://github.com/mohd-arham-islam/ADK-demo
Сменить язык
Читать эту статью на русском