背景
这个架构的目标是构建一个 完整 AI Operating System(AI OS),能够:
- 接入 LLM
- 管理 Tools
- 管理 Skills
- 管理 Memory
- 支持 Agent
- 支持 Workflow
- 支持 MCP
- 支持多数据源
很多 AI 产品底层结构都类似(例如 OpenAI、Anthropic、Google 的 Agent 系统)。
企业级 AI OS 系统
┌──────────────────────────────┐
│ 1 UI Layer │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 2 API Gateway │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 3 Agent Controller │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 4 Planning Layer │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 5 Context Engine │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 6 Tool / Skill Layer │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 7 Workflow Engine │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 8 Memory Layer │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 9 Knowledge / RAG │
└──────────────────────────────┘
│
┌──────────────────────────────┐
│ 10 Infrastructure Layer │
└──────────────────────────────┘
二、第1层:UI Layer
用户入口。
可能包括:
Web Chat
Mobile App
Slack Bot
API
Voice
例如:
Chat 界面
上传文件
语音交互
UI 不需要复杂逻辑。
主要作用:
- 用户输入
- 显示结果
三、第2层:API Gateway
所有请求先进入 API 层。
职责:
- 认证
- 日志
- 限流
- 路由
技术:
FastAPI
Flask
Gateway
例如:
POST /chat
POST /tool
POST /workflow
四、第3层:Agent Controller
这是 AI系统核心调度器。
职责:
- 接收任务
- 维护 Agent 状态
- 调用 Planner
- 执行工具
- 循环执行
典型结构:
AgentState
{
goal
plan
current_step
memory
}
执行循环:
while not finished:
plan
act
observe
这是经典 Agent Loop。
五、第4层:Planning Layer
Planner 的作用:
把复杂任务拆分成步骤
例如用户:
写一份AI行业报告
Planner生成:
1 搜索资料
2 提取公司
3 分析趋势
4 生成报告
Planner可以:
LLM Planner
Rule Planner
Hybrid Planner
六、第5层:Context Engine(非常关键)
很多系统忽略这一层。
Context Engine 负责:
构建 Prompt Context
来源包括:
- 用户输入
- 历史对话
- memory
- RAG文档
- tool结果
- system prompt
最终构造:
LLM Prompt
例如:
System Prompt
User Query
Memory
Docs
Tools
Context Engine 是 AI系统最关键组件之一。
七、第6层:Tool / Skill Layer
AI的能力来自工具。
工具类型:
1 外部 API
例如:
天气 股票 搜索
2 系统工具
例如:
发送邮件 文件读写 数据库查询
3 Skill(技能)
Skill = 多个工具组合。
例如:
generate_report
内部:
search
analyze
summarize
Skill 本质是:
workflow
八、第7层:Workflow Engine
复杂任务需要 workflow。
支持:
- step
- retry
- parallel
- timeout
- branch
例如:
step1 搜索
step2 抽取
step3 分析
step4 生成
开源工具:
Temporal
Apache Airflow
Prefect
九、第8层:Memory Layer
AI系统必须有 memory。
分三类。
- Conversation Memory. 聊天历史
- User Memory. 用户信息,用户偏好
- Semantic Memory. embedding, vector db
十、第9层:Knowledge / RAG
RAG:
Retrieval Augmented Generation
流程:
User query
↓
embedding
↓
vector search
↓
top k docs
↓
LLM
优点:
减少 hallucination, 使用私有知识
十一、第10层:Infrastructure Layer
最底层。
包括:
- LLM
- DB
- Cache
- Queue
典型组件:
LLM:
- OpenAI
- Anthropic
- 本地模型
数据库:
- PostgreSQL
- MongoDB
缓存:
- Redis
消息队列:
- Kafka
- RabbitMQ
11 Guardrail Layer
安全控制。
例如:
- 内容过滤
- PII保护
- Prompt injection防御
12 Tool Registry
工具注册中心。
记录:
- tool name
- description
- schema
- endpoint
Agent 通过 registry 发现工具。
最终完整架构(推荐)
User
│
▼
UI
│
▼
API Gateway
│
▼
Agent Controller
│
▼
Planner
│
▼
Context Engine
│
▼
Tool Router
│
▼
Tools / Skills
│
▼
Workflow Engine
│
▼
Memory
│
▼
RAG
│
▼
Infrastructure
一个真实执行例子
用户:帮我分析最近AI融资情况
系统执行:
User Query
↓
Agent Controller
↓
Planner
↓
Plan Steps
↓
Tool Router
↓
search_news tool
↓
extract_companies
↓
finance API
↓
LLM分析
↓
生成报告
一个非常重要的设计原则
企业级 AI 系统不是:
LLM + Prompt
而是:
LLM + Data + Tools + Memory + Workflow + Control
本质是:
AI Operating System