Phase 8 Task 4: AI 能力增强
- 创建 ai_manager.py - AI 能力增强管理模块 - 自定义模型训练(领域特定实体识别) - 多模态大模型集成(GPT-4V、Claude 3、Gemini、Kimi-VL) - 智能摘要与问答(基于知识图谱的 RAG) - 预测性分析(趋势预测、异常检测、实体增长、关系演变) - 更新 schema.sql - 添加 AI 能力增强相关数据库表 - custom_models, training_samples - multimodal_analyses - kg_rag_configs, rag_queries - smart_summaries - prediction_models, prediction_results - 更新 main.py - 添加 30+ 个 AI 相关 API 端点 - 自定义模型管理(创建、训练、预测) - 多模态分析 - 知识图谱 RAG - 智能摘要 - 预测模型(创建、训练、预测、反馈) - 创建 test_phase8_task4.py - 测试脚本 - 更新 README.md 和 STATUS.md
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
## 项目概述
|
||||
InsightFlow 是一个智能知识管理平台,支持从会议记录、文档中提取实体和关系,构建知识图谱。
|
||||
|
||||
## 当前阶段:Phase 8 - 多租户 SaaS 架构
|
||||
## 当前阶段:Phase 8 - 商业化与规模化
|
||||
|
||||
### 已完成任务
|
||||
|
||||
@@ -45,38 +45,127 @@ InsightFlow 是一个智能知识管理平台,支持从会议记录、文档
|
||||
- ✅ `requirements.txt` - 无需新增依赖
|
||||
- ✅ `test_tenant.py` - 测试脚本
|
||||
|
||||
#### Phase 8 Task 2: 订阅与计费系统 (P0 - 最高优先级) ✅
|
||||
|
||||
**功能实现:**
|
||||
|
||||
1. **多层级订阅计划**(Free/Pro/Enterprise)✅
|
||||
2. **按量计费**(转录时长、存储空间、API 调用次数)✅
|
||||
3. **支付集成**(Stripe、支付宝、微信支付)✅
|
||||
4. **发票管理、退款处理、账单历史**✅
|
||||
|
||||
**技术实现:**
|
||||
|
||||
- ✅ `subscription_manager.py` - 订阅与计费管理模块
|
||||
- ✅ `schema.sql` - 添加订阅相关数据库表
|
||||
- ✅ `main.py` - 添加 26 个 API 端点
|
||||
|
||||
#### Phase 8 Task 3: 企业级功能 (P1 - 高优先级) ✅
|
||||
|
||||
**功能实现:**
|
||||
|
||||
1. **SSO/SAML 单点登录**(企业微信、钉钉、飞书、Okta)✅
|
||||
2. **SCIM 用户目录同步**✅
|
||||
3. **审计日志导出**(SOC2/ISO27001 合规)✅
|
||||
4. **数据保留策略**(自动归档、数据删除)✅
|
||||
|
||||
**技术实现:**
|
||||
|
||||
- ✅ `enterprise_manager.py` - 企业级功能管理模块
|
||||
- ✅ `schema.sql` - 添加企业级功能相关数据库表
|
||||
- ✅ `main.py` - 添加 25 个 API 端点
|
||||
|
||||
#### Phase 8 Task 4: AI 能力增强 (P1 - 高优先级) ✅
|
||||
|
||||
**功能实现:**
|
||||
|
||||
1. **自定义模型训练**(领域特定实体识别)✅
|
||||
- CustomModel/ModelType/ModelStatus 数据模型
|
||||
- TrainingSample 训练样本管理
|
||||
- 模型训练流程(创建、添加样本、训练、预测)
|
||||
|
||||
2. **多模态大模型集成**(GPT-4V、Claude 3)✅
|
||||
- MultimodalAnalysis 多模态分析
|
||||
- 支持 GPT-4V、Claude 3、Gemini、Kimi-VL
|
||||
- 图片、视频、音频、混合输入分析
|
||||
|
||||
3. **智能摘要与问答**(基于知识图谱的 RAG)✅
|
||||
- KnowledgeGraphRAG 配置管理
|
||||
- RAGQuery 查询记录
|
||||
- SmartSummary 智能摘要(extractive/abstractive/key_points/timeline)
|
||||
|
||||
4. **预测性分析**(趋势预测、异常检测)✅
|
||||
- PredictionModel/PredictionType 预测模型管理
|
||||
- 趋势预测、异常检测、实体增长预测、关系演变预测
|
||||
- PredictionResult 预测结果管理
|
||||
|
||||
**技术实现:**
|
||||
|
||||
- ✅ `ai_manager.py` - AI 能力增强管理模块(1330+ 行代码)
|
||||
- AIManager: AI 能力管理主类
|
||||
- 自定义模型训练流程
|
||||
- 多模态分析(GPT-4V、Claude 3、Gemini、Kimi-VL)
|
||||
- 知识图谱 RAG 检索与生成
|
||||
- 智能摘要生成(多种类型)
|
||||
- 预测性分析(趋势、异常、增长、演变)
|
||||
|
||||
- ✅ `schema.sql` - 添加 AI 能力增强相关数据库表
|
||||
- `custom_models` - 自定义模型表
|
||||
- `training_samples` - 训练样本表
|
||||
- `multimodal_analyses` - 多模态分析表
|
||||
- `kg_rag_configs` - 知识图谱 RAG 配置表
|
||||
- `rag_queries` - RAG 查询记录表
|
||||
- `smart_summaries` - 智能摘要表
|
||||
- `prediction_models` - 预测模型表
|
||||
- `prediction_results` - 预测结果表
|
||||
|
||||
- ✅ `main.py` - 添加 30+ 个 API 端点
|
||||
- 自定义模型管理(创建、训练、预测)
|
||||
- 多模态分析
|
||||
- 知识图谱 RAG(配置、查询)
|
||||
- 智能摘要
|
||||
- 预测模型(创建、训练、预测、反馈)
|
||||
|
||||
- ✅ `test_phase8_task4.py` - 测试脚本
|
||||
|
||||
**API 端点:**
|
||||
|
||||
租户管理:
|
||||
- `POST /api/v1/tenants` - 创建租户
|
||||
- `GET /api/v1/tenants` - 列出租户
|
||||
- `GET /api/v1/tenants/{tenant_id}` - 获取租户详情
|
||||
- `PUT /api/v1/tenants/{tenant_id}` - 更新租户
|
||||
- `DELETE /api/v1/tenants/{tenant_id}` - 删除租户
|
||||
自定义模型管理:
|
||||
- `POST /api/v1/tenants/{tenant_id}/ai/custom-models` - 创建自定义模型
|
||||
- `GET /api/v1/tenants/{tenant_id}/ai/custom-models` - 列出自定义模型
|
||||
- `GET /api/v1/ai/custom-models/{model_id}` - 获取模型详情
|
||||
- `POST /api/v1/ai/custom-models/{model_id}/samples` - 添加训练样本
|
||||
- `GET /api/v1/ai/custom-models/{model_id}/samples` - 获取训练样本
|
||||
- `POST /api/v1/ai/custom-models/{model_id}/train` - 训练模型
|
||||
- `POST /api/v1/ai/custom-models/predict` - 模型预测
|
||||
|
||||
域名管理:
|
||||
- `POST /api/v1/tenants/{tenant_id}/domains` - 添加域名
|
||||
- `GET /api/v1/tenants/{tenant_id}/domains` - 列出自定义域名
|
||||
- `POST /api/v1/tenants/{tenant_id}/domains/{domain_id}/verify` - 验证域名
|
||||
- `DELETE /api/v1/tenants/{tenant_id}/domains/{domain_id}` - 移除域名
|
||||
多模态分析:
|
||||
- `POST /api/v1/tenants/{tenant_id}/projects/{project_id}/ai/multimodal` - 多模态分析
|
||||
- `GET /api/v1/tenants/{tenant_id}/ai/multimodal` - 获取多模态分析历史
|
||||
|
||||
品牌配置:
|
||||
- `GET /api/v1/tenants/{tenant_id}/branding` - 获取品牌配置
|
||||
- `PUT /api/v1/tenants/{tenant_id}/branding` - 更新品牌配置
|
||||
- `GET /api/v1/tenants/{tenant_id}/branding.css` - 获取品牌 CSS
|
||||
知识图谱 RAG:
|
||||
- `POST /api/v1/tenants/{tenant_id}/projects/{project_id}/ai/kg-rag` - 创建 RAG 配置
|
||||
- `GET /api/v1/tenants/{tenant_id}/ai/kg-rag` - 列出 RAG 配置
|
||||
- `POST /api/v1/ai/kg-rag/query` - 知识图谱 RAG 查询
|
||||
|
||||
成员管理:
|
||||
- `POST /api/v1/tenants/{tenant_id}/members` - 邀请成员
|
||||
- `GET /api/v1/tenants/{tenant_id}/members` - 列出成员
|
||||
- `PUT /api/v1/tenants/{tenant_id}/members/{member_id}` - 更新成员
|
||||
- `DELETE /api/v1/tenants/{tenant_id}/members/{member_id}` - 移除成员
|
||||
智能摘要:
|
||||
- `POST /api/v1/tenants/{tenant_id}/projects/{project_id}/ai/summarize` - 生成智能摘要
|
||||
|
||||
**测试状态:** ✅ 所有测试通过
|
||||
预测模型:
|
||||
- `POST /api/v1/tenants/{tenant_id}/projects/{project_id}/ai/prediction-models` - 创建预测模型
|
||||
- `GET /api/v1/tenants/{tenant_id}/ai/prediction-models` - 列出预测模型
|
||||
- `GET /api/v1/ai/prediction-models/{model_id}` - 获取预测模型详情
|
||||
- `POST /api/v1/ai/prediction-models/{model_id}/train` - 训练预测模型
|
||||
- `POST /api/v1/ai/prediction-models/predict` - 进行预测
|
||||
- `GET /api/v1/ai/prediction-models/{model_id}/results` - 获取预测结果历史
|
||||
- `POST /api/v1/ai/prediction-results/feedback` - 更新预测反馈
|
||||
|
||||
**测试状态:** ✅ 核心功能测试通过
|
||||
|
||||
运行测试:
|
||||
```bash
|
||||
cd /root/.openclaw/workspace/projects/insightflow/backend
|
||||
python3 test_tenant.py
|
||||
python3 test_phase8_task4.py
|
||||
```
|
||||
|
||||
## 历史阶段
|
||||
@@ -123,10 +212,9 @@ python3 test_tenant.py
|
||||
## 待办事项
|
||||
|
||||
### Phase 8 后续任务
|
||||
- [ ] 租户计费系统集成
|
||||
- [ ] 租户数据备份与恢复
|
||||
- [ ] 租户间数据迁移
|
||||
- [ ] 租户级审计日志
|
||||
- [ ] Task 5: 运营与增长工具
|
||||
- [ ] Task 6: 开发者生态
|
||||
- [ ] Task 8: 运维与监控
|
||||
|
||||
### 技术债务
|
||||
- [ ] 完善单元测试覆盖
|
||||
@@ -135,6 +223,7 @@ python3 test_tenant.py
|
||||
|
||||
## 最近更新
|
||||
|
||||
- 2025-02-25: Phase 8 Task 1 完成 - 多租户 SaaS 架构
|
||||
- 2025-02-24: Phase 7 完成 - 插件与集成
|
||||
- 2025-02-23: Phase 6 完成 - API 平台
|
||||
- 2026-02-26: Phase 8 Task 4 完成 - AI 能力增强
|
||||
- 2026-02-25: Phase 8 Task 1/2/3/7 完成 - 多租户、订阅计费、企业级功能、全球化
|
||||
- 2026-02-24: Phase 7 完成 - 插件与集成
|
||||
- 2026-02-23: Phase 6 完成 - API 平台
|
||||
|
||||
1359
backend/ai_manager.py
Normal file
1359
backend/ai_manager.py
Normal file
File diff suppressed because it is too large
Load Diff
1443
backend/main.py
1443
backend/main.py
File diff suppressed because it is too large
Load Diff
@@ -1406,3 +1406,320 @@ CREATE INDEX IF NOT EXISTS idx_retention_tenant ON data_retention_policies(tenan
|
||||
CREATE INDEX IF NOT EXISTS idx_retention_type ON data_retention_policies(resource_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_retention_jobs_policy ON data_retention_jobs(policy_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_retention_jobs_status ON data_retention_jobs(status);
|
||||
|
||||
-- ============================================
|
||||
-- Phase 8 Task 7: 全球化与本地化
|
||||
-- ============================================
|
||||
|
||||
-- 翻译表
|
||||
CREATE TABLE IF NOT EXISTS translations (
|
||||
id TEXT PRIMARY KEY,
|
||||
key TEXT NOT NULL,
|
||||
language TEXT NOT NULL,
|
||||
value TEXT NOT NULL,
|
||||
namespace TEXT DEFAULT 'common',
|
||||
context TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
is_reviewed INTEGER DEFAULT 0,
|
||||
reviewed_by TEXT,
|
||||
reviewed_at TIMESTAMP,
|
||||
UNIQUE(key, language, namespace)
|
||||
);
|
||||
|
||||
-- 语言配置表
|
||||
CREATE TABLE IF NOT EXISTS language_configs (
|
||||
code TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
name_local TEXT NOT NULL,
|
||||
is_rtl INTEGER DEFAULT 0,
|
||||
is_active INTEGER DEFAULT 1,
|
||||
is_default INTEGER DEFAULT 0,
|
||||
fallback_language TEXT,
|
||||
date_format TEXT,
|
||||
time_format TEXT,
|
||||
datetime_format TEXT,
|
||||
number_format TEXT,
|
||||
currency_format TEXT,
|
||||
first_day_of_week INTEGER DEFAULT 1,
|
||||
calendar_type TEXT DEFAULT 'gregorian'
|
||||
);
|
||||
|
||||
-- 数据中心表
|
||||
CREATE TABLE IF NOT EXISTS data_centers (
|
||||
id TEXT PRIMARY KEY,
|
||||
region_code TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
location TEXT NOT NULL,
|
||||
endpoint TEXT NOT NULL,
|
||||
status TEXT DEFAULT 'active',
|
||||
priority INTEGER DEFAULT 1,
|
||||
supported_regions TEXT DEFAULT '[]',
|
||||
capabilities TEXT DEFAULT '{}',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 租户数据中心映射表
|
||||
CREATE TABLE IF NOT EXISTS tenant_data_center_mappings (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL UNIQUE,
|
||||
primary_dc_id TEXT NOT NULL,
|
||||
secondary_dc_id TEXT,
|
||||
region_code TEXT NOT NULL,
|
||||
data_residency TEXT DEFAULT 'regional',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (primary_dc_id) REFERENCES data_centers(id),
|
||||
FOREIGN KEY (secondary_dc_id) REFERENCES data_centers(id)
|
||||
);
|
||||
|
||||
-- 本地化支付方式表
|
||||
CREATE TABLE IF NOT EXISTS localized_payment_methods (
|
||||
id TEXT PRIMARY KEY,
|
||||
provider TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL,
|
||||
name_local TEXT DEFAULT '{}',
|
||||
supported_countries TEXT DEFAULT '[]',
|
||||
supported_currencies TEXT DEFAULT '[]',
|
||||
is_active INTEGER DEFAULT 1,
|
||||
config TEXT DEFAULT '{}',
|
||||
icon_url TEXT,
|
||||
display_order INTEGER DEFAULT 0,
|
||||
min_amount REAL,
|
||||
max_amount REAL,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
-- 国家配置表
|
||||
CREATE TABLE IF NOT EXISTS country_configs (
|
||||
code TEXT PRIMARY KEY,
|
||||
code3 TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
name_local TEXT DEFAULT '{}',
|
||||
region TEXT NOT NULL,
|
||||
default_language TEXT NOT NULL,
|
||||
supported_languages TEXT DEFAULT '[]',
|
||||
default_currency TEXT NOT NULL,
|
||||
supported_currencies TEXT DEFAULT '[]',
|
||||
timezone TEXT NOT NULL,
|
||||
calendar_type TEXT DEFAULT 'gregorian',
|
||||
date_format TEXT,
|
||||
time_format TEXT,
|
||||
number_format TEXT,
|
||||
address_format TEXT,
|
||||
phone_format TEXT,
|
||||
vat_rate REAL,
|
||||
is_active INTEGER DEFAULT 1
|
||||
);
|
||||
|
||||
-- 时区配置表
|
||||
CREATE TABLE IF NOT EXISTS timezone_configs (
|
||||
id TEXT PRIMARY KEY,
|
||||
timezone TEXT NOT NULL UNIQUE,
|
||||
utc_offset TEXT NOT NULL,
|
||||
dst_offset TEXT,
|
||||
country_code TEXT NOT NULL,
|
||||
region TEXT NOT NULL,
|
||||
is_active INTEGER DEFAULT 1
|
||||
);
|
||||
|
||||
-- 货币配置表
|
||||
CREATE TABLE IF NOT EXISTS currency_configs (
|
||||
code TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
name_local TEXT DEFAULT '{}',
|
||||
symbol TEXT NOT NULL,
|
||||
decimal_places INTEGER DEFAULT 2,
|
||||
decimal_separator TEXT DEFAULT '.',
|
||||
thousands_separator TEXT DEFAULT ',',
|
||||
is_active INTEGER DEFAULT 1
|
||||
);
|
||||
|
||||
-- 租户本地化设置表
|
||||
CREATE TABLE IF NOT EXISTS localization_settings (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL UNIQUE,
|
||||
default_language TEXT DEFAULT 'en',
|
||||
supported_languages TEXT DEFAULT '["en"]',
|
||||
default_currency TEXT DEFAULT 'USD',
|
||||
supported_currencies TEXT DEFAULT '["USD"]',
|
||||
default_timezone TEXT DEFAULT 'UTC',
|
||||
default_date_format TEXT,
|
||||
default_time_format TEXT,
|
||||
default_number_format TEXT,
|
||||
calendar_type TEXT DEFAULT 'gregorian',
|
||||
first_day_of_week INTEGER DEFAULT 1,
|
||||
region_code TEXT DEFAULT 'global',
|
||||
data_residency TEXT DEFAULT 'regional',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 本地化相关索引
|
||||
CREATE INDEX IF NOT EXISTS idx_translations_key ON translations(key);
|
||||
CREATE INDEX IF NOT EXISTS idx_translations_lang ON translations(language);
|
||||
CREATE INDEX IF NOT EXISTS idx_translations_ns ON translations(namespace);
|
||||
CREATE INDEX IF NOT EXISTS idx_dc_region ON data_centers(region_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_dc_status ON data_centers(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_tenant_dc ON tenant_data_center_mappings(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_payment_provider ON localized_payment_methods(provider);
|
||||
CREATE INDEX IF NOT EXISTS idx_payment_active ON localized_payment_methods(is_active);
|
||||
CREATE INDEX IF NOT EXISTS idx_country_region ON country_configs(region);
|
||||
CREATE INDEX IF NOT EXISTS idx_tz_country ON timezone_configs(country_code);
|
||||
CREATE INDEX IF NOT EXISTS idx_locale_settings_tenant ON localization_settings(tenant_id);
|
||||
|
||||
-- ============================================
|
||||
-- Phase 8 Task 4: AI 能力增强
|
||||
-- ============================================
|
||||
|
||||
-- 自定义模型表
|
||||
CREATE TABLE IF NOT EXISTS custom_models (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
model_type TEXT NOT NULL, -- custom_ner, multimodal, summarization, prediction
|
||||
status TEXT DEFAULT 'pending', -- pending, training, ready, failed, archived
|
||||
training_data TEXT DEFAULT '{}',
|
||||
hyperparameters TEXT DEFAULT '{}',
|
||||
metrics TEXT DEFAULT '{}',
|
||||
model_path TEXT,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
trained_at TIMESTAMP,
|
||||
created_by TEXT NOT NULL,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 训练样本表
|
||||
CREATE TABLE IF NOT EXISTS training_samples (
|
||||
id TEXT PRIMARY KEY,
|
||||
model_id TEXT NOT NULL,
|
||||
text TEXT NOT NULL,
|
||||
entities TEXT DEFAULT '[]', -- JSON: [{"start": 0, "end": 5, "label": "PERSON", "text": "..."}]
|
||||
metadata TEXT DEFAULT '{}',
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (model_id) REFERENCES custom_models(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 多模态分析表
|
||||
CREATE TABLE IF NOT EXISTS multimodal_analyses (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
provider TEXT NOT NULL, -- gpt-4-vision, claude-3, gemini-pro-vision, kimi-vl
|
||||
input_type TEXT NOT NULL, -- image, video, audio, mixed
|
||||
input_urls TEXT DEFAULT '[]',
|
||||
prompt TEXT NOT NULL,
|
||||
result TEXT DEFAULT '{}',
|
||||
tokens_used INTEGER DEFAULT 0,
|
||||
cost REAL DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 知识图谱 RAG 配置表
|
||||
CREATE TABLE IF NOT EXISTS kg_rag_configs (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT,
|
||||
kg_config TEXT DEFAULT '{}', -- 知识图谱配置
|
||||
retrieval_config TEXT DEFAULT '{}', -- 检索配置
|
||||
generation_config TEXT DEFAULT '{}', -- 生成配置
|
||||
is_active INTEGER DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- RAG 查询记录表
|
||||
CREATE TABLE IF NOT EXISTS rag_queries (
|
||||
id TEXT PRIMARY KEY,
|
||||
rag_id TEXT NOT NULL,
|
||||
query TEXT NOT NULL,
|
||||
context TEXT DEFAULT '{}',
|
||||
answer TEXT NOT NULL,
|
||||
sources TEXT DEFAULT '[]',
|
||||
confidence REAL DEFAULT 0,
|
||||
tokens_used INTEGER DEFAULT 0,
|
||||
latency_ms INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (rag_id) REFERENCES kg_rag_configs(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 智能摘要表
|
||||
CREATE TABLE IF NOT EXISTS smart_summaries (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
source_type TEXT NOT NULL, -- transcript, entity, project
|
||||
source_id TEXT NOT NULL,
|
||||
summary_type TEXT NOT NULL, -- extractive, abstractive, key_points, timeline
|
||||
content TEXT NOT NULL,
|
||||
key_points TEXT DEFAULT '[]',
|
||||
entities_mentioned TEXT DEFAULT '[]',
|
||||
confidence REAL DEFAULT 0,
|
||||
tokens_used INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 预测模型表
|
||||
CREATE TABLE IF NOT EXISTS prediction_models (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
project_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
prediction_type TEXT NOT NULL, -- trend, anomaly, entity_growth, relation_evolution
|
||||
target_entity_type TEXT,
|
||||
features TEXT DEFAULT '[]',
|
||||
model_config TEXT DEFAULT '{}',
|
||||
accuracy REAL,
|
||||
last_trained_at TIMESTAMP,
|
||||
prediction_count INTEGER DEFAULT 0,
|
||||
is_active INTEGER DEFAULT 1,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (project_id) REFERENCES projects(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- 预测结果表
|
||||
CREATE TABLE IF NOT EXISTS prediction_results (
|
||||
id TEXT PRIMARY KEY,
|
||||
model_id TEXT NOT NULL,
|
||||
prediction_type TEXT NOT NULL,
|
||||
target_id TEXT,
|
||||
prediction_data TEXT DEFAULT '{}',
|
||||
confidence REAL DEFAULT 0,
|
||||
explanation TEXT,
|
||||
actual_value TEXT,
|
||||
is_correct INTEGER,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY (model_id) REFERENCES prediction_models(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
-- AI 能力增强相关索引
|
||||
CREATE INDEX IF NOT EXISTS idx_custom_models_tenant ON custom_models(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_custom_models_type ON custom_models(model_type);
|
||||
CREATE INDEX IF NOT EXISTS idx_custom_models_status ON custom_models(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_training_samples_model ON training_samples(model_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_multimodal_tenant ON multimodal_analyses(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_multimodal_project ON multimodal_analyses(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_kg_rag_tenant ON kg_rag_configs(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_kg_rag_project ON kg_rag_configs(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_rag_queries_rag ON rag_queries(rag_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_smart_summaries_tenant ON smart_summaries(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_smart_summaries_project ON smart_summaries(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prediction_models_tenant ON prediction_models(tenant_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prediction_models_project ON prediction_models(project_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_prediction_results_model ON prediction_results(model_id);
|
||||
|
||||
383
backend/test_phase8_task4.py
Normal file
383
backend/test_phase8_task4.py
Normal file
@@ -0,0 +1,383 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
InsightFlow Phase 8 Task 4 测试脚本
|
||||
测试 AI 能力增强功能
|
||||
"""
|
||||
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
|
||||
# Add backend directory to path
|
||||
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
||||
|
||||
from ai_manager import (
|
||||
get_ai_manager, CustomModel, TrainingSample, MultimodalAnalysis,
|
||||
KnowledgeGraphRAG, SmartSummary, PredictionModel, PredictionResult,
|
||||
ModelType, ModelStatus, MultimodalProvider, PredictionType
|
||||
)
|
||||
|
||||
|
||||
def test_custom_model():
|
||||
"""测试自定义模型功能"""
|
||||
print("\n=== 测试自定义模型 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 1. 创建自定义模型
|
||||
print("1. 创建自定义模型...")
|
||||
model = manager.create_custom_model(
|
||||
tenant_id="tenant_001",
|
||||
name="领域实体识别模型",
|
||||
description="用于识别医疗领域实体的自定义模型",
|
||||
model_type=ModelType.CUSTOM_NER,
|
||||
training_data={
|
||||
"entity_types": ["DISEASE", "SYMPTOM", "DRUG", "TREATMENT"],
|
||||
"domain": "medical"
|
||||
},
|
||||
hyperparameters={
|
||||
"epochs": 15,
|
||||
"learning_rate": 0.001,
|
||||
"batch_size": 32
|
||||
},
|
||||
created_by="user_001"
|
||||
)
|
||||
print(f" 创建成功: {model.id}, 状态: {model.status.value}")
|
||||
|
||||
# 2. 添加训练样本
|
||||
print("2. 添加训练样本...")
|
||||
samples = [
|
||||
{
|
||||
"text": "患者张三患有高血压,正在服用降压药治疗。",
|
||||
"entities": [
|
||||
{"start": 2, "end": 4, "label": "PERSON", "text": "张三"},
|
||||
{"start": 6, "end": 9, "label": "DISEASE", "text": "高血压"},
|
||||
{"start": 14, "end": 17, "label": "DRUG", "text": "降压药"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"text": "李四因感冒发烧到医院就诊,医生开具了退烧药。",
|
||||
"entities": [
|
||||
{"start": 0, "end": 2, "label": "PERSON", "text": "李四"},
|
||||
{"start": 3, "end": 5, "label": "SYMPTOM", "text": "感冒"},
|
||||
{"start": 5, "end": 7, "label": "SYMPTOM", "text": "发烧"},
|
||||
{"start": 21, "end": 24, "label": "DRUG", "text": "退烧药"}
|
||||
]
|
||||
},
|
||||
{
|
||||
"text": "王五接受了心脏搭桥手术,术后恢复良好。",
|
||||
"entities": [
|
||||
{"start": 0, "end": 2, "label": "PERSON", "text": "王五"},
|
||||
{"start": 5, "end": 11, "label": "TREATMENT", "text": "心脏搭桥手术"}
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
for sample_data in samples:
|
||||
sample = manager.add_training_sample(
|
||||
model_id=model.id,
|
||||
text=sample_data["text"],
|
||||
entities=sample_data["entities"],
|
||||
metadata={"source": "manual"}
|
||||
)
|
||||
print(f" 添加样本: {sample.id}")
|
||||
|
||||
# 3. 获取训练样本
|
||||
print("3. 获取训练样本...")
|
||||
all_samples = manager.get_training_samples(model.id)
|
||||
print(f" 共有 {len(all_samples)} 个训练样本")
|
||||
|
||||
# 4. 列出自定义模型
|
||||
print("4. 列出自定义模型...")
|
||||
models = manager.list_custom_models(tenant_id="tenant_001")
|
||||
print(f" 找到 {len(models)} 个模型")
|
||||
for m in models:
|
||||
print(f" - {m.name} ({m.model_type.value}): {m.status.value}")
|
||||
|
||||
return model.id
|
||||
|
||||
|
||||
async def test_train_and_predict(model_id: str):
|
||||
"""测试训练和预测"""
|
||||
print("\n=== 测试模型训练和预测 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 1. 训练模型
|
||||
print("1. 训练模型...")
|
||||
try:
|
||||
trained_model = await manager.train_custom_model(model_id)
|
||||
print(f" 训练完成: {trained_model.status.value}")
|
||||
print(f" 指标: {trained_model.metrics}")
|
||||
except Exception as e:
|
||||
print(f" 训练失败: {e}")
|
||||
return
|
||||
|
||||
# 2. 使用模型预测
|
||||
print("2. 使用模型预测...")
|
||||
test_text = "赵六患有糖尿病,正在使用胰岛素治疗。"
|
||||
try:
|
||||
entities = await manager.predict_with_custom_model(model_id, test_text)
|
||||
print(f" 输入: {test_text}")
|
||||
print(f" 预测实体: {entities}")
|
||||
except Exception as e:
|
||||
print(f" 预测失败: {e}")
|
||||
|
||||
|
||||
def test_prediction_models():
|
||||
"""测试预测模型"""
|
||||
print("\n=== 测试预测模型 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 1. 创建趋势预测模型
|
||||
print("1. 创建趋势预测模型...")
|
||||
trend_model = manager.create_prediction_model(
|
||||
tenant_id="tenant_001",
|
||||
project_id="project_001",
|
||||
name="实体数量趋势预测",
|
||||
prediction_type=PredictionType.TREND,
|
||||
target_entity_type="PERSON",
|
||||
features=["entity_count", "time_period", "document_count"],
|
||||
model_config={
|
||||
"algorithm": "linear_regression",
|
||||
"window_size": 7
|
||||
}
|
||||
)
|
||||
print(f" 创建成功: {trend_model.id}")
|
||||
|
||||
# 2. 创建异常检测模型
|
||||
print("2. 创建异常检测模型...")
|
||||
anomaly_model = manager.create_prediction_model(
|
||||
tenant_id="tenant_001",
|
||||
project_id="project_001",
|
||||
name="实体增长异常检测",
|
||||
prediction_type=PredictionType.ANOMALY,
|
||||
target_entity_type=None,
|
||||
features=["daily_growth", "weekly_growth"],
|
||||
model_config={
|
||||
"threshold": 2.5,
|
||||
"sensitivity": "medium"
|
||||
}
|
||||
)
|
||||
print(f" 创建成功: {anomaly_model.id}")
|
||||
|
||||
# 3. 列出预测模型
|
||||
print("3. 列出预测模型...")
|
||||
models = manager.list_prediction_models(tenant_id="tenant_001")
|
||||
print(f" 找到 {len(models)} 个预测模型")
|
||||
for m in models:
|
||||
print(f" - {m.name} ({m.prediction_type.value})")
|
||||
|
||||
return trend_model.id, anomaly_model.id
|
||||
|
||||
|
||||
async def test_predictions(trend_model_id: str, anomaly_model_id: str):
|
||||
"""测试预测功能"""
|
||||
print("\n=== 测试预测功能 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 1. 训练趋势预测模型
|
||||
print("1. 训练趋势预测模型...")
|
||||
historical_data = [
|
||||
{"date": "2024-01-01", "value": 10},
|
||||
{"date": "2024-01-02", "value": 12},
|
||||
{"date": "2024-01-03", "value": 15},
|
||||
{"date": "2024-01-04", "value": 14},
|
||||
{"date": "2024-01-05", "value": 18},
|
||||
{"date": "2024-01-06", "value": 20},
|
||||
{"date": "2024-01-07", "value": 22}
|
||||
]
|
||||
trained = await manager.train_prediction_model(trend_model_id, historical_data)
|
||||
print(f" 训练完成,准确率: {trained.accuracy}")
|
||||
|
||||
# 2. 趋势预测
|
||||
print("2. 趋势预测...")
|
||||
trend_result = await manager.predict(
|
||||
trend_model_id,
|
||||
{"historical_values": [10, 12, 15, 14, 18, 20, 22]}
|
||||
)
|
||||
print(f" 预测结果: {trend_result.prediction_data}")
|
||||
|
||||
# 3. 异常检测
|
||||
print("3. 异常检测...")
|
||||
anomaly_result = await manager.predict(
|
||||
anomaly_model_id,
|
||||
{
|
||||
"value": 50,
|
||||
"historical_values": [10, 12, 11, 13, 12, 14, 13]
|
||||
}
|
||||
)
|
||||
print(f" 检测结果: {anomaly_result.prediction_data}")
|
||||
|
||||
|
||||
def test_kg_rag():
|
||||
"""测试知识图谱 RAG"""
|
||||
print("\n=== 测试知识图谱 RAG ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 创建 RAG 配置
|
||||
print("1. 创建知识图谱 RAG 配置...")
|
||||
rag = manager.create_kg_rag(
|
||||
tenant_id="tenant_001",
|
||||
project_id="project_001",
|
||||
name="项目知识问答",
|
||||
description="基于项目知识图谱的智能问答",
|
||||
kg_config={
|
||||
"entity_types": ["PERSON", "ORG", "PROJECT", "TECH"],
|
||||
"relation_types": ["works_with", "belongs_to", "depends_on"]
|
||||
},
|
||||
retrieval_config={
|
||||
"top_k": 5,
|
||||
"similarity_threshold": 0.7,
|
||||
"expand_relations": True
|
||||
},
|
||||
generation_config={
|
||||
"temperature": 0.3,
|
||||
"max_tokens": 1000,
|
||||
"include_sources": True
|
||||
}
|
||||
)
|
||||
print(f" 创建成功: {rag.id}")
|
||||
|
||||
# 列出 RAG 配置
|
||||
print("2. 列出 RAG 配置...")
|
||||
rags = manager.list_kg_rags(tenant_id="tenant_001")
|
||||
print(f" 找到 {len(rags)} 个配置")
|
||||
|
||||
return rag.id
|
||||
|
||||
|
||||
async def test_kg_rag_query(rag_id: str):
|
||||
"""测试 RAG 查询"""
|
||||
print("\n=== 测试知识图谱 RAG 查询 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 模拟项目实体和关系
|
||||
project_entities = [
|
||||
{"id": "e1", "name": "张三", "type": "PERSON", "definition": "项目经理"},
|
||||
{"id": "e2", "name": "李四", "type": "PERSON", "definition": "技术负责人"},
|
||||
{"id": "e3", "name": "Project Alpha", "type": "PROJECT", "definition": "核心产品项目"},
|
||||
{"id": "e4", "name": "Kubernetes", "type": "TECH", "definition": "容器编排平台"},
|
||||
{"id": "e5", "name": "TechCorp", "type": "ORG", "definition": "科技公司"}
|
||||
]
|
||||
|
||||
project_relations = [
|
||||
{"source_entity_id": "e1", "target_entity_id": "e3", "source_name": "张三", "target_name": "Project Alpha", "relation_type": "works_with", "evidence": "张三负责 Project Alpha 的管理工作"},
|
||||
{"source_entity_id": "e2", "target_entity_id": "e3", "source_name": "李四", "target_name": "Project Alpha", "relation_type": "works_with", "evidence": "李四负责 Project Alpha 的技术架构"},
|
||||
{"source_entity_id": "e3", "target_entity_id": "e4", "source_name": "Project Alpha", "target_name": "Kubernetes", "relation_type": "depends_on", "evidence": "项目使用 Kubernetes 进行部署"},
|
||||
{"source_entity_id": "e1", "target_entity_id": "e5", "source_name": "张三", "target_name": "TechCorp", "relation_type": "belongs_to", "evidence": "张三是 TechCorp 的员工"}
|
||||
]
|
||||
|
||||
# 执行查询
|
||||
print("1. 执行 RAG 查询...")
|
||||
query_text = "Project Alpha 项目有哪些人参与?使用了什么技术?"
|
||||
|
||||
try:
|
||||
result = await manager.query_kg_rag(
|
||||
rag_id=rag_id,
|
||||
query=query_text,
|
||||
project_entities=project_entities,
|
||||
project_relations=project_relations
|
||||
)
|
||||
|
||||
print(f" 查询: {result.query}")
|
||||
print(f" 回答: {result.answer[:200]}...")
|
||||
print(f" 置信度: {result.confidence}")
|
||||
print(f" 来源: {len(result.sources)} 个实体")
|
||||
print(f" 延迟: {result.latency_ms}ms")
|
||||
except Exception as e:
|
||||
print(f" 查询失败: {e}")
|
||||
|
||||
|
||||
async def test_smart_summary():
|
||||
"""测试智能摘要"""
|
||||
print("\n=== 测试智能摘要 ===")
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
# 模拟转录文本
|
||||
transcript_text = """
|
||||
今天的会议主要讨论了 Project Alpha 的进展情况。张三作为项目经理,
|
||||
汇报了当前的项目进度,表示已经完成了 80% 的开发工作。李四提出了
|
||||
一些关于 Kubernetes 部署的问题,建议我们采用新的部署策略。
|
||||
会议还讨论了下一步的工作计划,包括测试、文档编写和上线准备。
|
||||
大家一致认为项目进展顺利,预计可以按时交付。
|
||||
"""
|
||||
|
||||
content_data = {
|
||||
"text": transcript_text,
|
||||
"entities": [
|
||||
{"name": "张三", "type": "PERSON"},
|
||||
{"name": "李四", "type": "PERSON"},
|
||||
{"name": "Project Alpha", "type": "PROJECT"},
|
||||
{"name": "Kubernetes", "type": "TECH"}
|
||||
]
|
||||
}
|
||||
|
||||
# 生成不同类型的摘要
|
||||
summary_types = ["extractive", "abstractive", "key_points"]
|
||||
|
||||
for summary_type in summary_types:
|
||||
print(f"1. 生成 {summary_type} 类型摘要...")
|
||||
try:
|
||||
summary = await manager.generate_smart_summary(
|
||||
tenant_id="tenant_001",
|
||||
project_id="project_001",
|
||||
source_type="transcript",
|
||||
source_id="transcript_001",
|
||||
summary_type=summary_type,
|
||||
content_data=content_data
|
||||
)
|
||||
|
||||
print(f" 摘要类型: {summary.summary_type}")
|
||||
print(f" 内容: {summary.content[:150]}...")
|
||||
print(f" 关键要点: {summary.key_points[:3]}")
|
||||
print(f" 置信度: {summary.confidence}")
|
||||
except Exception as e:
|
||||
print(f" 生成失败: {e}")
|
||||
|
||||
|
||||
async def main():
|
||||
"""主测试函数"""
|
||||
print("=" * 60)
|
||||
print("InsightFlow Phase 8 Task 4 - AI 能力增强测试")
|
||||
print("=" * 60)
|
||||
|
||||
try:
|
||||
# 测试自定义模型
|
||||
model_id = test_custom_model()
|
||||
|
||||
# 测试训练和预测
|
||||
await test_train_and_predict(model_id)
|
||||
|
||||
# 测试预测模型
|
||||
trend_model_id, anomaly_model_id = test_prediction_models()
|
||||
|
||||
# 测试预测功能
|
||||
await test_predictions(trend_model_id, anomaly_model_id)
|
||||
|
||||
# 测试知识图谱 RAG
|
||||
rag_id = test_kg_rag()
|
||||
|
||||
# 测试 RAG 查询
|
||||
await test_kg_rag_query(rag_id)
|
||||
|
||||
# 测试智能摘要
|
||||
await test_smart_summary()
|
||||
|
||||
print("\n" + "=" * 60)
|
||||
print("所有测试完成!")
|
||||
print("=" * 60)
|
||||
|
||||
except Exception as e:
|
||||
print(f"\n测试失败: {e}")
|
||||
import traceback
|
||||
traceback.print_exc()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user