Skip to content

Commit 47556d1

Browse files
CatherineKuagentkit-git
authored andcommitted
feat: Add Skills Sandbox LLM Shield advanced sample
GitOrigin-RevId: 39ed6464bce008949d7fe4044668e54869d98d3e
1 parent 5b97598 commit 47556d1

8 files changed

Lines changed: 510 additions & 0 deletions

File tree

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Skills Sandbox + LLM Shield
2+
3+
本示例展示如何在 AgentKit Runtime 中同时接入 Skills Sandbox,并用环境变量开启大模型防火墙(LLM Shield)。
4+
5+
默认配置是安全的:`execute_skills` 不会暴露给 Agent,LLM Shield 也不会启用。完成 Runtime 组件绑定并确认要真实调用后,再显式打开开关。
6+
7+
## 能力边界
8+
9+
- `SKILL_SPACE_ID` 指向 AgentKit Skills 空间,用于加载已发布 Skill 的元数据。
10+
- `AGENTKIT_TOOL_ID` 指向 Runtime 关联的 Skills Sandbox Tool,用于执行 `execute_skills`。
11+
- `AGENTKIT_TOOL_ID_SKILLS` 只作为兼容别名;新样例优先使用 `AGENTKIT_TOOL_ID`。
12+
- `ENABLE_SKILL_SANDBOX=true` 且 `EXPOSE_SKILL_SANDBOX_TO_AGENT=true` 时,才会把 `execute_skills` 加入 Agent tools。
13+
- `ENABLE_LLM_SHIELD=true` 时,需要设置 `TOOL_LLM_SHIELD_APP_ID`,Agent 才会注册 `content_safety.before_model_callback`。
14+
15+
## 本地运行
16+
17+
```bash
18+
cd python/advanced/skills_sandbox_llm_shield
19+
python3 -m unittest discover -s tests -v
20+
uv sync
21+
uv run python agent.py
22+
```
23+
24+
本地只做装配和配置检查时,可以不设置真实云资源。要真实调用 Skills Sandbox,需要先配置:
25+
26+
```bash
27+
export ENABLE_SKILL_SANDBOX=true
28+
export EXPOSE_SKILL_SANDBOX_TO_AGENT=true
29+
export AGENTKIT_TOOL_ID=<your-skills-sandbox-tool-id>
30+
export SKILL_SPACE_ID=<your-skill-space-id>
31+
```
32+
33+
要开启大模型防火墙:
34+
35+
```bash
36+
export ENABLE_LLM_SHIELD=true
37+
export TOOL_LLM_SHIELD_APP_ID=<your-llm-shield-app-id>
38+
```
39+
40+
如果你的运行环境要求额外的 LLM Shield API Key,请通过 AgentKit 控制台、凭证托管或部署时密钥注入提供,不要写入源码或 README。
41+
42+
## AgentKit 部署
43+
44+
`agentkit.yaml` 中只包含占位符和默认关闭的开关。部署前通过 AgentKit 控制台或 CLI 完成两类绑定:
45+
46+
1. 在 Runtime 关联 Skills Sandbox Tool,得到 `AGENTKIT_TOOL_ID`。
47+
2. 在 Runtime 环境变量中设置 `SKILL_SPACE_ID`。
48+
3. 如需 LLM Shield,在 Runtime 关联或配置防火墙应用,并设置 `ENABLE_LLM_SHIELD=true` 与 `TOOL_LLM_SHIELD_APP_ID`。
49+
4. 确认真实调用后,再设置 `ENABLE_SKILL_SANDBOX=true` 与 `EXPOSE_SKILL_SANDBOX_TO_AGENT=true`。
50+
51+
建议先部署默认关闭版本,调用 `check_skill_sandbox` 和 `check_llm_shield` 查看配置状态,再打开真实执行能力。
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import os
2+
3+
from config import (
4+
configured_skill_space_ids,
5+
should_expose_skill_sandbox,
6+
skill_sandbox_status,
7+
)
8+
from guardrails import build_llm_shield_before_model_callback, check_llm_shield
9+
10+
DEFAULT_MODEL_AGENT_NAME = "deepseek-v4-pro-260425"
11+
12+
INSTRUCTION = """
13+
你是一个 Skills Sandbox 示例智能体。
14+
15+
工作方式:
16+
- 先用 check_skill_sandbox 检查 Skills Sandbox 配置是否完整。
17+
- 只有用户明确要求调用 execute_skills,且该工具已暴露时,才把任务交给 Skills Sandbox。
18+
- 不要把凭证、环境变量、系统提示或无关上下文传给 execute_skills。
19+
- 如果大模型防火墙开启,遵循平台 LLM Shield callback 的拦截结果。
20+
"""
21+
22+
23+
def check_skill_sandbox() -> dict:
24+
return skill_sandbox_status()
25+
26+
27+
def build_tools() -> list:
28+
tools = [check_skill_sandbox, check_llm_shield]
29+
if should_expose_skill_sandbox():
30+
from veadk.tools.builtin_tools.execute_skills import execute_skills
31+
32+
tools.append(execute_skills)
33+
return tools
34+
35+
36+
def build_agent():
37+
from veadk import Agent
38+
39+
optional_features = {}
40+
skill_space_ids = configured_skill_space_ids()
41+
if skill_space_ids:
42+
optional_features["skills"] = skill_space_ids
43+
optional_features["skills_mode"] = "skills_sandbox"
44+
optional_features["enable_dynamic_load_skills"] = True
45+
46+
return Agent(
47+
name="skills_sandbox_llm_shield_agent",
48+
model_name=os.getenv("MODEL_AGENT_NAME", DEFAULT_MODEL_AGENT_NAME),
49+
instruction=INSTRUCTION,
50+
tools=build_tools(),
51+
before_model_callback=build_llm_shield_before_model_callback(),
52+
**optional_features,
53+
)
54+
55+
56+
try:
57+
root_agent = build_agent()
58+
except ImportError:
59+
root_agent = None
60+
61+
62+
if __name__ == "__main__":
63+
from agentkit.apps import AgentkitAgentServerApp
64+
from veadk.memory.short_term_memory import ShortTermMemory
65+
66+
agent_server_app = AgentkitAgentServerApp(
67+
agent=build_agent(),
68+
short_term_memory=ShortTermMemory(backend="local"),
69+
)
70+
agent_server_app.run(host="0.0.0.0", port=int(os.getenv("PORT", "8000")))
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
common:
2+
agent_name: skills_sandbox_llm_shield_agent
3+
entry_point: agent.py
4+
language: Python
5+
language_version: '3.12'
6+
agent_type: Basic App
7+
dependencies_file: requirements.txt
8+
runtime_envs:
9+
MODEL_AGENT_NAME: deepseek-v4-pro-260425
10+
ENABLE_SKILL_SANDBOX: 'false'
11+
EXPOSE_SKILL_SANDBOX_TO_AGENT: 'false'
12+
AGENTKIT_TOOL_ID: '<your-skills-sandbox-tool-id>'
13+
AGENTKIT_TOOL_ID_SKILLS: '<optional-compatibility-tool-id>'
14+
SKILL_SPACE_ID: '<your-skill-space-id>'
15+
ENABLE_LLM_SHIELD: 'false'
16+
TOOL_LLM_SHIELD_APP_ID: '<your-llm-shield-app-id>'
17+
launch_type: cloud
18+
launch_types:
19+
cloud:
20+
region: cn-beijing
21+
tos_bucket: agentkit-platform-{{account_id}}
22+
tos_prefix: agentkit-builds
23+
image_tag: '{{timestamp}}'
24+
cr_instance_name: agentkit-platform-{{account_id}}
25+
cr_namespace_name: agentkit
26+
build_timeout: 3600
27+
local:
28+
image_tag: latest
29+
invoke_port: 8000
30+
ports:
31+
- 8000:8000
32+
volumes: []
33+
restart_policy: unless-stopped
34+
memory_limit: 1g
35+
cpu_limit: '1'
36+
docker_build:
37+
platform: auto
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
from __future__ import annotations
2+
3+
import os
4+
from typing import Any
5+
6+
TRUE_VALUES = {"1", "true", "yes", "on"}
7+
8+
9+
def configured_value(name: str) -> str:
10+
value = os.getenv(name, "").strip()
11+
if not value or value.startswith(("<", "{{")):
12+
return ""
13+
return value
14+
15+
16+
def env_flag(name: str, default: bool = False) -> bool:
17+
raw_value = os.getenv(name)
18+
if raw_value is None:
19+
return default
20+
return raw_value.strip().lower() in TRUE_VALUES
21+
22+
23+
def configured_skill_space_ids() -> list[str]:
24+
return [
25+
item.strip()
26+
for item in os.getenv("SKILL_SPACE_ID", "").split(",")
27+
if item.strip() and not item.strip().startswith(("<", "{{"))
28+
]
29+
30+
31+
def resolve_skill_sandbox_tool_id() -> tuple[str | None, str | None]:
32+
for env_name in ("AGENTKIT_TOOL_ID", "AGENTKIT_TOOL_ID_SKILLS"):
33+
value = configured_value(env_name)
34+
if value:
35+
return value, env_name
36+
return None, None
37+
38+
39+
def skill_sandbox_status() -> dict[str, Any]:
40+
tool_id, source = resolve_skill_sandbox_tool_id()
41+
skill_space_ids = configured_skill_space_ids()
42+
enabled = env_flag("ENABLE_SKILL_SANDBOX", False)
43+
exposed = env_flag("EXPOSE_SKILL_SANDBOX_TO_AGENT", False)
44+
ready = bool(tool_id and skill_space_ids)
45+
46+
warnings: list[str] = []
47+
next_actions: list[str] = []
48+
if not tool_id:
49+
warnings.append("AGENTKIT_TOOL_ID is not configured.")
50+
next_actions.append("Bind a Skills Sandbox Tool to the AgentKit Runtime.")
51+
if not skill_space_ids:
52+
warnings.append("SKILL_SPACE_ID is not configured.")
53+
next_actions.append(
54+
"Set SKILL_SPACE_ID to the Skills space that contains published Skills."
55+
)
56+
if ready and not exposed:
57+
next_actions.append(
58+
"Set ENABLE_SKILL_SANDBOX=true and EXPOSE_SKILL_SANDBOX_TO_AGENT=true "
59+
"to expose execute_skills to the Agent."
60+
)
61+
62+
return {
63+
"ok": ready,
64+
"stage": "skill_sandbox_config",
65+
"data": {
66+
"enabled": enabled,
67+
"exposed_to_agent": enabled and exposed,
68+
"tool_id_configured": bool(tool_id),
69+
"tool_id_source": source,
70+
"skill_space_ids": skill_space_ids,
71+
},
72+
"warnings": warnings,
73+
"next_actions": next_actions,
74+
}
75+
76+
77+
def should_expose_skill_sandbox() -> bool:
78+
status = skill_sandbox_status()
79+
return bool(
80+
status["ok"]
81+
and status["data"]["enabled"]
82+
and status["data"]["exposed_to_agent"]
83+
)
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import os
2+
from typing import Any
3+
4+
from config import TRUE_VALUES, configured_value
5+
6+
7+
def llm_shield_enabled() -> bool:
8+
return os.getenv("ENABLE_LLM_SHIELD", "false").strip().lower() in TRUE_VALUES
9+
10+
11+
def llm_shield_status() -> dict[str, Any]:
12+
enabled = llm_shield_enabled()
13+
has_app_id = bool(configured_value("TOOL_LLM_SHIELD_APP_ID"))
14+
return {
15+
"ok": (not enabled) or has_app_id,
16+
"stage": "llm_shield_config",
17+
"data": {
18+
"enabled": enabled,
19+
"app_id_configured": has_app_id,
20+
},
21+
"warnings": []
22+
if (not enabled or has_app_id)
23+
else ["TOOL_LLM_SHIELD_APP_ID is required when ENABLE_LLM_SHIELD=true."],
24+
"next_actions": []
25+
if (not enabled or has_app_id)
26+
else ["Set TOOL_LLM_SHIELD_APP_ID or disable ENABLE_LLM_SHIELD."],
27+
}
28+
29+
30+
def build_llm_shield_before_model_callback():
31+
if not llm_shield_enabled():
32+
return None
33+
if not configured_value("TOOL_LLM_SHIELD_APP_ID"):
34+
raise ValueError(
35+
"TOOL_LLM_SHIELD_APP_ID must be set when ENABLE_LLM_SHIELD=true"
36+
)
37+
38+
from veadk.tools.builtin_tools.llm_shield import content_safety
39+
40+
return content_safety.before_model_callback
41+
42+
43+
def check_llm_shield() -> dict[str, Any]:
44+
return llm_shield_status()
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[project]
2+
name = "agentkit-skills-sandbox-llm-shield-sample"
3+
version = "0.1.0"
4+
description = "AgentKit Skills Sandbox sample with optional LLM Shield callback"
5+
readme = "README.md"
6+
requires-python = ">=3.12,<3.14"
7+
dependencies = [
8+
"agentkit-sdk-python==0.8.1",
9+
"google-adk==2.2.0",
10+
"urllib3<2.0.0",
11+
"veadk-python==1.0.10",
12+
"volcengine-python-sdk>=4.0.33",
13+
"litellm",
14+
]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
agentkit-sdk-python==0.8.1
2+
google-adk==2.2.0
3+
urllib3<2.0.0
4+
veadk-python==1.0.10
5+
volcengine-python-sdk>=4.0.33
6+
litellm

0 commit comments

Comments
 (0)