ni.agentkit 0.3.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (72) hide show
  1. agentkit/__init__.py +57 -0
  2. agentkit/_cli.py +72 -0
  3. agentkit/agents/__init__.py +0 -0
  4. agentkit/agents/agent.py +364 -0
  5. agentkit/agents/base_agent.py +59 -0
  6. agentkit/agents/orchestrators.py +62 -0
  7. agentkit/docs/Architecture.md +536 -0
  8. agentkit/docs/QuickStart.md +806 -0
  9. agentkit/docs/README.md +119 -0
  10. agentkit/docs/Reference.md +576 -0
  11. agentkit/docs/TestReport.md +80 -0
  12. agentkit/examples/__init__.py +0 -0
  13. agentkit/examples/ollama/01_basic_chat.py +34 -0
  14. agentkit/examples/ollama/02_tool_calling.py +70 -0
  15. agentkit/examples/ollama/03_skill_usage.py +86 -0
  16. agentkit/examples/ollama/04_multi_agent.py +84 -0
  17. agentkit/examples/ollama/05_guardrail.py +101 -0
  18. agentkit/examples/ollama/06_orchestration.py +123 -0
  19. agentkit/examples/ollama/07_sync_async_stream.py +180 -0
  20. agentkit/examples/ollama/08_memory.py +201 -0
  21. agentkit/examples/ollama/README.md +51 -0
  22. agentkit/examples/ollama/__init__.py +0 -0
  23. agentkit/examples/quickstart.py +204 -0
  24. agentkit/examples/standard/01_basic_chat.py +33 -0
  25. agentkit/examples/standard/02_tool_calling.py +70 -0
  26. agentkit/examples/standard/03_skill_usage.py +89 -0
  27. agentkit/examples/standard/04_multi_agent.py +87 -0
  28. agentkit/examples/standard/05_guardrail.py +104 -0
  29. agentkit/examples/standard/06_orchestration.py +122 -0
  30. agentkit/examples/standard/07_sync_async_stream.py +171 -0
  31. agentkit/examples/standard/08_memory.py +140 -0
  32. agentkit/examples/standard/README.md +31 -0
  33. agentkit/examples/standard/__init__.py +0 -0
  34. agentkit/examples/test_ollama.py +272 -0
  35. agentkit/llm/__init__.py +0 -0
  36. agentkit/llm/adapters/__init__.py +0 -0
  37. agentkit/llm/adapters/anthropic_adapter.py +192 -0
  38. agentkit/llm/adapters/google_adapter.py +184 -0
  39. agentkit/llm/adapters/ollama_adapter.py +250 -0
  40. agentkit/llm/adapters/openai_adapter.py +183 -0
  41. agentkit/llm/adapters/openai_compatible.py +35 -0
  42. agentkit/llm/base.py +66 -0
  43. agentkit/llm/cache.py +121 -0
  44. agentkit/llm/middleware.py +133 -0
  45. agentkit/llm/registry.py +138 -0
  46. agentkit/llm/types.py +191 -0
  47. agentkit/memory/__init__.py +0 -0
  48. agentkit/memory/base.py +47 -0
  49. agentkit/memory/mem0_provider.py +48 -0
  50. agentkit/runner/__init__.py +0 -0
  51. agentkit/runner/context.py +47 -0
  52. agentkit/runner/events.py +36 -0
  53. agentkit/runner/runner.py +105 -0
  54. agentkit/safety/__init__.py +0 -0
  55. agentkit/safety/guardrails.py +55 -0
  56. agentkit/safety/permissions.py +41 -0
  57. agentkit/skills/__init__.py +0 -0
  58. agentkit/skills/loader.py +90 -0
  59. agentkit/skills/models.py +106 -0
  60. agentkit/skills/registry.py +48 -0
  61. agentkit/tools/__init__.py +0 -0
  62. agentkit/tools/base_tool.py +44 -0
  63. agentkit/tools/function_tool.py +118 -0
  64. agentkit/tools/skill_toolset.py +199 -0
  65. agentkit/utils/__init__.py +0 -0
  66. agentkit/utils/schema.py +83 -0
  67. ni_agentkit-0.3.1.dist-info/METADATA +157 -0
  68. ni_agentkit-0.3.1.dist-info/RECORD +72 -0
  69. ni_agentkit-0.3.1.dist-info/WHEEL +5 -0
  70. ni_agentkit-0.3.1.dist-info/entry_points.txt +2 -0
  71. ni_agentkit-0.3.1.dist-info/licenses/LICENSE +21 -0
  72. ni_agentkit-0.3.1.dist-info/top_level.txt +1 -0
@@ -0,0 +1,119 @@
1
+ # AgentKit
2
+
3
+ > Python 原生的 Agent 开发框架,内置一等公民级别的 Skill 支持和自研多模型适配层。
4
+
5
+ [![Python](https://img.shields.io/badge/Python-≥3.11-blue.svg)](https://python.org)
6
+ [![Version](https://img.shields.io/badge/Version-0.3.1-green.svg)]()
7
+ [![License](https://img.shields.io/badge/License-MIT-yellow.svg)]()
8
+
9
+ ---
10
+
11
+ ## ✨ 特性一览
12
+
13
+ | 特性 | 说明 |
14
+ |------|------|
15
+ | **Skill 一等公民** | `skills=[...]` 与 `tools=[...]` 并列,三级渐进式加载(L1/L2/L3)节省 token |
16
+ | **自研多模型适配** | OpenAI / Anthropic / Google Gemini / Ollama / 国内模型(DeepSeek、通义千问、智谱…),前缀自动路由 |
17
+ | **双协作模式** | Handoff(控制权转移)+ as_tool(Agent 当工具调用),灵活覆盖所有协作场景 |
18
+ | **编排 Agent** | SequentialAgent / ParallelAgent / LoopAgent,组合出任意复杂的工作流 |
19
+ | **@function_tool** | 一行装饰器把 Python 函数变成 LLM 工具,自动推断 JSON Schema |
20
+ | **安全内置** | Input/Output 双向 Guardrail + 三层权限控制 + 三级沙箱执行 |
21
+ | **记忆系统** | Mem0 集成,跨会话长期记忆 |
22
+ | **7 个回调点** | before/after × agent/model/tool + error,任何环节可拦截定制 |
23
+
24
+ ---
25
+
26
+ ## 📦 安装
27
+
28
+ ```bash
29
+ # 基础安装
30
+ pip install pydantic>=2.0
31
+
32
+ # 按需安装 LLM 适配器
33
+ pip install openai>=1.0.0 # OpenAI + 国内兼容厂商
34
+ pip install anthropic>=0.30.0 # Anthropic Claude
35
+ pip install google-genai>=1.0.0 # Google Gemini
36
+ pip install aiohttp>=3.9.0 # Ollama 本地模型
37
+
38
+ # 可选
39
+ pip install mem0ai>=0.1.0 # 记忆系统
40
+ pip install docker>=7.0.0 # Docker 沙箱
41
+ ```
42
+
43
+ ---
44
+
45
+ ## 🚀 30 秒快速开始
46
+
47
+ ```python
48
+ from agentkit import Agent, Runner, function_tool
49
+
50
+ # 1. 定义工具
51
+ @function_tool
52
+ def calculate(expression: str) -> str:
53
+ """计算数学表达式"""
54
+ return str(eval(expression))
55
+
56
+ # 2. 创建 Agent
57
+ agent = Agent(
58
+ name="assistant",
59
+ instructions="你是一个有帮助的中文助手。需要计算时请使用工具。",
60
+ model="ollama/qwen3.5:cloud", # 或 "gpt-4o"、"claude-sonnet-4-20250514"、"deepseek/deepseek-chat"
61
+ tools=[calculate],
62
+ )
63
+
64
+ # 3. 运行
65
+ result = Runner.run_sync(agent, input="请计算 (15 + 27) * 3")
66
+ print(result.final_output)
67
+ ```
68
+
69
+ ---
70
+
71
+ ## 📖 文档目录
72
+
73
+ | 文档 | 说明 |
74
+ |------|------|
75
+ | **[QuickStart.md](QuickStart.md)** | 详细入门教程,包含 8 个从简到繁的完整示例 |
76
+ | **[Architecture.md](Architecture.md)** | 架构设计说明:六层分层、设计原则、核心流程 |
77
+ | **[Reference.md](Reference.md)** | 完整 API 参考手册:所有类、方法、参数说明 |
78
+
79
+ ---
80
+
81
+ ## 🤖 支持的 LLM
82
+
83
+ 使用模型标识字符串即可自动路由到对应适配器,**零配置**:
84
+
85
+ | 模型标识 | 适配器 | 示例 |
86
+ |---------|--------|------|
87
+ | `gpt-4o`、`gpt-4o-mini`、`o1`、`o3`、`o4` | OpenAIAdapter | `model="gpt-4o"` |
88
+ | `claude-sonnet-4-20250514`、`claude-opus-4-20250514` | AnthropicAdapter | `model="claude-sonnet-4-20250514"` |
89
+ | `gemini-2.5-pro`、`gemini-2.5-flash` | GoogleAdapter | `model="gemini-2.5-pro"` |
90
+ | `ollama/qwen3.5:cloud`、`ollama/qwen3.5:4b` | OllamaAdapter | `model="ollama/qwen3.5:cloud"` |
91
+ | `deepseek/deepseek-chat` | OpenAICompatibleAdapter | `model="deepseek/deepseek-chat"` |
92
+ | `qwen/qwen-max` | OpenAICompatibleAdapter | `model="qwen/qwen-max"` |
93
+ | `zhipu/glm-4` | OpenAICompatibleAdapter | `model="zhipu/glm-4"` |
94
+ | `baichuan/baichuan2-turbo` | OpenAICompatibleAdapter | `model="baichuan/baichuan2-turbo"` |
95
+ | `azure/your-deployment` | OpenAICompatibleAdapter | `model="azure/your-deployment"` |
96
+
97
+ ---
98
+
99
+ ## 🏗️ 项目结构
100
+
101
+ ```
102
+ agentkit/
103
+ ├── agents/ # Agent 层(BaseAgent + Agent + 编排器)
104
+ ├── tools/ # Tool 层(BaseTool + @function_tool + SkillToolset)
105
+ ├── skills/ # Skill 层(数据模型 + 加载器 + 注册中心)
106
+ ├── llm/ # LLM 适配层(5 个适配器 + Registry + 中间件)
107
+ ├── runner/ # Runner 层(核心循环 + 上下文 + 事件)
108
+ ├── safety/ # 安全层(Guardrail + 权限控制)
109
+ ├── memory/ # 记忆系统(Mem0 集成)
110
+ ├── utils/ # 工具函数(JSON Schema 生成)
111
+ ├── examples/ # 使用示例
112
+ └── docs/ # 文档
113
+ ```
114
+
115
+ ---
116
+
117
+ ## 📄 许可证
118
+
119
+ MIT License
@@ -0,0 +1,576 @@
1
+ # AgentKit API 参考手册
2
+
3
+ > 本文档覆盖 AgentKit 所有公共 API 的完整参数说明和用法示例。
4
+
5
+ ---
6
+
7
+ ## 目录
8
+
9
+ - [Agent 类](#agent-类)
10
+ - [Agent](#agent)
11
+ - [BaseAgent](#baseagent)
12
+ - [SequentialAgent / ParallelAgent / LoopAgent](#编排-agent)
13
+ - [Runner 类](#runner-类)
14
+ - [Runner](#runner)
15
+ - [RunResult](#runresult)
16
+ - [Event](#event)
17
+ - [Tool 类](#tool-类)
18
+ - [function_tool 装饰器](#function_tool-装饰器)
19
+ - [FunctionTool](#functiontool)
20
+ - [BaseTool / BaseToolset](#basetool--basetoolset)
21
+ - [Skill 类](#skill-类)
22
+ - [Skill](#skill)
23
+ - [SkillFrontmatter](#skillfrontmatter)
24
+ - [load_skill_from_dir](#load_skill_from_dir)
25
+ - [SkillRegistry](#skillregistry)
26
+ - [LLM 类](#llm-类)
27
+ - [LLMRegistry](#llmregistry)
28
+ - [LLMConfig](#llmconfig)
29
+ - [BaseLLM](#basellm)
30
+ - [Message / ToolCall / LLMResponse](#消息与响应类型)
31
+ - [安全类](#安全类)
32
+ - [InputGuardrail / OutputGuardrail](#guardrail)
33
+ - [PermissionPolicy](#permissionpolicy)
34
+ - [记忆类](#记忆类)
35
+ - [BaseMemoryProvider / Memory](#basememoryprovider)
36
+
37
+ ---
38
+
39
+ ## Agent 类
40
+
41
+ ### Agent
42
+
43
+ 核心 LLM Agent,开发者最常使用的类。
44
+
45
+ ```python
46
+ from agentkit import Agent
47
+ ```
48
+
49
+ **构造参数**:
50
+
51
+ | 参数 | 类型 | 默认值 | 说明 |
52
+ |------|------|--------|------|
53
+ | `name` | `str` | **必填** | Agent 名称 |
54
+ | `description` | `str` | `""` | 描述(Handoff 和发现时使用) |
55
+ | `model` | `str \| LLMConfig \| BaseLLM` | `""` | 模型标识。空字符串表示向上继承父 Agent 的模型 |
56
+ | `instructions` | `str \| Callable` | `""` | 系统提示词。可以是字符串或动态函数 `(ctx, agent) -> str` |
57
+ | `tools` | `list` | `[]` | 工具列表,接受 `BaseTool`、`BaseToolset` 或普通 `Callable` |
58
+ | `skills` | `list[Skill]` | `[]` | Skill 列表 |
59
+ | `handoffs` | `list[Agent]` | `[]` | Handoff 目标 Agent 列表 |
60
+ | `output_type` | `type \| None` | `None` | 结构化输出类型(Pydantic 模型) |
61
+ | `input_guardrails` | `list[InputGuardrail]` | `[]` | 输入安全护栏 |
62
+ | `output_guardrails` | `list[OutputGuardrail]` | `[]` | 输出安全护栏 |
63
+ | `permission_policy` | `PermissionPolicy \| None` | `None` | 工具权限策略 |
64
+ | `memory` | `BaseMemoryProvider \| None` | `None` | 记忆提供者 |
65
+ | `tool_use_behavior` | `str` | `"run_llm_again"` | 工具调用后行为:`"run_llm_again"` 或 `"stop"` |
66
+ | `max_tool_rounds` | `int` | `20` | 单次运行最大工具调用轮次 |
67
+ | `enable_cache` | `bool` | `True` | LLM 响应缓存,默认开启。对相同输入直接返回缓存结果,缓存绑定 Agent 实例生命周期。仅缓存纯文本回复,不缓存工具调用响应 |
68
+ | `cache_ttl` | `int` | `300` | 缓存有效期(秒)。过期条目自动淘汰 |
69
+ | `memory_async_write` | `bool` | `True` | 记忆写入模式。`True`=fire-and-forget 异步写入(不阻塞返回);`False`=同步等待写入完成(多轮串行对话推荐) |
70
+ | `before_agent_callback` | `Callable \| None` | `None` | Agent 运行前回调 |
71
+ | `after_agent_callback` | `Callable \| None` | `None` | Agent 运行后回调 |
72
+ | `before_model_callback` | `Callable \| None` | `None` | LLM 调用前回调 |
73
+ | `after_model_callback` | `Callable \| None` | `None` | LLM 调用后回调 |
74
+ | `before_tool_callback` | `Callable \| None` | `None` | 工具调用前回调 |
75
+ | `after_tool_callback` | `Callable \| None` | `None` | 工具调用后回调 |
76
+ | `on_error_callback` | `Callable \| None` | `None` | 错误回调 |
77
+
78
+ **方法**:
79
+
80
+ | 方法 | 签名 | 说明 |
81
+ |------|------|------|
82
+ | `as_tool` | `(name: str, description: str) -> FunctionTool` | 将自身包装为工具,供其他 Agent 调用 |
83
+ | `get_instructions` | `async (ctx) -> str` | 获取系统提示词(动态解析) |
84
+ | `get_all_tools` | `async (ctx) -> list[BaseTool]` | 汇总所有工具(tools + skills + handoffs) |
85
+
86
+ **示例**:
87
+
88
+ ```python
89
+ agent = Agent(
90
+ name="assistant",
91
+ instructions="你是一个中文助手",
92
+ model="ollama/qwen3.5:cloud",
93
+ tools=[my_tool],
94
+ skills=[my_skill],
95
+ )
96
+ ```
97
+
98
+ ---
99
+
100
+ ### BaseAgent
101
+
102
+ 所有 Agent 的基类。开发者通常不直接使用,除非要实现自定义 Agent 类型。
103
+
104
+ | 参数 | 类型 | 说明 |
105
+ |------|------|------|
106
+ | `name` | `str` | 名称 |
107
+ | `description` | `str` | 描述 |
108
+ | `sub_agents` | `list[BaseAgent]` | 子 Agent(自动建立父子关系) |
109
+ | `before_agent_callback` | `Callable \| None` | 运行前回调 |
110
+ | `after_agent_callback` | `Callable \| None` | 运行后回调 |
111
+
112
+ **子类化**:
113
+
114
+ ```python
115
+ class MyAgent(BaseAgent):
116
+ async def _run_impl(self, ctx):
117
+ yield Event(agent=self.name, type="final_output", data="自定义输出")
118
+ ```
119
+
120
+ ---
121
+
122
+ ### 编排 Agent
123
+
124
+ **SequentialAgent** — 按顺序执行子 Agent
125
+
126
+ ```python
127
+ from agentkit import SequentialAgent
128
+ pipeline = SequentialAgent(name="pipeline", sub_agents=[agent_a, agent_b, agent_c])
129
+ ```
130
+
131
+ **ParallelAgent** — 并行执行子 Agent(分支隔离)
132
+
133
+ ```python
134
+ from agentkit import ParallelAgent
135
+ parallel = ParallelAgent(name="parallel", sub_agents=[agent_a, agent_b])
136
+ ```
137
+
138
+ **LoopAgent** — 循环执行,直到 escalate 或达到上限
139
+
140
+ ```python
141
+ from agentkit import LoopAgent
142
+ loop = LoopAgent(name="loop", max_iterations=5, sub_agents=[coder, reviewer])
143
+ ```
144
+
145
+ | 参数 | 类型 | 说明 |
146
+ |------|------|------|
147
+ | `max_iterations` | `int` | LoopAgent 专属:最大循环次数,默认 10 |
148
+
149
+ ---
150
+
151
+ ## Runner 类
152
+
153
+ ### Runner
154
+
155
+ Agent 运行引擎。全部为类方法,无需实例化。
156
+
157
+ ```python
158
+ from agentkit import Runner
159
+ ```
160
+
161
+ **方法**:
162
+
163
+ | 方法 | 签名 | 说明 |
164
+ |------|------|------|
165
+ | `run` | `async (agent, *, input, context=None, user_id=None, max_turns=10) -> RunResult` | 异步运行 |
166
+ | `run_sync` | `(agent, **kwargs) -> RunResult` | 同步运行(内部调用 asyncio.run) |
167
+ | `run_streamed` | `async (agent, *, input, **kwargs) -> AsyncGenerator[Event]` | 流式运行,实时产出 Event |
168
+
169
+ **参数**:
170
+
171
+ | 参数 | 类型 | 默认值 | 说明 |
172
+ |------|------|--------|------|
173
+ | `agent` | `Agent` | **必填** | 要运行的 Agent |
174
+ | `input` | `str` | **必填** | 用户输入 |
175
+ | `context` | `Any` | `None` | 共享上下文(传给 RunContext.shared_context) |
176
+ | `user_id` | `str \| None` | `None` | 用户 ID(用于记忆隔离) |
177
+ | `max_turns` | `int` | `10` | 最大轮次(每个 turn 包含一次 LLM 调用) |
178
+
179
+ ---
180
+
181
+ ### RunResult
182
+
183
+ ```python
184
+ from agentkit import RunResult
185
+ ```
186
+
187
+ | 属性 | 类型 | 说明 |
188
+ |------|------|------|
189
+ | `final_output` | `Any` | 最终输出内容 |
190
+ | `events` | `list[Event]` | 运行过程中的所有事件 |
191
+ | `last_agent` | `str \| None` | 最后执行的 Agent 名称 |
192
+ | `error` | `str \| None` | 错误信息 |
193
+ | `success` | `bool` | 是否成功(`error is None and final_output is not None`) |
194
+
195
+ ---
196
+
197
+ ### Event
198
+
199
+ ```python
200
+ from agentkit import Event
201
+ ```
202
+
203
+ | 属性 | 类型 | 说明 |
204
+ |------|------|------|
205
+ | `agent` | `str` | 产生事件的 Agent 名称 |
206
+ | `type` | `str` | 事件类型 |
207
+ | `data` | `Any` | 事件数据 |
208
+ | `timestamp` | `float` | 时间戳 |
209
+
210
+ **事件类型**:
211
+
212
+ | type | 含义 |
213
+ |------|------|
214
+ | `llm_response` | LLM 返回了响应 |
215
+ | `tool_result` | 工具执行完成,data 包含 `{tool, result}` |
216
+ | `final_output` | 最终输出 |
217
+ | `handoff` | Agent 交接,data 包含 `{target}` |
218
+ | `escalate` | 上报/退出信号(LoopAgent 用) |
219
+ | `error` | 错误 |
220
+ | `callback` | 回调产生的事件 |
221
+ | `permission_denied` | 权限拒绝 |
222
+
223
+ ---
224
+
225
+ ## Tool 类
226
+
227
+ ### function_tool 装饰器
228
+
229
+ ```python
230
+ from agentkit import function_tool
231
+ ```
232
+
233
+ 将 Python 函数自动转换为 LLM 可调用的工具。
234
+
235
+ **用法 1:无参数**
236
+
237
+ ```python
238
+ @function_tool
239
+ def my_tool(query: str, top_k: int = 5) -> str:
240
+ """工具描述(自动从 docstring 提取)"""
241
+ return "result"
242
+ ```
243
+
244
+ **用法 2:带参数**
245
+
246
+ ```python
247
+ @function_tool(name="custom_name", description="自定义描述", needs_approval=True, timeout=30)
248
+ async def my_tool(target: str) -> str:
249
+ ...
250
+ ```
251
+
252
+ **装饰器参数**:
253
+
254
+ | 参数 | 类型 | 默认值 | 说明 |
255
+ |------|------|--------|------|
256
+ | `name` | `str \| None` | `None` | 自定义名称(默认取函数名) |
257
+ | `description` | `str \| None` | `None` | 自定义描述(默认取 docstring 首行) |
258
+ | `needs_approval` | `bool` | `False` | 是否需要人工审批 |
259
+ | `timeout` | `float \| None` | `None` | 超时秒数 |
260
+
261
+ **自动推断规则**:
262
+ - 工具名称 ← 函数名
263
+ - 工具描述 ← docstring 首行
264
+ - 参数 JSON Schema ← 函数签名的类型注解
265
+ - 如果第一个参数命名为 `ctx`/`context`/`tool_context`,自动识别为上下文参数
266
+
267
+ ---
268
+
269
+ ### FunctionTool
270
+
271
+ 底层工具类,通常通过 `@function_tool` 创建,也可手动构造。
272
+
273
+ ```python
274
+ from agentkit import FunctionTool
275
+
276
+ tool = FunctionTool(
277
+ name="my_tool",
278
+ description="工具描述",
279
+ handler=my_function,
280
+ json_schema={"type": "object", "properties": {...}},
281
+ )
282
+ ```
283
+
284
+ **类方法**:
285
+
286
+ | 方法 | 说明 |
287
+ |------|------|
288
+ | `FunctionTool.from_function(func)` | 从普通 Python 函数自动创建 |
289
+
290
+ ---
291
+
292
+ ### BaseTool / BaseToolset
293
+
294
+ **BaseTool**:工具抽象基类,自定义工具时继承。
295
+
296
+ ```python
297
+ class MyTool(BaseTool):
298
+ async def execute(self, ctx, arguments: dict) -> Any:
299
+ ...
300
+ def to_tool_definition(self) -> ToolDefinition:
301
+ ...
302
+ ```
303
+
304
+ **BaseToolset**:工具集基类,可动态展开为多个工具。
305
+
306
+ ```python
307
+ class MyToolset(BaseToolset):
308
+ async def get_tools(self, ctx) -> list[BaseTool]:
309
+ ...
310
+ ```
311
+
312
+ ---
313
+
314
+ ## Skill 类
315
+
316
+ ### Skill
317
+
318
+ ```python
319
+ from agentkit import Skill, SkillFrontmatter, SkillResources
320
+ ```
321
+
322
+ | 属性 | 类型 | 说明 |
323
+ |------|------|------|
324
+ | `frontmatter` | `SkillFrontmatter` | L1:元数据(name + description) |
325
+ | `instructions` | `str` | L2:SKILL.md 正文指令 |
326
+ | `resources` | `SkillResources` | L3:附加资源 |
327
+
328
+ **便捷属性**:`skill.name`、`skill.description`、`skill.additional_tools`、`skill.llm_config`
329
+
330
+ ---
331
+
332
+ ### SkillFrontmatter
333
+
334
+ | 字段 | 类型 | 说明 |
335
+ |------|------|------|
336
+ | `name` | `str` | kebab-case 标识符(≤64 字符) |
337
+ | `description` | `str` | 描述(≤1024 字符) |
338
+ | `license` | `str \| None` | 许可证 |
339
+ | `metadata` | `dict` | 扩展元数据,可含 `additional_tools` 和 `llm_config` |
340
+
341
+ ---
342
+
343
+ ### load_skill_from_dir
344
+
345
+ ```python
346
+ from agentkit import load_skill_from_dir
347
+
348
+ skill = load_skill_from_dir("./skills/my-skill")
349
+ ```
350
+
351
+ 从目录加载 Skill。目录必须包含 `SKILL.md`,且目录名必须与 Skill 的 `name` 字段一致。
352
+
353
+ ---
354
+
355
+ ### SkillRegistry
356
+
357
+ ```python
358
+ from agentkit import SkillRegistry
359
+
360
+ registry = SkillRegistry()
361
+ registry.add_search_path("./skills")
362
+ skills = registry.discover() # 自动发现目录下所有 Skill
363
+ skill = registry.get("my-skill") # 按名称获取
364
+ ```
365
+
366
+ ---
367
+
368
+ ## LLM 类
369
+
370
+ ### LLMRegistry
371
+
372
+ 模型注册中心,根据模型标识前缀自动选择适配器。
373
+
374
+ ```python
375
+ from agentkit import LLMRegistry
376
+ ```
377
+
378
+ | 方法 | 说明 |
379
+ |------|------|
380
+ | `LLMRegistry.set_default(model, **kwargs)` | 设置全局默认模型 |
381
+ | `LLMRegistry.create(model_or_config)` | 创建 LLM 实例(自动路由) |
382
+ | `LLMRegistry.create_default()` | 创建默认模型实例 |
383
+ | `LLMRegistry.register(prefix, adapter_class)` | 注册自定义适配器 |
384
+
385
+ **前缀路由表**:
386
+
387
+ | 前缀 | 适配器 | 需要的包 |
388
+ |------|--------|---------|
389
+ | `gpt-`、`o1`、`o3`、`o4` | OpenAIAdapter | `openai` |
390
+ | `claude-` | AnthropicAdapter | `anthropic` |
391
+ | `gemini-` | GoogleAdapter | `google-genai` |
392
+ | `ollama/` | OllamaAdapter | `aiohttp` |
393
+ | `deepseek/`、`qwen/`、`zhipu/`、`moonshot/`、`baichuan/`、`azure/` | OpenAICompatibleAdapter | `openai` |
394
+
395
+ ---
396
+
397
+ ### LLMConfig
398
+
399
+ LLM 调用的精细配置。
400
+
401
+ ```python
402
+ from agentkit import LLMConfig
403
+
404
+ config = LLMConfig(
405
+ model="gpt-4o",
406
+ temperature=0.3,
407
+ max_tokens=4096,
408
+ fallback_models=["gpt-4o-mini"],
409
+ )
410
+ ```
411
+
412
+ | 字段 | 类型 | 默认值 | 说明 |
413
+ |------|------|--------|------|
414
+ | `model` | `str` | **必填** | 模型标识 |
415
+ | `api_key` | `str \| None` | `None` | API Key(None 则从环境变量读取) |
416
+ | `api_base` | `str \| None` | `None` | 自定义 API 端点 |
417
+ | `temperature` | `float` | `0.7` | 温度 |
418
+ | `max_tokens` | `int \| None` | `None` | 最大 token 数 |
419
+ | `top_p` | `float` | `1.0` | top_p |
420
+ | `timeout` | `int` | `60` | 超时秒数 |
421
+ | `max_retries` | `int` | `3` | 最大重试次数 |
422
+ | `retry_delay` | `float` | `1.0` | 重试间隔(秒) |
423
+ | `fallback_models` | `list[str]` | `[]` | 降级模型列表 |
424
+ | `extra_params` | `dict` | `{}` | 厂商特有参数(见下方说明) |
425
+
426
+ **`extra_params` 常用参数**:
427
+
428
+ | 参数 | 适用适配器 | 默认值 | 说明 |
429
+ |------|-----------|--------|------|
430
+ | `think` | OllamaAdapter | `True` | 是否启用 thinking(深度思考)模式。默认开启。设为 `False` 可关闭深度推理,纯对话场景可能加速 2-3 倍,但工具调用场景下 cloud 模型关闭后可能反而更慢,请按实际测试结果选择 |
431
+
432
+ ```python
433
+ from agentkit.llm.registry import LLMRegistry
434
+
435
+ # 默认开启 thinking
436
+ llm = LLMRegistry.create("ollama/qwen3.5:cloud")
437
+
438
+ # 需要关闭时(纯对话场景可能更快)
439
+ llm = LLMRegistry.create("ollama/qwen3.5:cloud")
440
+ llm.config.extra_params["think"] = False
441
+ ```
442
+
443
+ ---
444
+
445
+ ### BaseLLM
446
+
447
+ LLM 抽象接口,自定义适配器时继承。
448
+
449
+ ```python
450
+ class MyLLM(BaseLLM):
451
+ async def generate(self, messages, *, tools=None, tool_choice=None, output_schema=None):
452
+ ...
453
+ async def generate_stream(self, messages, *, tools=None, tool_choice=None):
454
+ ...
455
+ ```
456
+
457
+ ---
458
+
459
+ ### 消息与响应类型
460
+
461
+ **Message** — 统一消息格式
462
+
463
+ ```python
464
+ from agentkit import Message
465
+
466
+ Message.system("你是助手")
467
+ Message.user("你好")
468
+ Message.assistant("你好!")
469
+ Message.tool(tool_call_id="call_123", content="工具结果")
470
+ ```
471
+
472
+ **ToolCall** — LLM 返回的工具调用
473
+
474
+ ```python
475
+ from agentkit import ToolCall
476
+ tc = ToolCall(id="call_123", name="get_weather", arguments={"city": "北京"})
477
+ ```
478
+
479
+ **LLMResponse** — 统一响应
480
+
481
+ | 属性 | 类型 | 说明 |
482
+ |------|------|------|
483
+ | `content` | `str \| None` | 文本回复 |
484
+ | `tool_calls` | `list[ToolCall]` | 工具调用请求 |
485
+ | `finish_reason` | `FinishReason` | `STOP` / `TOOL_CALLS` / `LENGTH` / `ERROR` |
486
+ | `usage` | `Usage` | token 用量 |
487
+ | `has_tool_calls` | `bool` | 是否有工具调用 |
488
+ | `has_content` | `bool` | 是否有文本回复 |
489
+
490
+ ---
491
+
492
+ ## 安全类
493
+
494
+ ### Guardrail
495
+
496
+ ```python
497
+ from agentkit import input_guardrail, output_guardrail, GuardrailResult
498
+
499
+ @input_guardrail
500
+ async def my_check(ctx):
501
+ if "危险" in ctx.input:
502
+ return GuardrailResult(triggered=True, reason="包含危险内容")
503
+ return GuardrailResult(triggered=False)
504
+
505
+ @output_guardrail
506
+ async def my_output_check(ctx, output):
507
+ return GuardrailResult(triggered=False)
508
+ ```
509
+
510
+ **GuardrailResult**:
511
+
512
+ | 字段 | 类型 | 说明 |
513
+ |------|------|------|
514
+ | `triggered` | `bool` | 是否触发熔断 |
515
+ | `reason` | `str \| None` | 触发原因 |
516
+ | `info` | `dict` | 附加信息 |
517
+
518
+ ---
519
+
520
+ ### PermissionPolicy
521
+
522
+ ```python
523
+ from agentkit import PermissionPolicy
524
+
525
+ policy = PermissionPolicy(
526
+ mode="ask", # "allow_all" / "deny_all" / "ask"
527
+ allowed_tools={"read_file"}, # 白名单
528
+ custom_check=my_check_fn, # 自定义检查函数
529
+ )
530
+ ```
531
+
532
+ | 参数 | 说明 |
533
+ |------|------|
534
+ | `mode="allow_all"` | 允许所有工具 |
535
+ | `mode="deny_all"` | 拒绝所有工具 |
536
+ | `mode="ask"` | 先查白名单,再调自定义检查 |
537
+
538
+ ---
539
+
540
+ ## 记忆类
541
+
542
+ ### BaseMemoryProvider
543
+
544
+ 记忆系统抽象接口。
545
+
546
+ ```python
547
+ from agentkit import BaseMemoryProvider, Memory
548
+
549
+ class MyMemory(BaseMemoryProvider):
550
+ async def add(self, content, *, user_id=None, agent_id=None, metadata=None) -> list[Memory]: ...
551
+ async def search(self, query, *, user_id=None, agent_id=None, limit=10) -> list[Memory]: ...
552
+ async def get_all(self, *, user_id=None, agent_id=None) -> list[Memory]: ...
553
+ async def delete(self, memory_id) -> bool: ...
554
+ ```
555
+
556
+ **内置实现**:`Mem0Provider`(需安装 `mem0ai`)
557
+
558
+ ```python
559
+ from agentkit.memory.mem0_provider import Mem0Provider
560
+
561
+ memory = Mem0Provider({"vector_store": {"provider": "qdrant", ...}})
562
+ agent = Agent(memory=memory, ...)
563
+ ```
564
+
565
+ Agent 会自动:
566
+ 1. 对话前——检索相关记忆注入上下文
567
+ 2. 对话后——提取新记忆存储
568
+
569
+ **Memory** 数据类:
570
+
571
+ | 字段 | 类型 | 说明 |
572
+ |------|------|------|
573
+ | `id` | `str` | 记忆 ID |
574
+ | `content` | `str` | 记忆内容 |
575
+ | `metadata` | `dict` | 元数据 |
576
+ | `score` | `float` | 相关性分数(检索时) |