Skip to content

Commit 5cfd811

Browse files
authored
feat: day 3 (#1)
1 parent 829aefd commit 5cfd811

4 files changed

Lines changed: 83 additions & 3 deletions

File tree

agents/my_agent/agent.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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?"))

agents/my_agent/root_agent.yaml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# yaml-language-server: $schema=https://raw.githubusercontent.com/google/adk-python/refs/heads/main/src/google/adk/agents/config_schemas/AgentConfig.json
2-
name: root_agent
3-
description: A helpful assistant for user questions.
4-
instruction: Answer user questions to the best of your knowledge
2+
name: search_agent
3+
description: A helpful assistant that can search Google.
4+
instruction: |
5+
You are a helpful assistant with access to Google Search.
6+
7+
If the user asks a question that requires current information or facts, use the 'google_search' tool.
8+
Always cite your sources implicitly by providing the answer clearly based on the search results.
59
model: gemini-2.5-flash
10+
tools:
11+
- name: google_search

figs/day-03.png

979 KB
Loading

notes/day-03.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Day 03 - 在 Python 代码中加载 YAML 配置的智能体
2+
3+
**挑战链接**: [Advent of Agents - Day 03](https://adventofagents.com/day/03)
4+
5+
![Day 03 Screenshot](figs/day-03.png)
6+
7+
## 关键学习
8+
9+
- Google ADK 提供了内置方法 `load_agent_from_config()` 来从 YAML 文件加载智能体配置
10+
- YAML 配置文件可以包含完整的智能体定义,包括名称、描述、指令、模型和工具
11+
- 工具可以在 YAML 文件中通过 `tools` 部分定义,例如 `google_search`
12+
- ADK Web Developer UI 可以自动检测并加载 Python 代码中定义的智能体
13+
- 使用 `Path(__file__).parent` 可以获取相对于当前脚本文件的路径,方便加载同目录下的 YAML 文件
14+
15+
## 面临的挑战
16+
17+
- 最初尝试手动解析 YAML 文件,但发现 ADK 有更优雅的内置方法
18+
- 需要了解 `Agent` 类和 `LlmAgent` 类的区别,以及何时使用 `load_agent_from_config`
19+
- 确保 YAML 文件中的工具名称与 ADK 提供的工具名称匹配
20+
21+
## 资源
22+
23+
- [ADK Config Agent Documentation](http://google.github.io/adk-docs/agent/config/) - 使用 YAML 配置构建智能体的官方文档
24+
- [ADK Agents API Reference](http://google.github.io/adk-docs/agents/) - 智能体 API 参考文档
25+
- [Google ADK Python Documentation](http://google.github.io/adk-docs/) - ADK Python 完整文档
26+
27+
## 问题 / 开放话题
28+
29+
- 不是必须要用 Gemini Pro 3,在 YAML 配置里把 model 模型改成 free tier 里提供的即可

0 commit comments

Comments
 (0)