Google ADK와 LiteLLM

Google ADK와 LiteLLM

Google ADK를 LiteLLM Python SDK, LiteLLM Proxy와 함께 사용해 봐요. 이 튜토리얼은 Agent Development Kit(ADK)를 사용해 여러 LLM 제공자를 LiteLLM으로 지원하는 지능형 에이전트를 만드는 방법을 보여줍니다.

출처: 문서

본문

개요

ADK(Agent Development Kit)는 LLM으로 구동되는 지능형 에이전트를 만들 수 있게 해줍니다. LiteLLM과 통합하면 다음을 할 수 있어요:

  • 여러 LLM 제공자(OpenAI, Anthropic, Google 등) 사용
  • 다른 제공자의 모델 간 손쉬운 전환
  • 중앙 집중식 모델 관리를 위해 LiteLLM proxy에 연결

전제 조건

  • Python 환경 설정
  • 모델 제공자(OpenAI, Anthropic, Google AI Studio)용 API 키
  • LLM과 에이전트 개념에 대한 기본 이해

설치

의존성을 설치합니다.

uv add google-adk litellm

1. 환경 설정

먼저 필요한 라이브러리를 가져오고 API 키를 설정합니다:

Setup environment and API keys

import osimport asynciofrom google.adk.agents import Agentfrom google.adk.models.lite_llm import LiteLlm  # For multi-model supportfrom google.adk.sessions import InMemorySessionServicefrom google.adk.runners import Runnerfrom google.genai import typesimport litellm  # Import for proxy configuration# Set your API keysos.environ["GOOGLE_API_KEY"] = "your-google-api-key"  # For Gemini modelsos.environ["OPENAI_API_KEY"] = "your-openai-api-key"  # For OpenAI modelsos.environ["ANTHROPIC_API_KEY"] = "your-anthropic-api-key"  # For Claude models# Define model constants for cleaner codeMODEL_GEMINI_PRO = "gemini-3.1-pro-preview"MODEL_GPT = "openai/gpt-5.6-terra"MODEL_CLAUDE_SONNET = "anthropic/claude-sonnet-5"

2. 간단한 도구 정의

에이전트가 사용할 도구를 만드세요:

Weather tool implementation

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

        Args:
        city (str): The name of the city (e.g., "New York", "London", "Tokyo").
        Returns:
        dict: A dictionary containing the weather information.
              Includes a 'status' key ('success' or 'error').
              If 'success', includes a 'report' key with weather details.
              If 'error', includes an 'error_message' key.
    """
    print(f"Tool: get_weather called for city: {city}")
        # Mock weather data
    mock_weather_db = {
        "newyork": {"status": "success", "report": "The weather in New York is sunny with a temperature of 25°C."},
        "london": {"status": "success", "report": "It's cloudy in London with a temperature of 15°C."},
        "tokyo": {"status": "success", "report": "Tokyo is experiencing light rain and a temperature of 18°C."},
    }
        city_normalized = city.lower().replace(" ", "")
        if city_normalized in mock_weather_db:
        return mock_weather_db[city_normalized]
    else:
        return {"status": "error", "error_message": f"Sorry, I don't have weather information for '{city}'."}

3. 에이전트 상호작용 헬퍼 함수

에이전트 상호작용을 돕는 헬퍼 함수를 만드세요:

Agent interaction helper function

async def call_agent_async(query: str, runner, user_id, session_id):
    """Sends a query to the agent and prints the final response."""
    print(f"\n>>> User Query: {query}")
    # Prepare the user's message in ADK format
    content = types.Content(role='user', parts=[types.Part(text=query)])
        final_response_text = "Agent did not produce a final response."
        # Execute the agent and find the final response
    async for event in runner.run_async(
        user_id=user_id,
        session_id=session_id,
        new_message=content
    ):
        if event.is_final_response():
            if event.content and event.content.parts:
                final_response_text = event.content.parts[0].text
            break
                print(f"

4. ADK로 다양한 모델 제공자 사용

4.1 OpenAI 모델 사용

OpenAI model implementation

# Create an agent powered by OpenAI's GPT modelweather_agent_gpt = Agent(
    name="weather_agent_gpt",
    model=LiteLlm(model=MODEL_GPT),  # Use OpenAI's GPT model
    description="Provides weather information using OpenAI's GPT.",
    instruction="You are a helpful weather assistant powered by gpt-5.6-terra. "
                "Use the 'get_weather' tool for city weather requests. "
                "Present information clearly.",
    tools=[get_weather],)# Set up session and runnersession_service_gpt = InMemorySessionService()session_gpt = session_service_gpt.create_session(
    app_name="weather_app",
    user_id="user_1",
    session_id="session_gpt")runner_gpt = Runner(
    agent=weather_agent_gpt,
    app_name="weather_app",
    session_service=session_service_gpt)# Test the GPT agentasync def test_gpt_agent():    print("\n--- Testing GPT Agent ---")
    await call_agent_async(
        "What's the weather in London?",
        runner=runner_gpt,
        user_id="user_1",
        session_id="session_gpt"
    )# Execute the conversation with the GPT agentawait test_gpt_agent()# Or if running as a standard Python script:# if __name__ == "__main__":#     asyncio.run(test_gpt_agent())

4.2 Anthropic 모델 사용

Anthropic model implementation

# Create an agent powered by Anthropic's Claude modelweather_agent_claude = Agent(
    name="weather_agent_claude",
    model=LiteLlm(model=MODEL_CLAUDE_SONNET),  # Use Anthropic's Claude model
    description="Provides weather information using Anthropic's Claude.",
    instruction="You are a helpful weather assistant powered by Claude Sonnet. "
                "Use the 'get_weather' tool for city weather requests. "
                "Present information clearly.",
    tools=[get_weather],)# Set up session and runnersession_service_claude = InMemorySessionService()session_claude = session_service_claude.create_session(
    app_name="weather_app",
    user_id="user_1",
    session_id="session_claude")runner_claude = Runner(
    agent=weather_agent_claude,
    app_name="weather_app",
    session_service=session_service_claude)# Test the Claude agentasync def test_claude_agent():    print("\n--- Testing Claude Agent ---")
    await call_agent_async(
        "What's the weather in Tokyo?",
        runner=runner_claude,
        user_id="user_1",
        session_id="session_claude"
    )# Execute the conversation with the Claude agentawait test_claude_agent()# Or if running as a standard Python script:# if __name__ == "__main__":#     asyncio.run(test_claude_agent())

4.3 Google Gemini 모델 사용

Gemini model implementation

# Create an agent powered by Google's Gemini modelweather_agent_gemini = Agent(
    name="weather_agent_gemini",
    model=MODEL_GEMINI_PRO,  # Use Gemini model directly (no LiteLlm wrapper needed)
    description="Provides weather information using Google's Gemini.",
    instruction="You are a helpful weather assistant powered by Gemini Pro. "
                "Use the 'get_weather' tool for city weather requests. "
                "Present information clearly.",
    tools=[get_weather],)# Set up session and runnersession_service_gemini = InMemorySessionService()session_gemini = session_service_gemini.create_session(
    app_name="weather_app",
    user_id="user_1",
    session_id="session_gemini")runner_gemini = Runner(
    agent=weather_agent_gemini,
    app_name="weather_app",
    session_service=session_service_gemini)# Test the Gemini agentasync def test_gemini_agent():    print("\n--- Testing Gemini Agent ---")
    await call_agent_async(
        "What's the weather in New York?",
        runner=runner_gemini,
        user_id="user_1",
        session_id="session_gemini"
    )# Execute the conversation with the Gemini agentawait test_gemini_agent()# Or if running as a standard Python script:# if __name__ == "__main__":#     asyncio.run(test_gemini_agent())

5. ADK와 함께 LiteLLM Proxy 사용

LiteLLM proxy는 여러 모델을 위한 통합 API 엔드포인트를 제공하여 배포와 중앙 집중식 관리를 단순화해요.

Required settings for using litellm proxy

| Variable | Description | | LITELLM_PROXY_API_KEY | The API key for the LiteLLM proxy | | LITELLM_PROXY_API_BASE | The base URL for the LiteLLM proxy | | USE_LITELLM_PROXY or litellm.use_litellm_proxy | When set to True, your request will be sent to litellm proxy. |

LiteLLM proxy integration

# Set your LiteLLM Proxy credentials as environment variablesos.environ["LITELLM_PROXY_API_KEY"] = "your-litellm-proxy-api-key"os.environ["LITELLM_PROXY_API_BASE"] = "your-litellm-proxy-url"  # e.g., "http://localhost:4000"# Enable the use_litellm_proxy flaglitellm.use_litellm_proxy = True# Create a proxy-enabled agent (using environment variables)weather_agent_proxy_env = Agent(
    name="weather_agent_proxy_env",
    model=LiteLlm(model="gpt-5.6-terra"), # this will call the `gpt-5.6-terra` model on LiteLLM proxy
    description="Provides weather information using a model from LiteLLM proxy.",
    instruction="You are a helpful weather assistant. "
                "Use the 'get_weather' tool for city weather requests. "
                "Present information clearly.",
    tools=[get_weather],)# Set up session and runnersession_service_proxy_env = InMemorySessionService()session_proxy_env = session_service_proxy_env.create_session(
    app_name="weather_app",
    user_id="user_1",
    session_id="session_proxy_env")runner_proxy_env = Runner(
    agent=weather_agent_proxy_env,
    app_name="weather_app",
    session_service=session_service_proxy_env)# Test the proxy-enabled agent (environment variables method)async def test_proxy_env_agent():    print("\n--- Testing Proxy-enabled Agent (Environment Variables) ---")
    await call_agent_async(
        "What's the weather in London?",
        runner=runner_proxy_env,
        user_id="user_1",
        session_id="session_proxy_env"
    )# Execute the conversationawait test_proxy_env_agent()

더 알아보기 (Learn more)