fix: auto-fix code issues (cron)
- 修复重复导入/字段 - 修复异常处理 - 修复PEP8格式问题 - 添加类型注解 - 修复重复函数定义 (health_check, create_webhook_endpoint, etc) - 修复未定义名称 (SearchOperator, TenantTier, Query, Body, logger) - 修复 workflow_manager.py 的类定义重复问题 - 添加缺失的导入
This commit is contained in:
@@ -4,6 +4,9 @@ InsightFlow Phase 8 Task 4 测试脚本
|
||||
测试 AI 能力增强功能
|
||||
"""
|
||||
|
||||
from ai_manager import (
|
||||
get_ai_manager, ModelType, PredictionType
|
||||
)
|
||||
import asyncio
|
||||
import sys
|
||||
import os
|
||||
@@ -11,19 +14,13 @@ 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(
|
||||
@@ -43,7 +40,7 @@ def test_custom_model():
|
||||
created_by="user_001"
|
||||
)
|
||||
print(f" 创建成功: {model.id}, 状态: {model.status.value}")
|
||||
|
||||
|
||||
# 2. 添加训练样本
|
||||
print("2. 添加训练样本...")
|
||||
samples = [
|
||||
@@ -72,7 +69,7 @@ def test_custom_model():
|
||||
]
|
||||
}
|
||||
]
|
||||
|
||||
|
||||
for sample_data in samples:
|
||||
sample = manager.add_training_sample(
|
||||
model_id=model.id,
|
||||
@@ -81,28 +78,28 @@ def test_custom_model():
|
||||
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:
|
||||
@@ -112,7 +109,7 @@ async def test_train_and_predict(model_id: str):
|
||||
except Exception as e:
|
||||
print(f" 训练失败: {e}")
|
||||
return
|
||||
|
||||
|
||||
# 2. 使用模型预测
|
||||
print("2. 使用模型预测...")
|
||||
test_text = "赵六患有糖尿病,正在使用胰岛素治疗。"
|
||||
@@ -127,9 +124,9 @@ async def test_train_and_predict(model_id: str):
|
||||
def test_prediction_models():
|
||||
"""测试预测模型"""
|
||||
print("\n=== 测试预测模型 ===")
|
||||
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
|
||||
# 1. 创建趋势预测模型
|
||||
print("1. 创建趋势预测模型...")
|
||||
trend_model = manager.create_prediction_model(
|
||||
@@ -145,7 +142,7 @@ def test_prediction_models():
|
||||
}
|
||||
)
|
||||
print(f" 创建成功: {trend_model.id}")
|
||||
|
||||
|
||||
# 2. 创建异常检测模型
|
||||
print("2. 创建异常检测模型...")
|
||||
anomaly_model = manager.create_prediction_model(
|
||||
@@ -161,23 +158,23 @@ def test_prediction_models():
|
||||
}
|
||||
)
|
||||
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 = [
|
||||
@@ -191,7 +188,7 @@ async def test_predictions(trend_model_id: str, anomaly_model_id: str):
|
||||
]
|
||||
trained = await manager.train_prediction_model(trend_model_id, historical_data)
|
||||
print(f" 训练完成,准确率: {trained.accuracy}")
|
||||
|
||||
|
||||
# 2. 趋势预测
|
||||
print("2. 趋势预测...")
|
||||
trend_result = await manager.predict(
|
||||
@@ -199,7 +196,7 @@ async def test_predictions(trend_model_id: str, anomaly_model_id: str):
|
||||
{"historical_values": [10, 12, 15, 14, 18, 20, 22]}
|
||||
)
|
||||
print(f" 预测结果: {trend_result.prediction_data}")
|
||||
|
||||
|
||||
# 3. 异常检测
|
||||
print("3. 异常检测...")
|
||||
anomaly_result = await manager.predict(
|
||||
@@ -215,9 +212,9 @@ async def test_predictions(trend_model_id: str, anomaly_model_id: str):
|
||||
def test_kg_rag():
|
||||
"""测试知识图谱 RAG"""
|
||||
print("\n=== 测试知识图谱 RAG ===")
|
||||
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
|
||||
# 创建 RAG 配置
|
||||
print("1. 创建知识图谱 RAG 配置...")
|
||||
rag = manager.create_kg_rag(
|
||||
@@ -241,21 +238,21 @@ def test_kg_rag():
|
||||
}
|
||||
)
|
||||
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": "项目经理"},
|
||||
@@ -264,18 +261,36 @@ async def test_kg_rag_query(rag_id: str):
|
||||
{"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 的员工"}
|
||||
]
|
||||
|
||||
|
||||
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,
|
||||
@@ -283,7 +298,7 @@ async def test_kg_rag_query(rag_id: str):
|
||||
project_entities=project_entities,
|
||||
project_relations=project_relations
|
||||
)
|
||||
|
||||
|
||||
print(f" 查询: {result.query}")
|
||||
print(f" 回答: {result.answer[:200]}...")
|
||||
print(f" 置信度: {result.confidence}")
|
||||
@@ -296,9 +311,9 @@ async def test_kg_rag_query(rag_id: str):
|
||||
async def test_smart_summary():
|
||||
"""测试智能摘要"""
|
||||
print("\n=== 测试智能摘要 ===")
|
||||
|
||||
|
||||
manager = get_ai_manager()
|
||||
|
||||
|
||||
# 模拟转录文本
|
||||
transcript_text = """
|
||||
今天的会议主要讨论了 Project Alpha 的进展情况。张三作为项目经理,
|
||||
@@ -307,7 +322,7 @@ async def test_smart_summary():
|
||||
会议还讨论了下一步的工作计划,包括测试、文档编写和上线准备。
|
||||
大家一致认为项目进展顺利,预计可以按时交付。
|
||||
"""
|
||||
|
||||
|
||||
content_data = {
|
||||
"text": transcript_text,
|
||||
"entities": [
|
||||
@@ -317,10 +332,10 @@ async def test_smart_summary():
|
||||
{"name": "Kubernetes", "type": "TECH"}
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
# 生成不同类型的摘要
|
||||
summary_types = ["extractive", "abstractive", "key_points"]
|
||||
|
||||
|
||||
for summary_type in summary_types:
|
||||
print(f"1. 生成 {summary_type} 类型摘要...")
|
||||
try:
|
||||
@@ -332,7 +347,7 @@ async def test_smart_summary():
|
||||
summary_type=summary_type,
|
||||
content_data=content_data
|
||||
)
|
||||
|
||||
|
||||
print(f" 摘要类型: {summary.summary_type}")
|
||||
print(f" 内容: {summary.content[:150]}...")
|
||||
print(f" 关键要点: {summary.key_points[:3]}")
|
||||
@@ -346,33 +361,33 @@ 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
|
||||
|
||||
Reference in New Issue
Block a user