|
| 1 | +from google.adk.agents.config_agent_utils import load_agent_from_config |
| 2 | +from google.adk.runners import Runner |
| 3 | +from google.adk.sessions import InMemorySessionService |
| 4 | +from google.genai import types |
| 5 | +import asyncio |
| 6 | +from pathlib import Path |
| 7 | + |
| 8 | +# CONFIGURATION |
| 9 | +APP_NAME = "simple_search_agent" |
| 10 | +USER_ID = "user_default" |
| 11 | +SESSION_ID = "session_01" |
| 12 | + |
| 13 | +# AGENT DEFINITION - Load from YAML using ADK's built-in method |
| 14 | +yaml_path = Path(__file__).parent / 'root_agent.yaml' |
| 15 | +root_agent = load_agent_from_config(str(yaml_path)) |
| 16 | + |
| 17 | + |
| 18 | +# Session and Runner |
| 19 | +async def setup_session_and_runner(): |
| 20 | + session_service = InMemorySessionService() |
| 21 | + session = await session_service.create_session( |
| 22 | + app_name=APP_NAME, user_id=USER_ID, session_id=SESSION_ID |
| 23 | + ) |
| 24 | + runner = Runner( |
| 25 | + agent=root_agent, app_name=APP_NAME, session_service=session_service |
| 26 | + ) |
| 27 | + return session, runner |
| 28 | + |
| 29 | + |
| 30 | +# Agent Interaction |
| 31 | +async def call_agent_async(query): |
| 32 | + content = types.Content(role="user", parts=[types.Part(text=query)]) |
| 33 | + session, runner = await setup_session_and_runner() |
| 34 | + events = runner.run_async( |
| 35 | + user_id=USER_ID, session_id=SESSION_ID, new_message=content |
| 36 | + ) |
| 37 | + |
| 38 | + async for event in events: |
| 39 | + if event.is_final_response(): |
| 40 | + final_response = event.content.parts[0].text |
| 41 | + print("Agent Response: ", final_response) |
| 42 | + |
| 43 | + |
| 44 | +if __name__ == "__main__": |
| 45 | + asyncio.run(call_agent_async("what's the latest ai news?")) |
0 commit comments