feat(dc/tool-c): 完成AI代码生成服务(Day 3 MVP)

核心功能:
- 新增AICodeService(550行):AI代码生成核心服务
- 新增AIController(257行):4个API端点
- 新增dc_tool_c_ai_history表:存储对话历史
- 实现自我修正机制:最多3次智能重试
- 集成LLMFactory:复用通用能力层
- 10个Few-shot示例:覆盖Level 1-4场景

技术优化:
- 修复NaN序列化问题(Python端转None)
- 修复数据传递问题(从Session获取真实数据)
- 优化System Prompt(明确环境信息)
- 调整Few-shot示例(移除import语句)

测试结果:
- 通过率:9/11(81.8%) 达到MVP标准
- 成功场景:缺失值处理、编码、分箱、BMI、筛选、填补、统计、分类
- 待优化:数值清洗、智能去重(已记录技术债务TD-C-006)

API端点:
- POST /api/v1/dc/tool-c/ai/generate(生成代码)
- POST /api/v1/dc/tool-c/ai/execute(执行代码)
- POST /api/v1/dc/tool-c/ai/process(生成并执行,一步到位)
- GET /api/v1/dc/tool-c/ai/history/:sessionId(对话历史)

文档更新:
- 新增Day 3开发完成总结(770行)
- 新增复杂场景优化技术债务(TD-C-006)
- 更新工具C当前状态文档
- 更新技术债务清单

影响范围:
- backend/src/modules/dc/tool-c/*(新增2个文件,更新1个文件)
- backend/scripts/create-tool-c-ai-history-table.mjs(新增)
- backend/prisma/schema.prisma(新增DcToolCAiHistory模型)
- extraction_service/services/dc_executor.py(NaN序列化修复)
- docs/03-业务模块/DC-数据清洗整理/*(5份文档更新)

Breaking Changes: 无

总代码行数:+950行

Refs: #Tool-C-Day3
This commit is contained in:
2025-12-07 16:21:32 +08:00
parent 2348234013
commit f01981bf78
68 changed files with 6257 additions and 17 deletions

View File

@@ -0,0 +1,63 @@
"""快速测试DC API"""
import requests
import json
print("=" * 60)
print("DC工具C - Python微服务快速测试")
print("=" * 60)
# 测试1: 代码验证(正常代码)
print("\n【测试1】代码验证 - 正常代码")
try:
r = requests.post("http://localhost:8000/api/dc/validate", json={"code": "df['x'] = 1"}, timeout=5)
print(f" 状态码: {r.status_code}")
if r.status_code == 200:
result = r.json()
print(f" valid={result['valid']}, errors={result['errors']}, warnings={result['warnings']}")
print(f" ✅ 测试1通过")
else:
print(f" ❌ 测试1失败: {r.text}")
except Exception as e:
print(f" ❌ 测试1异常: {e}")
# 测试2: 代码验证(危险代码)
print("\n【测试2】代码验证 - 危险代码(应被拦截)")
try:
r = requests.post("http://localhost:8000/api/dc/validate", json={"code": "import os"}, timeout=5)
print(f" 状态码: {r.status_code}")
if r.status_code == 200:
result = r.json()
print(f" valid={result['valid']}, errors数量={len(result.get('errors',[]))}")
if not result['valid'] and len(result.get('errors',[])) > 0:
print(f" ✅ 测试2通过危险代码被拦截")
else:
print(f" ❌ 测试2失败危险代码未被拦截")
else:
print(f" ❌ 测试2失败: {r.text}")
except Exception as e:
print(f" ❌ 测试2异常: {e}")
# 测试3: 代码执行
print("\n【测试3】代码执行 - 简单Pandas操作")
try:
data = [{"age": 25}, {"age": 65}, {"age": 45}]
code = "df['old'] = df['age'] > 60"
r = requests.post("http://localhost:8000/api/dc/execute", json={"data": data, "code": code}, timeout=10)
print(f" 状态码: {r.status_code}")
if r.status_code == 200:
result = r.json()
print(f" success={result.get('success')}, 执行时间={result.get('execution_time',0):.3f}")
if result.get('success'):
print(f" 结果数据: {result['result_data']}")
print(f" ✅ 测试3通过代码成功执行")
else:
print(f" ❌ 测试3失败: {result.get('error')}")
else:
print(f" ❌ 测试3失败: {r.text}")
except Exception as e:
print(f" ❌ 测试3异常: {e}")
print("\n" + "=" * 60)
print("🎉 Day 1 Python服务测试完成")
print("=" * 60)