import os, asyncio import warnings, logging from google.adk.agents import Agent from google.adk.models.lite_llm import LiteLlm from google.adk.sessions import InMemorySessionService from google.adk.runners import Runner from google.genai import types from tools import greetings, farewell
greeting_agent = Agent( model=LiteLlm( model="openrouter/meta-llama/llama-4-scout:free", api_key=os.getenv("OpenRouter_API"), api_base=os.getenv("OpenRouter_URL"), ), name="greeting_agent", instruction="You are the Greeting Agent. Your ONLY task is to provide a friendly greeting to the user. " "Use the 'greetings' tool to generate the greeting. " "If the user provides their name, make sure to pass it to the tool. " "Do not engage in any other conversation or tasks.", description="Handles simple greetings and hellos using the 'greetings' tool.", # Crucial for delegation tools=[greetings], )
farewell_agent = Agent( model=LiteLlm( model="openrouter/meta-llama/llama-4-scout:free", api_key=os.getenv("OpenRouter_API"), api_base=os.getenv("OpenRouter_URL"), ), name="farewell_agent", instruction="You are the Farewell Agent. Your ONLY task is to provide a friendly farewell to the user. " "Use the 'farewell' tool to generate the farewell message. " "If the user provides their name, make sure to pass it to the tool. " "Do not engage in any other conversation or tasks.", description="Handles simple farewells and goodbyes using the 'farewell' tool.", # Crucial for delegation tools=[farewell], )
我们现在来定义 weather agent team. 唯一有区别的在于,我们在 Agent class 初始化的时候,传入 subagent=[] 参数来告诉 agent 下一步该调用哪个 agent. 至于如何调用、在什么条件下调用,完全由 LLM 决策、由 Google ADK 解析决策结果并执行.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
weather_agent_team = Agent( name="weather_agent_team", model=LiteLlm( model="openrouter/meta-llama/llama-4-scout:free", api_key=os.getenv("OpenRouter_API"), api_base=os.getenv("OpenRouter_URL"), ), description="The main coordinator agent. Handles weather requests and delegates greetings/farewells to specialists.", instruction="You are the main Weather Agent coordinating a team. Your primary responsibility is to provide weather information. " "Use the 'get_weather' tool ONLY for specific weather requests (e.g., 'weather in London'). " "You have specialized sub-agents: " "1. 'greeting_agent': Handles simple greetings like 'Hi', 'Hello'. Delegate to it for these. " "2. 'farewell_agent': Handles simple farewells like 'Bye', 'See you'. Delegate to it for these. " "Analyze the user's query. If it's a greeting, delegate to 'greeting_agent'. If it's a farewell, delegate to 'farewell_agent'. " "If it's a weather request, handle it yourself using 'get_weather'. " "For anything else, respond appropriately or state you cannot handle it.", tools=[get_weather], # Root agent still needs the weather tool for its core task sub_agents=[greeting_agent, farewell_agent], )