refactor: 重构目录结构,扁平化agent布局

- 将 agents/root_agent、agents/note_agent 移至项目根目录
- 将 base/ 重命名为 agent_base/,避免被ADK误识别为agent
- 移除所有 __init__.py 中的 sys.path.insert hack
- root_agent 添加 note_agent 作为子智能体(sub_agents)
- 更新所有 import 路径
- 更新 README.md 和 API_DOC.md
This commit is contained in:
2026-04-08 02:46:15 +08:00
parent f5e8de559e
commit 7f3176efc7
11 changed files with 102 additions and 111 deletions

159
README.md
View File

@@ -1,26 +1,28 @@
# 慧遇 Agent # 慧遇 Agent
慧遇智能体项目,基于 Google ADK (Agent Development Kit) 构建。 基于 Google ADK (Agent Development Kit) 构建的智能体项目,支持多智能体协作、多用户会话管理和 REST API 集成
## 项目结构 ## 项目结构
``` ```
workspace/ ├── root_agent/ # 根智能体(小慧)
├── agents/ # 所有 Agent 模块
│ ├── __init__.py │ ├── __init__.py
│ └── root_agent/ # Root Agent │ └── agent.py # 定义 root_agent,包含子智能体引用
├── note_agent/ # 笔记子智能体
│ ├── __init__.py │ ├── __init__.py
── agent.py ── agent.py # 定义 root_agentADK 要求导出名)
├── base/ # 基础工具类 │ └── note_formatter.py # Markdown 笔记格式化器
├── agent_base/ # Agent 基类
│ ├── __init__.py │ ├── __init__.py
│ └── agent.py # HuiYuBaseAgent 基类 │ └── agent_base.py # HuiYuBaseAgent(防注入 + 日志)
├── common/ # 公共模块 ├── common/ # 公共工具
│ ├── __init__.py # 环境初始化(.env 加载、日志配置) │ ├── __init__.py # 环境初始化(.env 加载、日志配置)
│ ├── logger.py # 模型调用耗时日志 │ ├── logger.py # 模型调用耗时日志
│ └── prompt_guard.py # 防提示词注入检测 │ └── prompt_guard.py # 防提示词注入检测
├── .env # 环境变量配置(不提交到 Git ├── .env # 环境变量配置(不提交到 Git
├── .env.example # 配置示例(可提交到 Git ├── .env.example # 配置示例
├── .gitignore ├── API_DOC.md # REST API 接口文档
├── requirements.txt
└── README.md └── README.md
``` ```
@@ -29,16 +31,9 @@ workspace/
- Python >= 3.10 - Python >= 3.10
- pip - pip
## 环境搭建 ## 快速开始
### 1. 克隆项目 ### 1. 安装依赖
```bash
git clone <your-repo-url>
cd <project-directory>
```
### 2. 安装依赖
```bash ```bash
pip install -r requirements.txt pip install -r requirements.txt
@@ -47,97 +42,98 @@ pip install -r requirements.txt
主要依赖: 主要依赖:
- `google-adk` — Google Agent Development Kit - `google-adk` — Google Agent Development Kit
- `python-dotenv` — 环境变量管理 - `python-dotenv` — 环境变量管理
- `litellm` — 多模型适配层 - `litellm` — 多模型适配层(对接 MiniMax 等大模型)
### 3. 配置环境变量 ### 2. 配置环境变量
复制示例配置文件并填入你的 API Key
```bash ```bash
cp .env.example .env cp .env.example .env
``` ```
编辑 `.env` 文件 编辑 `.env`
```env ```env
# MiniMax API Key替换为你自己的
MINIMAX_API_KEY=your_api_key_here MINIMAX_API_KEY=your_api_key_here
# MiniMax API 地址
MINIMAX_API_BASE=https://api.minimaxi.com/v1 MINIMAX_API_BASE=https://api.minimaxi.com/v1
# 模型名称
MINIMAX_MODEL=openai/MiniMax-M2.7 MINIMAX_MODEL=openai/MiniMax-M2.7
``` ```
### 4. 启动服务 ### 3. 启动服务
```bash ```bash
cd agents # Web UI 模式(带聊天界面)
adk web adk web .
# API 服务模式(纯 REST API适合外部集成
adk api_server . --auto_create_session
``` ```
启动后访问 http://127.0.0.1:8000,在下拉菜单中选择 Agent 即可开始对话 启动后访问 `http://127.0.0.1:8000`
## 智能体架构
```
root_agent小慧
├── 通用对话:回答用户问题
└── 子智能体委派:
└── note_agent笔记助手
├── generate_text_note — 生成文本笔记
└── generate_image_note — 生成图片笔记
```
当用户请求涉及笔记生成、内容整理时root_agent 会自动委派给 note_agent 处理。
## API 集成
使用 `adk api_server` 启动后,可通过 REST API 与 Agent 对话:
```bash
# 启动 API 服务
adk api_server . --auto_create_session --port 8000
# 发送对话
curl -X POST http://localhost:8000/run \
-H "Content-Type: application/json" \
-d '{
"app_name": "root_agent",
"user_id": "user_001",
"session_id": "my_session",
"new_message": {"role": "user", "parts": [{"text": "你好"}]}
}'
```
详细的接口文档见 [API_DOC.md](API_DOC.md)Swagger 文档地址:`http://localhost:8000/docs`
## 内置能力 ## 内置能力
所有继承 `HuiYuBaseAgent` 的 Agent 自动获得以下能力 所有继承 `HuiYuBaseAgent` 的 Agent 自动获得:
### 防提示词注入 ### 防提示词注入
模型调用前自动检测用户输入中的提示词注入攻击,包括: 模型调用前自动检测用户输入中的注入攻击(角色扮演、指令泄露、分隔符注入、越狱尝试等),检测到时拒绝请求。
- 角色扮演 / 身份覆盖
- 指令泄露尝试
- 分隔符注入
- 越狱尝试
检测到注入时会拒绝请求并返回提示。
### 模型调用日志 ### 模型调用日志
每次模型调用完成后自动记录: 每次模型调用完成后自动记录 Agent 名称、模型版本、调用耗时、Token 用量
- Agent 名称
- 模型版本
- 调用耗时
- Token 使用量prompt / completion / total
日志示例:
``` ```
2026-04-06 07:54:59 [adk.agent] INFO - 模型调用完成 | agent=minimax_agent model=MiniMax-M2.7 latency=11.701s prompt_tokens=78 completion_tokens=31 total_tokens=109 2026-04-06 07:54:59 [adk.agent] INFO - 模型调用完成 | agent=huiyu_agent model=MiniMax-M2.7 latency=11.701s prompt_tokens=78 completion_tokens=31 total_tokens=109
``` ```
## 新增 Agent ## 新增 Agent
`agents/` 目录下创建新的子目录即可: 项目根目录下创建新的子目录即可:
``` ```
agents/ my_new_agent/
├── root_agent/ ├── __init__.py # 留空即可
│ ├── __init__.py └── agent.py # 必须导出名为 root_agent 的变量
│ └── agent.py
└── my_new_agent/ # 新增 Agent
├── __init__.py # 内容同 root_agent/__init__.py
└── agent.py
``` ```
`__init__.py` 内容(用于设置 Python 路径) `agent.py` 示例
```python ```python
import sys from agent_base.agent_base import HuiYuBaseAgent
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from . import agent
__all__ = ["agent"]
```
`agent.py` 中继承 `HuiYuBaseAgent`
```python
from base.agent import HuiYuBaseAgent
from google.adk.models.lite_llm import LiteLlm from google.adk.models.lite_llm import LiteLlm
import os import os
@@ -147,11 +143,26 @@ model = LiteLlm(
api_key=os.getenv("MINIMAX_API_KEY"), api_key=os.getenv("MINIMAX_API_KEY"),
) )
my_agent = HuiYuBaseAgent( root_agent = HuiYuBaseAgent(
name="my_agent", name="my_agent",
model=model, model=model,
description="我的智能体",
instruction="你是一个专业的助手。", instruction="你是一个专业的助手。",
) )
``` ```
重启 `adk web` 后,新 Agent 会自动出现在下拉菜单中。 > **注意**ADK 要求每个 agent 目录的 `agent.py` 中必须导出名为 `root_agent` 的变量。
如需作为 root_agent 的子智能体,在 `root_agent/agent.py` 中添加引用:
```python
from my_new_agent.agent import root_agent as my_new_agent
root_agent = HuiYuBaseAgent(
name="huiyu_agent",
...,
sub_agents=[my_new_agent],
)
```
重启服务后新 Agent 自动生效。

View File

@@ -1,10 +0,0 @@
"""
Agent 模块
每个 Agent 是一个独立的子目录,包含 agent.py 和 __init__.py。
"""
# 导出所有 Agent供 adk web 服务注册
from .root_agent import agent as root_agent
__all__ = ["root_agent"]

View File

@@ -1,8 +0,0 @@
import sys
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from . import agent
__all__ = ["agent"]

View File

@@ -1,10 +0,0 @@
import sys
from pathlib import Path
# 将项目根目录加入 sys.pathadk web 从 agents/ 启动)
# __file__ = agents/root_agent/__init__.py → parent.parent.parent = workspace/
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from . import agent
__all__ = ["agent"]

1
note_agent/__init__.py Normal file
View File

@@ -0,0 +1 @@
# ADK agent_loader 会自动扫描并加载 agent.py

View File

@@ -1,6 +1,6 @@
import os import os
from base.agent import HuiYuBaseAgent from agent_base.agent_base import HuiYuBaseAgent
from google.adk.models.lite_llm import LiteLlm from google.adk.models.lite_llm import LiteLlm
from .note_formatter import NoteFormatter from .note_formatter import NoteFormatter
@@ -55,7 +55,7 @@ def generate_image_note(title: str, description: str) -> str:
# ============================================================ # ============================================================
# 定义 Agent继承 HuiYuBaseAgent # 定义 Agent继承 HuiYuBaseAgent
# ============================================================ # ============================================================
note_agent = HuiYuBaseAgent( root_agent = HuiYuBaseAgent(
name="note_agent", name="note_agent",
model=model, model=model,
description="一个多模态笔记助手,能够将图片、文字等内容转换为结构化的 Markdown 笔记。", description="一个多模态笔记助手,能够将图片、文字等内容转换为结构化的 Markdown 笔记。",

1
root_agent/__init__.py Normal file
View File

@@ -0,0 +1 @@
# ADK agent_loader 会自动扫描并加载 agent.py

View File

@@ -1,7 +1,8 @@
import os import os
from base.agent import HuiYuBaseAgent from agent_base.agent_base import HuiYuBaseAgent
from google.adk.models.lite_llm import LiteLlm from google.adk.models.lite_llm import LiteLlm
from note_agent.agent import root_agent as note_agent
# ============================================================ # ============================================================
# MiniMax 模型配置(从 .env 文件读取) # MiniMax 模型配置(从 .env 文件读取)
@@ -26,5 +27,10 @@ root_agent = HuiYuBaseAgent(
name="huiyu_agent", name="huiyu_agent",
model=model, model=model,
description="一个智能助手,能够回答用户的各种问题。", description="一个智能助手,能够回答用户的各种问题。",
instruction="你是一个乐于助人的中文 AI 助手,你的名字叫小慧,请用中文回答用户的问题,回答要准确、简洁、友好。", instruction=(
"你是一个乐于助人的中文 AI 助手,你的名字叫小慧,请用中文回答用户的问题,回答要准确、简洁、友好。\n\n"
"你有一个子助手「笔记助手」note_agent当用户需要生成笔记、整理内容、做总结时"
"请将任务委派给 note_agent 处理。"
),
sub_agents=[note_agent],
) )