HomeArtificial IntelligenceStep-by-Step Information to Create an AI agent with Google ADK

Step-by-Step Information to Create an AI agent with Google ADK


Agent Improvement Package (ADK) is an open-source Python framework that helps builders construct, handle, and deploy multi-agent methods. It’s designed to be modular and versatile, making it simple to make use of for each easy and complicated agent-based purposes.

On this tutorial, we’ll create a easy AI agent utilizing ADK. The agent may have entry to 2 instruments:

Google API Key

To make use of Google’s AI providers, you’ll want an API key:

AlphaVantage API Key

For accessing monetary information, we’ll use the Alpha Vantage API:

  • Go to https://www.alphavantage.co/
  • Click on “Get your free API key” or go to this direct hyperlink
  • Enter your electronic mail and comply with the directions
  • When you obtain your API key, copy and put it aside securely. We’ll use it to authenticate requests to monetary endpoints.

Python Libraries

We solely want one package deal:

Arrange your challenge folder with the next construction:

parent_folder/
│
└───multi_agent/
    ├── __init__.py
    ├── agent.py
    └── .env

__init__.py

Paste the next code into multi_agent/__init__.py:

.env

Create a .env file contained in the multi_agent folder and paste the next:

GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY=""
ALPHA_VANTAGE_API_KEY="

Substitute the placeholders together with your precise API keys

agent.py

Paste the next code within the agent.py file:

from google.adk.brokers import Agent
import requests
import os
from typing import Non-compulsory

ALPHA_VANTAGE_API_KEY = os.getenv("ALPHA_VANTAGE_API_KEY")

def get_company_overview(image: str) -> dict:
    """
    Get complete firm data and monetary metrics
   
    Args:
        image: Inventory ticker image (e.g., IBM)
   
    Returns:
        dict: Firm overview information or error
    """
    if not ALPHA_VANTAGE_API_KEY:
        return {"standing": "error", "error": "Lacking API key"}
   
    base_url = "https://www.alphavantage.co/question"
    params = {
        "perform": "OVERVIEW",
        "image": image,
        "apikey": ALPHA_VANTAGE_API_KEY
    }
   
    strive:
        response = requests.get(base_url, params=params)
        response.raise_for_status()
        information = response.json()
       
        if "Error Message" in information:
            return {"standing": "error", "error": information["Error Message"]}
           
        # Filter key metrics
        key_metrics = {
            "Description": information.get("Description"),
            "Sector": information.get("Sector"),
            "MarketCap": information.get("MarketCapitalization"),
            "PERatio": information.get("PERatio"),
            "ProfitMargin": information.get("ProfitMargin"),
            "52WeekHigh": information.get("52WeekHigh"),
            "52WeekLow": information.get("52WeekLow")
        }
       
        return {
            "standing": "success",
            "image": image,
            "overview": key_metrics
        }
       
    besides Exception as e:
        return {"standing": "error", "error": str(e)}

def get_earnings(image: str) -> dict:
    """
    Get annual and quarterly earnings (EPS) information with analyst estimates and surprises
   
    Args:
        image: Inventory ticker image (e.g., IBM)
   
    Returns:
        dict: Earnings information with estimates or error message
    """
    if not ALPHA_VANTAGE_API_KEY:
        return {"standing": "error", "error": "Lacking API key"}
   
    base_url = "https://www.alphavantage.co/question"
    params = {
        "perform": "EARNINGS",
        "image": image,
        "apikey": ALPHA_VANTAGE_API_KEY
    }
   
    strive:
        response = requests.get(base_url, params=params)
        response.raise_for_status()
        information = response.json()
       
        if "Error Message" in information:
            return {"standing": "error", "error": information["Error Message"]}
           
        # Course of annual and quarterly earnings
        annual_earnings = information.get("annualEarnings", [])[:5]  # Final 5 years
        quarterly_earnings = information.get("quarterlyEarnings", [])[:4]  # Final 4 quarters
       
        # Format shock percentages
        for q in quarterly_earnings:
            if "surprisePercentage" in q:
                q["surprise"] = f"{q['surprisePercentage']}%"
       
        return {
            "standing": "success",
            "image": image,
            "annual_earnings": annual_earnings,
            "quarterly_earnings": quarterly_earnings,
            "metrics": {
                "latest_eps": quarterly_earnings[0]["reportedEPS"] if quarterly_earnings else None
            }
        }
       
    besides Exception as e:
        return {"standing": "error", "error": str(e)}
   
   
root_agent = Agent(
    title="Financial_analyst_agent",
    mannequin="gemini-2.0-flash",
    description=(
        "Agent to present firm overviews with key monetary metrics."
    ),
    instruction=(
        "You're a useful AI agent that gives firm overviews and earnings data"
    ),
    instruments=[get_company_overview, get_earnings],
)

On this script, we outline a monetary evaluation agent utilizing the Google Agent Improvement Package (ADK). The agent is designed to reply consumer queries by accessing real-time monetary information via the Alpha Vantage API. Particularly, it exposes two instruments: get_company_overview and get_earnings. The get_company_overview perform retrieves key firm particulars equivalent to sector, market capitalization, P/E ratio, and 52-week excessive/low values. The get_earnings perform supplies each annual and quarterly earnings information, together with reported EPS and shock percentages.To create the agent, we use the Agent class from the google.adk.brokers module, giving it a reputation, a mannequin (e.g., Gemini 2.0 Flash), an outline, and an instruction immediate. The agent is then outfitted with the 2 instruments talked about above, permitting it to reply to questions associated to firm financials.

To run the agent, navigate to the father or mother listing of your agent challenge (e.g. utilizing cd ..)

parent_folder/      ← Navigate to this listing in your terminal
│
└───multi_agent/
    ├── __init__.py     # Initializes the module
    ├── agent.py        # Accommodates the agent logic and instruments
    └── .env            # Shops your API keys securely

After navigating, run the next code:

Open the URL offered (normally http://localhost:8000 or http://127.0.0.1:8000) immediately in your browser. You’ll see a easy chat interface the place you’ll be able to work together together with your agent utilizing the enter textbox.

Moreover, you’ll be able to examine every step of the agent’s reasoning by clicking on Actions. This lets you view:

  • The instruments being referred to as
  • The inputs and outputs of every perform
  • The responses generated by the language mannequin

You could find your entire code together with folder construction at this hyperlink: https://github.com/mohd-arham-islam/ADK-demo


I’m a Civil Engineering Graduate (2022) from Jamia Millia Islamia, New Delhi, and I’ve a eager curiosity in Information Science, particularly Neural Networks and their software in varied areas.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments