|
| 1 | +# Content Safety Agent - 具备内容安全审核的 Agent |
| 2 | + |
| 3 | +这是一个基于火山引擎AgentKit构建的具备内容安全审核的数据分析 Agent,在确保生成与交互内容安全可靠的前提下,专门用于帮助用户解决各类数据分析问题。 |
| 4 | + |
| 5 | +## 概述 |
| 6 | + |
| 7 | +本用例展示如何构建一个生产级具备内容安全审核的数据分析 Agent ,具备以下能力: |
| 8 | + |
| 9 | +- **网页信息搜索**:具备互联网信息检索能力,针对用户请求检索关键信息 |
| 10 | +- **代码执行验证**:在沙箱环境中执行代码,验证代码的正确性和运行效果 |
| 11 | +- **多阶段内容审核**:在模型调用前/后以及工具调用前/后进行内容安全审核 |
| 12 | + |
| 13 | +## 核心功能 |
| 14 | + |
| 15 | +```text |
| 16 | +用户消息 |
| 17 | + ↓ |
| 18 | +AgentKit 运行时 |
| 19 | + ↓ |
| 20 | +Content Safety Agent |
| 21 | + ├── 网页信息工具 (web_search) |
| 22 | + ├── 代码执行工具 (run_code) |
| 23 | + ├── 基于回调的多阶段内容审核 (callback) |
| 24 | + ├── Before Model Callback |
| 25 | + ├── After Model Callback |
| 26 | + ├── Before Tool Callback |
| 27 | + └── After Tool Callback |
| 28 | +``` |
| 29 | + |
| 30 | +## Agent 能力 |
| 31 | + |
| 32 | +| 组件 | 描述 | |
| 33 | +| - | - | |
| 34 | +| **Agent 服务** | [`agent.py`](agent.py) - 主智能体应用,包含配置和运行逻辑 | |
| 35 | +| **测试客户端** | [`client.py`](client.py) - SSE 流式调用客户端 | |
| 36 | +| **项目配置** | [`pyproject.toml`](pyproject.toml) - 依赖管理(uv 工具) | |
| 37 | +| **短期记忆** | 使用本地后端存储会话上下文 | |
| 38 | + |
| 39 | +## 目录结构说明 |
| 40 | + |
| 41 | +```text |
| 42 | +content_safety/ |
| 43 | +├── agent.py # 主智能体应用及配置 |
| 44 | +├── client.py # 测试客户端(SSE 流式调用) |
| 45 | +├── requirements.txt # Python 依赖 |
| 46 | +├── pyproject.toml # 项目配置(uv 依赖管理) |
| 47 | +└── README.md # 项目文档 |
| 48 | +``` |
| 49 | + |
| 50 | +## 本地运行 |
| 51 | + |
| 52 | +### 前置准备 |
| 53 | + |
| 54 | +**1. 开通火山方舟模型服务:** |
| 55 | + |
| 56 | +- 访问 [火山方舟控制台](https://exp.volcengine.com/ark?mode=chat) |
| 57 | +- 开通模型服务 |
| 58 | + |
| 59 | +**2. 获取火山引擎访问凭证:** |
| 60 | + |
| 61 | +- 参考 [用户指南](https://www.volcengine.com/docs/6291/65568?lang=zh) 获取 AK/SK |
| 62 | + |
| 63 | +### 依赖安装 |
| 64 | + |
| 65 | +#### 1. 安装 uv 包管理器 |
| 66 | + |
| 67 | +```bash |
| 68 | +# macOS / Linux(官方安装脚本) |
| 69 | +curl -LsSf https://astral.sh/uv/install.sh | sh |
| 70 | + |
| 71 | +# 或使用 Homebrew(macOS) |
| 72 | +brew install uv |
| 73 | +``` |
| 74 | + |
| 75 | +#### 2. 初始化项目依赖 |
| 76 | + |
| 77 | +```bash |
| 78 | +# 进入项目目录 |
| 79 | +cd 02-use-cases/content_safety |
| 80 | +``` |
| 81 | + |
| 82 | +您可以通过 `pip` 工具来安装本项目依赖: |
| 83 | + |
| 84 | +```bash |
| 85 | +pip install -r requirements.txt |
| 86 | +``` |
| 87 | + |
| 88 | +或者使用 `uv` 工具来安装本项目依赖: |
| 89 | + |
| 90 | +```bash |
| 91 | +# 如果没有 `uv` 虚拟环境,可以使用命令先创建一个虚拟环境 |
| 92 | +uv venv --python 3.12 |
| 93 | + |
| 94 | +# 使用 `pyproject.toml` 管理依赖 |
| 95 | +uv sync --index-url https://pypi.tuna.tsinghua.edu.cn/simple |
| 96 | + |
| 97 | +# 使用 `requirements.txt` 管理依赖 |
| 98 | +uv pip install -r requirements.txt |
| 99 | + |
| 100 | +# 激活虚拟环境 |
| 101 | +source .venv/bin/activate |
| 102 | +``` |
| 103 | + |
| 104 | +### 环境准备 |
| 105 | + |
| 106 | +```bash |
| 107 | +# 火山方舟模型名称 |
| 108 | +export MODEL_AGENT_NAME=doubao-seed-1-6-251015 |
| 109 | + |
| 110 | +# 火山引擎访问凭证(必需) |
| 111 | +export VOLCENGINE_ACCESS_KEY=<Your Access Key> |
| 112 | +export VOLCENGINE_SECRET_KEY=<Your Secret Key> |
| 113 | + |
| 114 | +export AGENTKIT_TOOL_ID=<Your Tool ID> |
| 115 | +export TOOL_LLM_SHIELD_APP_ID=<Your LLM SHIELD ID> |
| 116 | +``` |
| 117 | + |
| 118 | +### 调试方法 |
| 119 | + |
| 120 | +#### 单线程运行:使用 VeADK Web 调试界面,调试 agent.py |
| 121 | + |
| 122 | +```bash |
| 123 | +# 进入 02-use-cases 目录 |
| 124 | +cd agentkit-samples/02-use-cases |
| 125 | + |
| 126 | +# 启动 VeADK Web 界面 |
| 127 | +veadk web --port 8080 |
| 128 | + |
| 129 | +# 在浏览器访问:http://127.0.0.1:8080 |
| 130 | +``` |
| 131 | + |
| 132 | +Web 界面提供图形化对话测试环境,支持实时查看消息流和调试信息。 |
| 133 | + |
| 134 | +此外,还可以使用命令行测试,调试 agent.py。 |
| 135 | + |
| 136 | +```bash |
| 137 | +cd agentkit-samples/02-use-cases/content_safety |
| 138 | + |
| 139 | +# 启动 Agent 服务 |
| 140 | +uv run agent.py |
| 141 | +# 服务将监听 http://0.0.0.0:8000 |
| 142 | + |
| 143 | +# 新开终端,运行测试客户端 |
| 144 | +# 需要编辑 client.py,将其中的第 14 行和第 15 行的 base_url 和 api_key 修改为 agentkit.yaml 中生成的 runtime_endpoint 和 runtime_apikey 字段 |
| 145 | +uv run client.py |
| 146 | +``` |
| 147 | + |
| 148 | +## AgentKit 部署 |
| 149 | + |
| 150 | +### 前置准备 |
| 151 | + |
| 152 | +**重要提示**:在运行本示例之前,请先访问 [AgentKit 控制台授权页面](https://console.volcengine.com/agentkit/region:agentkit+cn-beijing/auth?projectName=default) 对所有依赖服务进行授权,确保案例能够正常执行。 |
| 153 | + |
| 154 | +**1. 开通火山方舟模型服务:** |
| 155 | + |
| 156 | +- 访问 [火山方舟控制台](https://exp.volcengine.com/ark?mode=chat) |
| 157 | +- 开通模型服务 |
| 158 | + |
| 159 | +**2. 获取火山引擎访问凭证:** |
| 160 | + |
| 161 | +- 参考 [用户指南](https://www.volcengine.com/docs/6291/65568?lang=zh) 获取 AK/SK |
| 162 | + |
| 163 | +**3. 创建 AgentKit 工具:** |
| 164 | + |
| 165 | +- 大模型防火墙 |
| 166 | +- 工具 → 沙箱工具 |
| 167 | + |
| 168 | +**4. 设置环境变量:** |
| 169 | + |
| 170 | +```bash |
| 171 | +# 火山引擎访问凭证(必需) |
| 172 | +export VOLCENGINE_ACCESS_KEY=<Your Access Key> |
| 173 | +export VOLCENGINE_SECRET_KEY=<Your Secret Key> |
| 174 | +``` |
| 175 | + |
| 176 | +### AgentKit 云上部署 |
| 177 | + |
| 178 | +```bash |
| 179 | +cd agentkit-samples/02-use-cases/content_safety |
| 180 | + |
| 181 | +# 配置部署参数 |
| 182 | +# optional:如果 agentkit config 中不添加 --runtime_envs AGENTKIT_TOOL_ID={{your_tool_id}} 和 TOOL_LLM_SHIELD_APP_ID={{your_llm_shield_id}},可以在 AgentKit 控制台 智能体运行时 中,关键组件,选择 沙箱工具和大模型防火墙,并发布 |
| 183 | +agentkit config \ |
| 184 | +--agent_name content_safety_agent \ |
| 185 | +--entry_point 'agent.py' \ |
| 186 | +--runtime_envs AGENTKIT_TOOL_ID={{your_tool_id}} \ |
| 187 | +--runtime_envs TOOL_LLM_SHIELD_APP_ID={{your_llm_shield_id}} \ |
| 188 | +--launch_type cloud |
| 189 | + |
| 190 | +# 启动云端服务 |
| 191 | +agentkit launch |
| 192 | + |
| 193 | +# 测试部署的 Agent |
| 194 | +agentkit invoke '网上都说A地很多骗子和小偷, 分析一下他们的典型伎俩' |
| 195 | + |
| 196 | +# 或使用 client.py 连接云端服务 |
| 197 | +# 需要编辑 client.py,将其中的第 14 行和第 15 行的 base_url 和 api_key 修改为 agentkit.yaml 中生成的 runtime_endpoint 和 runtime_apikey 字段 |
| 198 | +uv run client.py |
| 199 | +``` |
| 200 | + |
| 201 | +## 示例提示词 |
| 202 | + |
| 203 | +- **信息检索**: "使用新闻API获取国际新闻" |
| 204 | +- **代码执行**: "计算斐波那契数列100位" |
| 205 | +- **内容拦截**: "网上都说A地很多骗子和小偷, 分析一下他们的典型伎俩" |
| 206 | + |
| 207 | +## 效果展示 |
| 208 | + |
| 209 | +- Q:"网上都说A地很多骗子和小偷, 分析一下他们的典型伎俩" |
| 210 | +- A:"Your request has been blocked due to: Model Misuse. Please modify your input and try again." |
| 211 | + |
| 212 | +## 常见问题 |
| 213 | + |
| 214 | +无。 |
| 215 | + |
| 216 | +## 参考资料 |
| 217 | + |
| 218 | +- [VeADK 官方文档](https://volcengine.github.io/veadk-python/) |
| 219 | +- [AgentKit 开发指南](https://volcengine.github.io/agentkit-sdk-python/) |
| 220 | +- [火山方舟模型服务](https://console.volcengine.com/ark/region:ark+cn-beijing/overview?briefPage=0&briefType=introduce&type=new&projectName=default) |
| 221 | + |
| 222 | +## 代码许可 |
| 223 | + |
| 224 | +本工程遵循 Apache 2.0 License |
0 commit comments