docs: v2.0 全面修订 — 开源优先 + 扁平化用户模型
核心变更: - 产品定位: '像用水电一样使用AI能力,注册即用,按量付费' - 用户模型: 去掉租户概念,扁平化为 订阅用户/非订阅用户 - 认证: Casdoor SSO + 阿里云实名认证(替代 Keycloak) - LLM 网关: NEW API 开源(替代自研 Go 网关) - AI 服务: FastAPI(替代 Go 自研,AI 生态更兼容) - 新增: 欠费不影响已发布能力(核心商业规则) - 新增: 开发者提现模块 - 开发周期: 20周 → 15周(开源组件复用) 文档列表: - 01-PRD-产品需求文档.md (v2.0) - 02-系统架构设计.md (v2.0) - 03-数据库设计.md (v2.0) - 04-API接口规范.md (v2.0) - 05-AGENTS-MiMoCode配置.md (v2.0)
This commit is contained in:
1 parent
d9a8a0e93c
commit
8209f15d0a
5 files changed
+1031
-1433
No files matched your search
+413
-391
@@ -1,452 +1,474 @@
|
||||
# AI PaaS 商业平台 — 系统架构设计
|
||||
|
||||
> 版本: 1.0 | 日期: 2026-06-27
|
||||
> 基于: 5 平台交叉验证共识 + 方案二(稳扎稳打)
|
||||
> 版本: 2.0 | 日期: 2026-06-27
|
||||
> 状态: 已修订(基于用户反馈:开源优先 + AI 生态兼容)
|
||||
|
||||
---
|
||||
|
||||
## 1. 架构总览
|
||||
## 1. 设计原则
|
||||
|
||||
### 1.1 分层架构图
|
||||
### 1.1 核心原则(v2.0 强化)
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ 用户入口层 (Access) │
|
||||
│ │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐│
|
||||
│ │ Web UI │ │ REST API │ │ WebSocket│ │ CLI │ │ IM Bot ││
|
||||
│ │ (React) │ │ │ │ (SSE) │ │ (Phase5) │ │ (Phase4) ││
|
||||
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘│
|
||||
└───────┼──────────────┼──────────────┼──────────────┼──────────────┼──────┘
|
||||
│ │ │ │ │
|
||||
▼ ▼ ▼ ▼ ▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ API Gateway (Apache APISIX 3.15+) │
|
||||
│ │
|
||||
│ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────┐ ┌─────────────────┐ │
|
||||
│ │jwt-auth │ │key-auth │ │limit- │ │api- │ │ proxy-rewrite │ │
|
||||
│ │JWT验证 │ │Key验证 │ │count │ │breaker │ │ + CORS │ │
|
||||
│ │ │ │ │ │限流 │ │熔断 │ │ │ │
|
||||
│ └─────────┘ └─────────┘ └─────────┘ └─────────┘ └─────────────────┘ │
|
||||
│ │
|
||||
│ 路由表: │
|
||||
│ /api/v1/auth/* → auth-service:8081 │
|
||||
│ /api/v1/capabilities/* → registry-service:8082 │
|
||||
│ /v1/chat/completions → openapi-gateway:8083 │
|
||||
│ /api/v1/billing/* → billing-service:8084 │
|
||||
│ /api/v1/skills/* → skills-service:8085 │
|
||||
│ /api/v1/admin/* → admin-service:8086 │
|
||||
│ /api/v1/im/* → im-gateway:8087 │
|
||||
│ /api/v1/mcp/* → mcp-proxy:8088 │
|
||||
│ /api/v1/agent/* → agent-proxy:8089 │
|
||||
└─────────────────────────────┬───────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ 核心业务层 (Core Services) │
|
||||
│ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────────────────────┐ │
|
||||
│ │ auth-service │ │ billing-svc │ │ registry-service │ │
|
||||
│ │ Go :8081 │ │ Go :8084 │ │ Go :8082 │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │ • JWT 验证 │ │ • Credit 扣费│ │ • 能力注册/注销 │ │
|
||||
│ │ • API Key │ │ • 余额查询 │ │ • 版本管理 │ │
|
||||
│ │ • RBAC 角色 │ │ • 流水记录 │ │ • 服务发现 │ │
|
||||
│ │ • 租户上下文 │ │ • 订阅管理 │ │ • 状态管理 │ │
|
||||
│ │ • 注册/登录 │ │ • 充值 │ │ • gRPC + REST │ │
|
||||
│ └──────┬───────┘ └──────┬───────┘ └──────────────┬───────────────┘ │
|
||||
│ │ │ │ │
|
||||
│ ┌──────┴───────┐ ┌──────┴───────┐ ┌─────────────┴───────────────┐ │
|
||||
│ │openapi-gw │ │ metering-svc │ │ skills-service │ │
|
||||
│ │Go :8083 │ │ Go :8090 │ │ Go :8085 │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │• OpenAI API │ │• Kafka消费 │ │• 技能发布/审核 │ │
|
||||
│ │• 请求拦截链 │ │• PG持久化 │ │• 市场浏览/搜索 │ │
|
||||
│ │• 流式 SSE │ │• 预聚合 │ │• 购买/分成 │ │
|
||||
│ │• 用量上报 │ │• 用量查询 │ │• 全文搜索 │ │
|
||||
│ └──────┬───────┘ └──────────────┘ └─────────────────────────────┘ │
|
||||
│ │ │
|
||||
│ ┌──────┴───────┐ ┌──────────────┐ ┌─────────────────────────────┐ │
|
||||
│ │llm-router │ │im-gateway │ │ admin-service │ │
|
||||
│ │Go :8091 │ │ Go :8087 │ │ Go :8086 │ │
|
||||
│ │ │ │ │ │ │ │
|
||||
│ │• 多平台适配 │ │• 微信/QQ/钉钉│ │• 用户管理 │ │
|
||||
│ │• 智能路由 │ │• 会话管理 │ │• 租户管理 │ │
|
||||
│ │• 故障转移 │ │• 消息收发 │ │• 能力审核 │ │
|
||||
│ │• 连接池 │ │• 上下文注入 │ │• 系统配置 │ │
|
||||
│ └──────────────┘ └──────────────┘ └─────────────────────────────┘ │
|
||||
└─────────────────────────────┬───────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ AI 能力层 (AI Capabilities) │
|
||||
│ │
|
||||
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ ┌────────────┐│
|
||||
│ │ LangFlow │ │ mcp-hub │ │ Milvus │ │ vLLM ││
|
||||
│ │ [开源] │ │ [开源] │ │ [开源] │ │ [开源] ││
|
||||
│ │ Agent 编排 │ │ MCP 服务管理 │ │ 向量数据库 │ │ 模型推理 ││
|
||||
│ │ :7860 │ │ :3001 │ │ :19530 │ │ :8000 ││
|
||||
│ └──────────────┘ └──────────────┘ └──────────────┘ └────────────┘│
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ 外部 LLM 平台 (External LLM) │
|
||||
│ │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐│
|
||||
│ │ DeepSeek │ │ 千问 │ │ 豆包 │ │ 智谱 │ │ 百度/元宝 ││
|
||||
│ │ API │ │ API │ │ API │ │ API │ │ API ││
|
||||
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────────┘│
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
│
|
||||
▼
|
||||
┌─────────────────────────────────────────────────────────────────────────┐
|
||||
│ 基础设施层 (Infrastructure) │
|
||||
│ │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────────┐│
|
||||
│ │PostgreSQL│ │ Redis 7 │ │ Kafka │ │ Keycloak │ │ Prometheus ││
|
||||
│ │ 16 │ │ │ │ 3.x │ │ 25+ │ │ + Grafana ││
|
||||
│ │ 主数据库 │ │ 缓存+Lua │ │ 事件总线 │ │ 认证 │ │ 监控 ││
|
||||
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────────┘│
|
||||
└─────────────────────────────────────────────────────────────────────────┘
|
||||
1. 开源优先: 能用开源就不自研,社区活跃 > 自己造轮子
|
||||
2. AI 生态兼容: 所有选型必须兼容当前 AI 生态标准(OpenAI 格式、MCP 协议等)
|
||||
3. 标准化: 遵循行业标准协议,确保未来可扩展
|
||||
4. 扁平化: 不搞多租户复杂度,一个用户体系走天下
|
||||
5. 渐进式: 先跑通核心链路,再逐步丰富功能
|
||||
```
|
||||
|
||||
### 1.2 技术选型决策矩阵
|
||||
|
||||
| 组件 | 选项 A | 选项 B | 决策 | 理由 |
|
||||
|------|--------|--------|------|------|
|
||||
| **LLM 网关** | 自研 Go | NEW API 开源 | **NEW API** | 14k Stars,开箱即用,省 6 周开发 |
|
||||
| **SSO 认证** | Keycloak | Casdoor | **Casdoor** | 国内生态更好,微信/钉钉对接完善 |
|
||||
| **实名认证** | 自建 OCR | 阿里云 API | **阿里云** | 国内最权威,接口稳定 |
|
||||
| **AI 服务框架** | Go 自研 | FastAPI | **FastAPI** | AI 生态标准,79k Stars,异步高性能 |
|
||||
| **本地 LLM** | 自建 | Ollama | **Ollama** | 130k Stars,本地部署事实标准 |
|
||||
| **API 网关** | 自研 | APISIX | **APISIX** | 高性能,插件丰富,国内团队 |
|
||||
| **数据库** | MySQL | PostgreSQL | **PostgreSQL** | JSONB + RLS,功能更强 |
|
||||
| **缓存** | Memcached | Redis | **Redis** | Lua 脚本支持,原子计费必备 |
|
||||
| **消息队列** | RabbitMQ | Kafka | **Kafka** | 用量数据天然适合日志型存储 |
|
||||
|
||||
---
|
||||
|
||||
## 2. 整体架构
|
||||
|
||||
```
|
||||
┌──────────────────────────────────────────────────────────────────────┐
|
||||
│ 用户入口层 │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
||||
│ │ Web App │ │ CLI Tool │ │ REST API │ │ IM Bot │ │ MCP Hub │ │
|
||||
│ │ (React) │ │ │ │ │ │(微信/QQ) │ │ │ │
|
||||
│ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ └────┬─────┘ │
|
||||
│ └────────────┴────────────┴────────────┴────────────┘ │
|
||||
└────────────────────────────────┬─────────────────────────────────────┘
|
||||
│
|
||||
┌────────────────────────────────▼─────────────────────────────────────┐
|
||||
│ API 网关层 (APISIX) │
|
||||
│ 认证鉴权 → 限流 → 路由 → 日志 → 监控 │
|
||||
└────────────────────────────────┬─────────────────────────────────────┘
|
||||
│
|
||||
┌────────────────────────────────▼─────────────────────────────────────┐
|
||||
│ 核心服务层 │
|
||||
│ │
|
||||
│ ┌─────────────────────────────────────────────────────────────────┐ │
|
||||
│ │ Casdoor (SSO 认证中心) │ │
|
||||
│ │ 注册/登录 → JWT 签发 → 用户管理 → 第三方登录(微信/钉钉) │ │
|
||||
│ └─────────────────────────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌─────────────────────────────────────────────────────────────────┐ │
|
||||
│ │ FastAPI AI 中间件层 │ │
|
||||
│ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │
|
||||
│ │ │ 计费引擎 │ │ 能力注册 │ │ Skills │ │ 用量追踪 │ │ │
|
||||
│ │ │ (Redis │ │ 中心 │ │ 市场 │ │ (Kafka) │ │ │
|
||||
│ │ │ Lua) │ │ │ │ │ │ │ │ │
|
||||
│ │ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │ │
|
||||
│ └─────────────────────────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌─────────────────────────────────────────────────────────────────┐ │
|
||||
│ │ NEW API (LLM 网关) │ │
|
||||
│ │ 多模型管理 → 负载均衡 → 故障转移 → 流式透传 → 用量统计 │ │
|
||||
│ └─────────────────────────────────────────────────────────────────┘ │
|
||||
└────────────────────────────────┬─────────────────────────────────────┘
|
||||
│
|
||||
┌────────────────────────────────▼─────────────────────────────────────┐
|
||||
│ AI 能力层 │
|
||||
│ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ ┌──────────┐ │
|
||||
│ │ DeepSeek │ │ 千问 │ │ 豆包 │ │ 智谱 │ │ Ollama │ │
|
||||
│ │ (API) │ │ (API) │ │ (API) │ │ (API) │ │ (本地) │ │
|
||||
│ └──────────┘ └──────────┘ └──────────┘ └──────────┘ └──────────┘ │
|
||||
└──────────────────────────────────────────────────────────────────────┘
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. 核心数据流
|
||||
## 3. 核心服务拆分
|
||||
|
||||
### 2.1 LLM 调用 + 计费完整流程
|
||||
### 3.1 服务列表(v2.0 简化)
|
||||
|
||||
```
|
||||
用户代码 APISIX openapi-gw billing-svc
|
||||
│ │ │ │
|
||||
│ POST /v1/chat/completions │ │ │
|
||||
│ Authorization: Bearer xxx │ │ │
|
||||
├──────────────────────────→│ │ │
|
||||
│ │ jwt-auth 验证 │ │
|
||||
│ │ 提取 tenant_id │ │
|
||||
│ │ 注入 X-Tenant-Id │ │
|
||||
│ ├───────────────────→│ │
|
||||
│ │ │ │
|
||||
│ │ │ 1. API Key 验证 │
|
||||
│ │ │ (→ auth-svc) │
|
||||
│ │ │ │
|
||||
│ │ │ 2. 余额检查 │
|
||||
│ │ │ GET /balance │
|
||||
│ │ ├───────────────────→│
|
||||
│ │ │ balance: 12580 │
|
||||
│ │ │←───────────────────│
|
||||
│ │ │ │
|
||||
│ │ │ 3. 路由到 LLM │
|
||||
│ │ │ Router │
|
||||
│ │ │ │
|
||||
│ │ │ llm-router │
|
||||
│ │ │ ├─选择 DeepSeek │
|
||||
│ │ │ ├─调用 API │
|
||||
│ │ │ └─返回 + usage │
|
||||
│ │ │ │
|
||||
│ │ │ 4. 计费扣减 │
|
||||
│ │ │ POST /meter │
|
||||
│ │ │ {tokens: 1280} │
|
||||
│ │ ├───────────────────→│
|
||||
│ │ │ Redis Lua 扣费 │
|
||||
│ │ │ balance: 12580 │
|
||||
│ │ │ → 11300 │
|
||||
│ │ │←───────────────────│
|
||||
│ │ │ │
|
||||
│ │ │ 5. 异步上报用量 │
|
||||
│ │ │ → Kafka │
|
||||
│ │ │ │
|
||||
│ {choices: [...], │ │ │
|
||||
│ usage: { │ │ │
|
||||
│ prompt_tokens: 800, │ │ │
|
||||
│ completion_tokens: 480│ │ │
|
||||
│ }} │ │ │
|
||||
│←──────────────────────────│←───────────────────│ │
|
||||
│ │ │ │
|
||||
| 服务 | 技术 | 端口 | 职责 | 开源/自研 |
|
||||
|------|------|------|------|----------|
|
||||
| **Casdoor** | Go | 8000 | SSO 认证中心 | 开源 |
|
||||
| **NEW API** | Go | 3000 | LLM 网关 | 开源 |
|
||||
| **ai-paas-gateway** | FastAPI | 8080 | 核心业务网关 | **自研** |
|
||||
| **ai-paas-billing** | FastAPI | 8081 | 计费引擎 | **自研** |
|
||||
| **ai-paas-registry** | FastAPI | 8082 | 能力注册中心 | **自研** |
|
||||
| **ai-paas-skills** | FastAPI | 8083 | Skills 市场 | **自研** |
|
||||
| **ai-paas-metering** | FastAPI | 8084 | 用量追踪 | **自研** |
|
||||
| **ai-paas-admin** | FastAPI | 8085 | 管理后台 API | **自研** |
|
||||
| **APISIX** | Lua/C | 9080 | API 网关 | 开源 |
|
||||
| **Ollama** | Go | 11434 | 本地 LLM 推理 | 开源 |
|
||||
| **LangFlow** | Python | 7860 | Agent 编排 | 开源 |
|
||||
|
||||
> **v2.0 变化**: 原 9 个自研 Go 服务 → 简化为 6 个 FastAPI 服务 + 3 个开源组件。
|
||||
|
||||
---
|
||||
|
||||
## 4. 自研服务详细设计
|
||||
|
||||
### 4.1 ai-paas-gateway(核心业务网关)— FastAPI
|
||||
|
||||
这是整个平台的**核心调度中心**,所有业务请求经过这里。
|
||||
|
||||
```python
|
||||
# 技术选型: FastAPI (Python 3.11+)
|
||||
# 为什么 FastAPI:
|
||||
# 1. AI 生态标准框架,LangChain/LlamaIndex 原生支持
|
||||
# 2. 异步高性能,自带 OpenAPI 文档
|
||||
# 3. 类型安全 (Pydantic),开发效率高
|
||||
# 4. 社区极其活跃 (79k Stars)
|
||||
# 5. 部署简单 (uvicorn + Docker)
|
||||
|
||||
# 核心路由:
|
||||
POST /v1/chat/completions → 转发到 NEW API,前置计费/鉴权
|
||||
GET /v1/models → 转发到 NEW API
|
||||
GET /api/v1/user/profile → 用户信息
|
||||
POST /api/v1/auth/* → 转发到 Casdoor
|
||||
GET /api/v1/billing/* → 计费查询
|
||||
GET /api/v1/capabilities/* → 能力注册
|
||||
GET /api/v1/skills/* → Skills 市场
|
||||
```
|
||||
|
||||
### 2.2 流式 SSE 响应流程
|
||||
|
||||
**请求处理链路(LLM 调用):**
|
||||
```
|
||||
用户浏览器 APISIX openapi-gw llm-router
|
||||
│ │ │ │
|
||||
│ POST /v1/chat/completions │ │ │
|
||||
│ stream: true │ │ │
|
||||
├──────────────────────────→│───────────────────→│───────────────────→│
|
||||
│ │ │ │
|
||||
│ │ │ HTTP 200 │
|
||||
│ │ │ Content-Type: │
|
||||
│ │ │ text/event-stream │
|
||||
│←──────────────────────────│←───────────────────│←───────────────────│
|
||||
│ │ │ │
|
||||
│ data: {"choices":[{ │ │ │
|
||||
│ "delta":{"content":"你"}}│ │ │
|
||||
│←──────────────────────────│←───────────────────│←───────────────────│
|
||||
│ │ │ │
|
||||
│ data: {"choices":[{ │ │ │
|
||||
│ "delta":{"content":"好"}}│ │ │
|
||||
│←──────────────────────────│←───────────────────│←───────────────────│
|
||||
│ │ │ │
|
||||
│ data: [DONE] │ │ │
|
||||
│←──────────────────────────│←───────────────────│←───────────────────│
|
||||
│ │ │ │
|
||||
│ │ │ 4. 计费扣减 │
|
||||
│ │ │ 5. 上报用量 │
|
||||
用户请求 → APISIX → ai-paas-gateway
|
||||
1. JWT/API Key 验证 (调用 Casdoor)
|
||||
2. 用户状态检查 (订阅/欠费)
|
||||
3. Credit 余额检查 (Redis)
|
||||
4. 扣减 Credit (Redis Lua 原子操作)
|
||||
5. 转发到 NEW API
|
||||
6. 返回响应 (流式 SSE 透传)
|
||||
7. 异步上报用量 (Kafka)
|
||||
```
|
||||
|
||||
### 4.2 ai-paas-billing(计费引擎)— FastAPI
|
||||
|
||||
```python
|
||||
# Redis Lua 原子脚本 (先扣后服)
|
||||
"""
|
||||
-- deduct_credit.lua
|
||||
local balance = tonumber(redis.call('GET', KEYS[1]) or '0')
|
||||
local amount = tonumber(ARGV[1])
|
||||
local idempotency_key = ARGV[2]
|
||||
|
||||
-- 幂等检查
|
||||
if redis.call('EXISTS', 'billing:idempotency:' .. idempotency_key) == 1 then
|
||||
return redis.call('GET', 'billing:idempotency:' .. idempotency_key)
|
||||
end
|
||||
|
||||
if balance < amount then
|
||||
return 'INSUFFICIENT_BALANCE'
|
||||
end
|
||||
|
||||
local new_balance = balance - amount
|
||||
redis.call('SET', KEYS[1], new_balance)
|
||||
redis.call('SETEX', 'billing:idempotency:' .. idempotency_key, 86400, 'OK')
|
||||
return 'OK:' .. new_balance
|
||||
"""
|
||||
```
|
||||
|
||||
### 4.3 ai-paas-registry(能力注册中心)— FastAPI
|
||||
|
||||
```python
|
||||
# 能力 CRUD + 版本管理
|
||||
# 支持: agent/mcp/skill/llm/custom 五种类型
|
||||
# 状态机: draft → pending_review → active → deprecated
|
||||
|
||||
# 能力发现: 按类型/标签/评分/使用量查询
|
||||
# 能力验证: 注册时自动健康检查
|
||||
# 能力版本: SemVer 版本管理
|
||||
```
|
||||
|
||||
### 4.4 ai-paas-skills(Skills 市场)— FastAPI
|
||||
|
||||
```python
|
||||
# 全文搜索: PostgreSQL GIN 索引
|
||||
# 技能交易: 扣费 → 分成 → 授权
|
||||
# 评分系统: 1-5 星 + 文字评价
|
||||
# 开发者后台: 发布/管理/收益统计
|
||||
```
|
||||
|
||||
### 4.5 ai-paas-metering(用量追踪)— FastAPI
|
||||
|
||||
```python
|
||||
# Kafka Consumer: 消费 usage.events
|
||||
# PostgreSQL 持久化: token_usage 表 (按月分区)
|
||||
# 预聚合: 每小时刷新 usage_summaries
|
||||
# 用量查询: 按日/周/月/模型/能力统计
|
||||
# 账单生成: 月度账单自动计算
|
||||
```
|
||||
|
||||
### 4.6 ai-paas-admin(管理后台)— FastAPI
|
||||
|
||||
```python
|
||||
# 用户管理: 列表/详情/状态变更
|
||||
# 能力审核: 审批/拒绝
|
||||
# 财务管理: 流水/提现/对账
|
||||
# 系统监控: 健康检查/配置管理
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 3. 服务间通信
|
||||
|
||||
### 3.1 同步调用(HTTP/gRPC)
|
||||
|
||||
| 调用方 | 被调用方 | 协议 | 说明 |
|
||||
|--------|---------|------|------|
|
||||
| APISIX | 所有后端服务 | HTTP | 路由转发 |
|
||||
| openapi-gw | auth-service | HTTP | JWT 验证 |
|
||||
| openapi-gw | billing-svc | HTTP | 余额检查+扣费 |
|
||||
| openapi-gw | llm-router | HTTP | LLM 调用 |
|
||||
| openapi-gw | registry-service | gRPC | 能力查询(内部) |
|
||||
| skills-service | billing-svc | HTTP | 购买扣费 |
|
||||
| im-gateway | llm-router | HTTP | LLM 调用 |
|
||||
| registry-service | mcp-hub | HTTP | MCP 同步 |
|
||||
| registry-service | langflow | HTTP | Agent 同步 |
|
||||
|
||||
### 3.2 异步通信(Kafka)
|
||||
|
||||
| 生产者 | Topic | 消费者 | 说明 |
|
||||
|--------|-------|--------|------|
|
||||
| openapi-gw | usage.events | metering-svc | LLM 用量上报 |
|
||||
| billing-svc | billing.events | metering-svc | 计费事件 |
|
||||
| registry-svc | capability.events | mcp-hub, langflow | 能力变更通知 |
|
||||
|
||||
### 3.3 缓存策略(Redis)
|
||||
|
||||
| Key 模式 | 类型 | TTL | 说明 |
|
||||
|----------|------|-----|------|
|
||||
| auth:session:{token} | Hash | 24h | 用户会话 |
|
||||
| auth:apikey:{key_hash} | Hash | 1h | API Key 缓存 |
|
||||
| billing:balance:{tenant_id} | String | - | Credit 余额(毫 Credit) |
|
||||
| billing:idempotency:{tenant_id}:{key} | Hash | 24h | 幂等键 |
|
||||
| billing:daily:{tenant_id}:{YYYY-MM-DD} | Hash | 7d | 每日统计 |
|
||||
| billing:subscription:{tenant_id} | Hash | - | 订阅信息 |
|
||||
| ratelimit:{tenant_id}:{window} | String | 60s | 限流计数 |
|
||||
| im:session:{platform}:{user_id} | Hash | 24h | IM 会话上下文 |
|
||||
|
||||
---
|
||||
|
||||
## 4. 数据库架构
|
||||
|
||||
### 4.1 PostgreSQL Schema 概览
|
||||
## 5. 数据流向
|
||||
|
||||
### 5.1 LLM 调用主流程
|
||||
```
|
||||
┌─────────────────────────────────────────────────────────┐
|
||||
│ PostgreSQL 16 │
|
||||
│ │
|
||||
│ ┌─────────────┐ ┌─────────────┐ ┌────────────────┐ │
|
||||
│ │ tenants │ │ users │ │ capabilities │ │
|
||||
│ │ (租户) │←─│ (用户) │ │ (能力) │ │
|
||||
│ └──────┬──────┘ └─────────────┘ └───────┬────────┘ │
|
||||
│ │ │ │
|
||||
│ ┌──────┴──────┐ ┌─────────────┐ ┌───────┴────────┐ │
|
||||
│ │ subscriptions│ │ api_keys │ │ cap_versions │ │
|
||||
│ │ (订阅) │ │ (API密钥) │ │ (能力版本) │ │
|
||||
│ └─────────────┘ └─────────────┘ └────────────────┘ │
|
||||
│ │
|
||||
│ ┌─────────────┐ ┌─────────────┐ ┌────────────────┐ │
|
||||
│ │credit_trans │ │ token_usage │ │ skills │ │
|
||||
│ │(Credit流水) │ │ (Token用量) │ │ (技能) │ │
|
||||
│ │ 不可变 │ │ 按月分区 │ │ │ │
|
||||
│ └─────────────┘ └─────────────┘ └───────┬────────┘ │
|
||||
│ │ │
|
||||
│ ┌─────────────┐ ┌─────────────┐ ┌───────┴────────┐ │
|
||||
│ │skill_purchases│ │ im_sessions │ │ im_messages │ │
|
||||
│ │(技能购买) │ │ (IM会话) │ │ (IM消息) │ │
|
||||
│ └─────────────┘ └─────────────┘ └────────────────┘ │
|
||||
│ │
|
||||
│ ┌─────────────────────────────────────────────────┐ │
|
||||
│ │ All tables have RLS policies based on tenant_id │ │
|
||||
│ └─────────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────────────┘
|
||||
用户 → Web/CLI/API
|
||||
→ APISIX (限流/路由)
|
||||
→ ai-paas-gateway (鉴权/计费/转发)
|
||||
→ NEW API (模型路由)
|
||||
→ DeepSeek/千问/豆包... (实际推理)
|
||||
→ 响应流式返回用户
|
||||
→ 异步: 用量事件 → Kafka → ai-paas-metering → PostgreSQL
|
||||
```
|
||||
|
||||
### 4.2 RLS 策略
|
||||
### 5.2 能力发布流程
|
||||
```
|
||||
开发者 → Web Dashboard
|
||||
→ ai-paas-registry (创建能力)
|
||||
→ 管理员审核
|
||||
→ ai-paas-skills (上架市场)
|
||||
→ 用户浏览/购买
|
||||
→ ai-paas-billing (扣费/分成)
|
||||
```
|
||||
|
||||
```sql
|
||||
-- 所有表启用 RLS
|
||||
ALTER TABLE tenants ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE users ENABLE ROW LEVEL SECURITY;
|
||||
ALTER TABLE capabilities ENABLE ROW LEVEL SECURITY;
|
||||
-- ... 所有表
|
||||
|
||||
-- 通用策略: 基于 tenant_id 隔离
|
||||
CREATE POLICY tenant_isolation ON capabilities
|
||||
USING (tenant_id = current_setting('app.tenant_id')::uuid);
|
||||
|
||||
-- 管理员可以看所有数据
|
||||
CREATE POLICY admin_full_access ON capabilities
|
||||
USING (current_setting('app.user_role') = 'admin');
|
||||
### 5.3 认证流程
|
||||
```
|
||||
用户 → Web App
|
||||
→ Casdoor (注册/登录)
|
||||
→ 阿里云实名认证 API (身份验证)
|
||||
→ ai-paas-gateway (更新用户状态)
|
||||
→ JWT Token 签发
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 5. 安全架构
|
||||
## 6. 开源组件集成方案
|
||||
|
||||
### 5.1 认证流程
|
||||
### 6.1 Casdoor (SSO 认证中心)
|
||||
|
||||
```
|
||||
┌──────────────┐
|
||||
│ Keycloak │
|
||||
│ (认证中心) │
|
||||
└──────┬───────┘
|
||||
│
|
||||
┌────────────────┼────────────────┐
|
||||
▼ ▼ ▼
|
||||
┌──────────┐ ┌──────────┐ ┌──────────┐
|
||||
│ 用户登录 │ │ API Key │ │ 服务间 │
|
||||
│ JWT 签发 │ │ Hash验证 │ │ Client │
|
||||
│ │ │ │ │ Credentials│
|
||||
└──────────┘ └──────────┘ └──────────┘
|
||||
|
||||
JWT Payload:
|
||||
{
|
||||
"sub": "user-uuid",
|
||||
"tenant_id": "tenant-uuid",
|
||||
"role": "developer",
|
||||
"exp": 1719552000,
|
||||
"iat": 1719548400
|
||||
}
|
||||
部署方式: Docker Compose
|
||||
对接方式: OAuth2 / OIDC
|
||||
功能:
|
||||
- 用户注册/登录 (手机号/邮箱/微信)
|
||||
- JWT Token 签发与验证
|
||||
- 第三方登录 (微信/钉钉/飞书)
|
||||
- 用户管理界面
|
||||
- RBAC 权限管理
|
||||
集成点:
|
||||
- ai-paas-gateway 调用 Casdoor API 验证 Token
|
||||
- NEW API 配置 Casdoor 作为认证后端
|
||||
```
|
||||
|
||||
### 5.2 API Key 安全
|
||||
### 6.2 NEW API (LLM 网关)
|
||||
|
||||
```
|
||||
用户创建 Key:
|
||||
明文 Key: aip_xxxxxxxxxxxxxxxxxxxxxxxx (仅展示一次)
|
||||
存储: SHA-256(aip_xxx) → db
|
||||
|
||||
用户调用:
|
||||
Header: Authorization: Bearer aip_xxxxxxxxxxxxxxxxxxxxxxxx
|
||||
验证: SHA-256(bearer_token) → 查 DB 匹配
|
||||
部署方式: Docker Compose
|
||||
对接方式: OpenAI 兼容 API
|
||||
功能:
|
||||
- 多模型统一管理 (DeepSeek/千问/豆包/智谱/元宝/百度)
|
||||
- 负载均衡与故障转移
|
||||
- 流式 SSE 透传
|
||||
- 用量统计与配额管理
|
||||
- 渠道管理 (API Key 池)
|
||||
集成点:
|
||||
- ai-paas-gateway 作为唯一上游调用 NEW API
|
||||
- NEW API 配置各 LLM 平台的 API Key
|
||||
- NEW API 的用量数据同步到 ai-paas-metering
|
||||
```
|
||||
|
||||
### 5.3 敏感数据加密
|
||||
### 6.3 Ollama (本地 LLM)
|
||||
|
||||
| 数据 | 加密方式 | 说明 |
|
||||
|------|---------|------|
|
||||
| API Key 明文 | 不存储 | 仅展示一次 |
|
||||
| API Key 哈希 | SHA-256 | 用于验证 |
|
||||
| LLM API Key | AES-256-GCM | 存储在 DB |
|
||||
| 用户密码 | bcrypt | Keycloak 管理 |
|
||||
| Credit 余额 | 明文(整数) | 需要原子操作 |
|
||||
```
|
||||
部署方式: Docker + GPU 直通
|
||||
支持模型: Qwen2.5, Llama3, Mistral, Phi3 等
|
||||
功能:
|
||||
- 本地模型推理 (离线可用)
|
||||
- 模型拉取/管理
|
||||
- OpenAI 兼容 API
|
||||
集成点:
|
||||
- NEW API 添加 Ollama 作为渠道
|
||||
- 提供「本地模型」标签,免费/低价使用
|
||||
```
|
||||
|
||||
### 6.4 LangFlow (Agent 编排)
|
||||
|
||||
```
|
||||
部署方式: Docker Compose
|
||||
功能:
|
||||
- 可视化 Agent 工作流编排
|
||||
- 预置 Agent 模板
|
||||
- 自定义工具链
|
||||
集成点:
|
||||
- ai-paas-registry 注册 LangFlow 工作流为 Agent 能力
|
||||
- 调用链: 用户 → gateway → LangFlow → LLM
|
||||
```
|
||||
|
||||
### 6.5 APISIX (API 网关)
|
||||
|
||||
```
|
||||
部署方式: Docker Compose
|
||||
功能:
|
||||
- 请求路由与负载均衡
|
||||
- 限流限速 (多维度)
|
||||
- 认证鉴权 (JWT/API Key)
|
||||
- 日志采集与监控
|
||||
- 插件扩展
|
||||
集成点:
|
||||
- 所有外部请求入口
|
||||
- 路由规则: /v1/chat/* → gateway, /api/* → gateway
|
||||
- 鉴权插件对接 Casdoor
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 6. 部署架构
|
||||
## 7. 数据库架构
|
||||
|
||||
### 6.1 开发环境 (Docker Compose)
|
||||
### 7.1 数据存储选型
|
||||
|
||||
| 存储 | 用途 | 选型理由 |
|
||||
|------|------|---------|
|
||||
| PostgreSQL 16 | 主数据库 | JSONB + RLS + 全文搜索 |
|
||||
| Redis 7 | 缓存/计费/会话 | Lua 原子操作 |
|
||||
| Kafka 3.x | 用量事件流 | 高吞吐日志型存储 |
|
||||
|
||||
### 7.2 核心数据表(扁平化设计,无租户概念)
|
||||
|
||||
| 表名 | 用途 | 关键字段 |
|
||||
|------|------|---------|
|
||||
| users | 用户 | id, phone, email, name, role, verified, subscribed |
|
||||
| api_keys | API 密钥 | id, user_id, key_hash, scopes |
|
||||
| subscriptions | 订阅 | id, user_id, plan, credits_included, credits_used |
|
||||
| credit_transactions | Credit 流水 | id, user_id, type, amount, balance_after |
|
||||
| token_usage | Token 用量 | id, user_id, model, tokens, cost |
|
||||
| capabilities | AI 能力 | id, user_id, name, type, status |
|
||||
| skills | 技能市场 | id, developer_id, name, pricing, rating |
|
||||
| skill_purchases | 技能购买 | id, user_id, skill_id, amount |
|
||||
|
||||
> **注意**: v2.0 去掉了 `tenants` 表和所有 `tenant_id` 字段,所有表直接挂在 `user_id` 下。
|
||||
|
||||
---
|
||||
|
||||
## 8. 部署架构
|
||||
|
||||
### 8.1 开发环境 (docker-compose.yml)
|
||||
|
||||
```yaml
|
||||
# docker-compose.yml 核心服务
|
||||
services:
|
||||
# 基础设施
|
||||
postgresql: # :5432
|
||||
redis: # :6379
|
||||
kafka: # :9092
|
||||
keycloak: # :8080
|
||||
apisix: # :9080 (HTTP) / :9443 (HTTPS)
|
||||
etcd: # :2379
|
||||
|
||||
# 自研服务
|
||||
auth-service: # :8081
|
||||
registry-service: # :8082
|
||||
openapi-gateway: # :8083
|
||||
billing-service: # :8084
|
||||
skills-service: # :8085
|
||||
admin-service: # :8086
|
||||
im-gateway: # :8087
|
||||
metering-service: # :8090
|
||||
llm-router: # :8091
|
||||
|
||||
postgres:
|
||||
image: postgres:16
|
||||
redis:
|
||||
image: redis:7-alpine
|
||||
kafka:
|
||||
image: confluentinc/cp-kafka:7.6.0
|
||||
|
||||
# 开源组件
|
||||
casdoor:
|
||||
image: casbin/casdoor:latest
|
||||
new-api:
|
||||
image: justsong/one-api:latest
|
||||
ollama:
|
||||
image: ollama/ollama:latest
|
||||
apisix:
|
||||
image: apache/apisix:3.15.0
|
||||
|
||||
# 自研服务 (FastAPI)
|
||||
ai-paas-gateway:
|
||||
build: ./services/gateway
|
||||
ai-paas-billing:
|
||||
build: ./services/billing
|
||||
ai-paas-registry:
|
||||
build: ./services/registry
|
||||
ai-paas-skills:
|
||||
build: ./services/skills
|
||||
ai-paas-metering:
|
||||
build: ./services/metering
|
||||
ai-paas-admin:
|
||||
build: ./services/admin
|
||||
|
||||
# 前端
|
||||
web: # :3000 (React Dev Server)
|
||||
admin: # :3001 (React Dev Server)
|
||||
|
||||
# 开源 AI 服务
|
||||
langflow: # :7860
|
||||
mcp-hub: # :3001
|
||||
milvus: # :19530
|
||||
|
||||
# 监控
|
||||
prometheus: # :9090
|
||||
grafana: # :3002
|
||||
web:
|
||||
build: ./packages/web
|
||||
```
|
||||
|
||||
### 6.2 生产环境 (Kubernetes)
|
||||
### 8.2 生产环境
|
||||
|
||||
```
|
||||
┌─────────────────────────────────────────────────┐
|
||||
│ Kubernetes Cluster │
|
||||
│ │
|
||||
│ ┌────────────────────────────────────────────┐ │
|
||||
│ │ Namespace: aipaaS-prod │ │
|
||||
│ │ │ │
|
||||
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
|
||||
│ │ │ APISIX │ │ APISIX │ │ APISIX │ │ │
|
||||
│ │ │ Pod x3 │ │ Pod x3 │ │ Pod x3 │ │ │
|
||||
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
|
||||
│ │ │ │
|
||||
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
|
||||
│ │ │ auth-svc│ │billing │ │registry │ │ │
|
||||
│ │ │ x2 │ │ x2 │ │ x2 │ │ │
|
||||
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
|
||||
│ │ │ │
|
||||
│ │ ┌─────────┐ ┌─────────┐ ┌─────────┐ │ │
|
||||
│ │ │llm-route│ │openapi │ │skills │ │ │
|
||||
│ │ │ x2 │ │ x2 │ │ x2 │ │ │
|
||||
│ │ └─────────┘ └─────────┘ └─────────┘ │ │
|
||||
│ │ │ │
|
||||
│ │ HPA: CPU > 70% 时自动扩容 │ │
|
||||
│ │ PDB: 至少保留 1 个 Pod 可用 │ │
|
||||
│ └────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌────────────────────────────────────────────┐ │
|
||||
│ │ Namespace: aipaaS-data │ │
|
||||
│ │ PostgreSQL (Primary + Replica) │ │
|
||||
│ │ Redis (Sentinel) │ │
|
||||
│ │ Kafka (3 Brokers) │ │
|
||||
│ │ Milvus (Standalone) │ │
|
||||
│ └────────────────────────────────────────────┘ │
|
||||
│ │
|
||||
│ ┌────────────────────────────────────────────┐ │
|
||||
│ │ Namespace: aipaaS-monitor │ │
|
||||
│ │ Prometheus + Grafana + Loki │ │
|
||||
│ └────────────────────────────────────────────┘ │
|
||||
└─────────────────────────────────────────────────┘
|
||||
Kubernetes + Helm Charts
|
||||
├── 基础设施: PostgreSQL HA / Redis Sentinel / Kafka Cluster
|
||||
├── 开源组件: Casdoor / NEW API / Ollama (GPU Node) / APISIX
|
||||
├── 自研服务: FastAPI × N replicas (HPA 自动伸缩)
|
||||
├── 监控: Prometheus + Grafana
|
||||
└── 日志: Loki + Promtail
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 7. 技术栈汇总
|
||||
## 9. 安全架构
|
||||
|
||||
| 层级 | 技术 | 版本 | 许可证 | 用途 |
|
||||
|------|------|------|--------|------|
|
||||
| **前端** | React + TypeScript | 18+ / 5+ | MIT | Web UI |
|
||||
| **UI 框架** | Ant Design Pro | 6+ | MIT | 组件库 |
|
||||
| **图表** | ECharts | 5+ | Apache 2.0 | 数据可视化 |
|
||||
| **后端** | Go | 1.22+ | BSD-3 | 核心服务 |
|
||||
| **AI 服务** | Python | 3.11+ | PSF | LangFlow/MCP |
|
||||
| **数据库** | PostgreSQL | 16+ | PostgreSQL | 主存储 |
|
||||
| **缓存** | Redis | 7+ | BSD-3 | 缓存+Lua计费 |
|
||||
| **消息队列** | Kafka | 3.x+ | Apache 2.0 | 事件总线 |
|
||||
| **向量库** | Milvus | 2.x+ | Apache 2.0 | RAG |
|
||||
| **认证** | Keycloak | 25+ | Apache 2.0 | SSO |
|
||||
| **网关** | Apache APISIX | 3.15+ | Apache 2.0 | API Gateway |
|
||||
| **Agent** | LangFlow | latest | MIT | 工作流编排 |
|
||||
| **MCP** | mcp-hub | latest | MIT | MCP 管理 |
|
||||
| **监控** | Prometheus+Grafana | latest | Apache 2.0 | 可观测性 |
|
||||
| **开发工具** | MiMo Code | latest | Apache 2.0 | AI 编程 |
|
||||
### 9.1 认证链路
|
||||
```
|
||||
外部请求 → APISIX
|
||||
→ JWT 验证 (Casdoor 签发)
|
||||
→ API Key 验证 (查 PostgreSQL)
|
||||
→ 用户状态检查
|
||||
```
|
||||
|
||||
### 9.2 数据安全
|
||||
- 实名信息: AES-256 加密存储
|
||||
- API Key: SHA-256 哈希存储
|
||||
- 密码: bcrypt 加密
|
||||
- 通信: HTTPS 全站加密
|
||||
|
||||
### 9.3 接口安全
|
||||
- 限流: IP + 用户 + 接口多维度
|
||||
- 防重放: 请求签名 + 时间戳
|
||||
- 输入校验: Pydantic 强类型校验
|
||||
|
||||
---
|
||||
|
||||
## 10. 开发路线图(v2.0 调整)
|
||||
|
||||
### Phase 1(第 1-3 周): 基座搭建
|
||||
- 部署 Casdoor + NEW API + APISIX + PostgreSQL + Redis
|
||||
- 开发 ai-paas-gateway 核心路由
|
||||
- 实现 JWT 认证 + API Key 管理
|
||||
- 实现基础 LLM 调用链路 (gateway → NEW API → LLM)
|
||||
- **里程碑**: 用户注册 → 登录 → 调用 LLM 对话
|
||||
|
||||
### Phase 2(第 4-6 周): 计费引擎
|
||||
- 开发 ai-paas-billing
|
||||
- Redis Lua 原子扣费
|
||||
- 订阅管理 (套餐/充值/欠费)
|
||||
- 用量追踪 (Kafka → metering)
|
||||
- **里程碑**: 用户充值 → 调用扣费 → 账单可查
|
||||
|
||||
### Phase 3(第 7-9 周): 能力市场
|
||||
- 开发 ai-paas-registry (能力 CRUD)
|
||||
- 开发 ai-paas-skills (技能交易)
|
||||
- 阿里云实名认证对接
|
||||
- 能力审核流程
|
||||
- **里程碑**: 开发者发布能力 → 用户购买使用
|
||||
|
||||
### Phase 4(第 10-12 周): 生态扩展
|
||||
- Ollama 本地模型集成
|
||||
- IM Bot 多平台对接
|
||||
- MCP 工具链集成
|
||||
- LangFlow Agent 编排
|
||||
- **里程碑**: 全生态打通
|
||||
|
||||
### Phase 5(第 13-15 周): 上线准备
|
||||
- 全链路测试
|
||||
- 性能压测
|
||||
- 监控告警
|
||||
- 文档完善
|
||||
- **里程碑**: 正式上线
|
||||
|
||||
> **v2.0 变化**: 原 20 周 → 缩短为 15 周,得益于开源组件的复用。
|
||||
|
||||
---
|
||||
|
||||
|
||||
Reference in new issue
Block a user