[AI] google-adk 를 써보자.

늘 공부하는 괴짜·2025년 5월 8일
0

AI : mcp

목록 보기
9/9
post-thumbnail

1. venv 환경 설정

% python -m venv .venv
% source .venv/bin/activate

2. google-adk 설치

% pip install google-adk

3. 폴더 생성

% mkdir multi_tool_agent
% cd multi_tool_agent

4. init.py

% echo "from . import agent" > __init__.py

5. agent.py

공식사이트에 있는 그대로 퍼옴

import datetime
from zoneinfo import ZoneInfo
from google.adk.agents import Agent

def get_weather(city: str) -> dict:
    """Retrieves the current weather report for a specified city.

    Args:
        city (str): The name of the city for which to retrieve the weather report.

    Returns:
        dict: status and result or error msg.
    """
    if city.lower() == "new york":
        return {
            "status": "success",
            "report": (
                "The weather in New York is sunny with a temperature of 25 degrees"
                " Celsius (77 degrees Fahrenheit)."
            ),
        }
    else:
        return {
            "status": "error",
            "error_message": f"Weather information for '{city}' is not available.",
        }


def get_current_time(city: str) -> dict:
    """Returns the current time in a specified city.

    Args:
        city (str): The name of the city for which to retrieve the current time.

    Returns:
        dict: status and result or error msg.
    """

    if city.lower() == "new york":
        tz_identifier = "America/New_York"
    else:
        return {
            "status": "error",
            "error_message": (
                f"Sorry, I don't have timezone information for {city}."
            ),
        }

    tz = ZoneInfo(tz_identifier)
    now = datetime.datetime.now(tz)
    report = (
        f'The current time in {city} is {now.strftime("%Y-%m-%d %H:%M:%S %Z%z")}'
    )
    return {"status": "success", "report": report}


root_agent = Agent(
    name="weather_time_agent",
    model="gemini-2.0-flash",
    description=(
        "Agent to answer questions about the time and weather in a city."
    ),
    instruction=(
        "You are a helpful agent who can answer user questions about the time and weather in a city."
    ),
    tools=[get_weather, get_current_time],
)

6. API Key 받기

6-1. Google AI Studio 접속

https://aistudio.google.com/apikey
기존 프로젝트에서 API 키 만들기 (또는 프로젝트 생성 후 재시도)

6-2. API Key 복사 및 프로젝트 이동

  • 프로젝트 이름 옆의 아이콘 클릭

  • 방금 만든 따끈따끈한 API Key

7. .env 파일 수정

GOOGLE_GENAI_USE_VERTEXAI=FALSE
GOOGLE_API_KEY=<<Google Api Key 입력>>

8. 에이전트 실행

소스가 들어있는 multi_tool_agent 와 동일 레벨 폴더에서 실행한다.

% adk run multi_tool_agent

9. 에이전트에 말걸어 보자.

시간 알려주는 mcp와 날씨 알려주는 mcp가 있다.

10. 특정 정보만 알려주는 mcp 가 가능해진다.

오직 시간과 날짜만...ㅠㅠ

11. 무제한은 아니라고 한다.

  • Google console > API 및 서비스 > 사용 설정된 API 및 서비스 화면
    Generative language API 클릭

  • Generative language API 클릭
    모델별로 1분당 제한이 걸려있다.

Finally

이전까지 claude desktop, cursor ai, langchain+ollama 등으로 mcp 를 사부작사부작 만들었는데 google-adk 는 뭔가 기다렸다는 듯히 딱! 하고 등장한 느낌이다. API 를 거의 무료로 사용 가능하고(한도 내에서) 속도도 괜찮고 테스트 가능한 모델도 많아서 꽤 오랜 시간 이걸 가지고 둘러보지 않을까...

profile
인공지능이라는 옷을 입었습니다. 뭔가 멋지면서도 잘 맞습니다.

0개의 댓글