Files
AIclinicalresearch/docs/03-业务模块/ASL-AI智能文献/05-开发记录/2025-11-21-字段映射问题修复.md
HaHafeng beb7f7f559 feat(asl): Implement full-text screening core LLM service and validation system (Day 1-3)
Core Components:
- PDFStorageService with Dify/OSS adapters
- LLM12FieldsService with Nougat-first + dual-model + 3-layer JSON parsing
- PromptBuilder for dynamic prompt assembly
- MedicalLogicValidator with 5 rules + fault tolerance
- EvidenceChainValidator for citation integrity
- ConflictDetectionService for dual-model comparison

Prompt Engineering:
- System Prompt (6601 chars, Section-Aware strategy)
- User Prompt template (PICOS context injection)
- JSON Schema (12 fields constraints)
- Cochrane standards (not loaded in MVP)

Key Innovations:
- 3-layer JSON parsing (JSON.parse + json-repair + code block extraction)
- Promise.allSettled for dual-model fault tolerance
- safeGetFieldValue for robust field extraction
- Mixed CN/EN token calculation

Integration Tests:
- integration-test.ts (full test)
- quick-test.ts (quick test)
- cached-result-test.ts (fault tolerance test)

Documentation Updates:
- Development record (Day 2-3 summary)
- Quality assurance strategy (full-text screening)
- Development plan (progress update)
- Module status (v1.1 update)
- Technical debt (10 new items)

Test Results:
- JSON parsing success rate: 100%
- Medical logic validation: 5/5 passed
- Dual-model parallel processing: OK
- Cost per PDF: CNY 0.10

Files: 238 changed, 14383 insertions(+), 32 deletions(-)
Docs: docs/03-涓氬姟妯″潡/ASL-AI鏅鸿兘鏂囩尞/05-寮€鍙戣褰?2025-11-22_Day2-Day3_LLM鏈嶅姟涓庨獙璇佺郴缁熷紑鍙?md
2025-11-22 22:21:12 +08:00

6.1 KiB
Raw Blame History

字段映射问题修复报告

日期: 2025-11-21
问题: 真实LLM筛选失败成功0/20
原因: 字段名不匹配
状态: 已修复


🔍 问题诊断

症状

任务状态: completed
进度: 20/20
成功: 0 ❌
筛选结果数: 0

表现

  • 任务瞬间完成1秒
  • 所有文献处理失败
  • 没有保存任何筛选结果

🎯 根本原因

问题1: PICOS字段名不匹配

前端/数据库格式 (TitleScreeningSettings.tsx):

picoCriteria: {
  P: '2型糖尿病患者...',
  I: 'SGLT2抑制剂...',
  C: '安慰剂或常规治疗...',
  O: '心血管结局...',
  S: 'RCT'
}

LLM服务期望格式 (llmScreeningService.ts):

// 实际上支持两种格式,但优先使用短格式
picoCriteria: {
  P: '...',  // ✅
  I: '...',  // ✅
  C: '...',  // ✅
  O: '...',  // ✅
  S: '...'   // ✅
}

诊断:前端使用 P/I/C/O/S 格式,但 screeningService.ts 直接传递了数据库的原始格式,未做映射。


问题2: 模型名格式不匹配

前端格式 (TitleScreeningSettings.tsx):

models: ['DeepSeek-V3', 'Qwen-Max']

LLM服务期望格式 (llmScreeningService.ts):

models: ['deepseek-chat', 'qwen-max']

原因前端使用展示名称后端需要API名称。


问题3: 缺少字段验证

文献可能缺少 titleabstract导致LLM调用失败。


修复方案

修复1: 添加PICOS字段映射

文件: backend/src/modules/asl/services/screeningService.ts

// 🔧 修复:字段名映射(数据库格式 → LLM服务格式
const rawPicoCriteria = project.picoCriteria as any;
const picoCriteria = {
  P: rawPicoCriteria?.P || rawPicoCriteria?.population || '',
  I: rawPicoCriteria?.I || rawPicoCriteria?.intervention || '',
  C: rawPicoCriteria?.C || rawPicoCriteria?.comparison || '',
  O: rawPicoCriteria?.O || rawPicoCriteria?.outcome || '',
  S: rawPicoCriteria?.S || rawPicoCriteria?.studyDesign || '',
};

优势

  • 兼容两种格式P/I/C/O/S 或 population/intervention/...
  • 防御性编程避免undefined

修复2: 添加模型名映射

// 🔧 修复:模型名映射(前端格式 → API格式
const MODEL_NAME_MAP: Record<string, string> = {
  'DeepSeek-V3': 'deepseek-chat',
  'Qwen-Max': 'qwen-max',
  'GPT-4o': 'gpt-4o',
  'Claude-4.5': 'claude-sonnet-4.5',
  'deepseek-chat': 'deepseek-chat',  // 兼容直接使用API名
  'qwen-max': 'qwen-max',
  // ... 更多映射
};

const rawModels = screeningConfig?.models || ['deepseek-chat', 'qwen-max'];
const models = rawModels.map((m: string) => MODEL_NAME_MAP[m] || m);

映射表

前端展示名 API名称
DeepSeek-V3 deepseek-chat
Qwen-Max qwen-max
GPT-4o gpt-4o
Claude-4.5 claude-sonnet-4.5

修复3: 添加文献验证

// 🔧 验证:必须有标题和摘要
if (!literature.title || !literature.abstract) {
  logger.warn('Skipping literature without title or abstract', {
    literatureId: literature.id,
    hasTitle: !!literature.title,
    hasAbstract: !!literature.abstract,
  });
  console.log(`⚠️  跳过文献 ${processedCount + 1}: 缺少标题或摘要`);
  processedCount++;
  continue;
}

修复4: 增强调试日志

console.log('\n🚀 开始真实LLM筛选:');
console.log('  任务ID:', taskId);
console.log('  项目ID:', projectId);
console.log('  文献数:', literatures.length);
console.log('  模型(映射后):', models);  // ⭐ 显示映射后的值
console.log('  PICOS-P:', picoCriteria.P?.substring(0, 50) || '(空)');
console.log('  PICOS-I:', picoCriteria.I?.substring(0, 50) || '(空)');
console.log('  PICOS-C:', picoCriteria.C?.substring(0, 50) || '(空)');
console.log('  纳入标准:', inclusionCriteria?.substring(0, 50) || '(空)');
console.log('  排除标准:', exclusionCriteria?.substring(0, 50) || '(空)');

🧪 测试步骤

1. 重启后端(必须!)

# 停止当前后端Ctrl+C
cd D:\MyCursor\AIclinicalresearch\backend
npm run dev

2. 测试(小规模)

  1. 访问前端
  2. 填写PICOS
  3. 上传5篇文献(先测试小规模)
  4. 点击"开始AI初筛"

3. 查看后端控制台

应该看到

🚀 开始真实LLM筛选:
  任务ID: xxx
  文献数: 5
  模型(映射后): [ 'deepseek-chat', 'qwen-max' ]
  PICOS-P: 2型糖尿病患者...
  PICOS-I: SGLT2抑制剂...
  PICOS-C: 安慰剂...
  纳入标准: 成人2型糖尿病...
  排除标准: 综述、系统评价...

[等待10-20秒]

✅ 文献 1/5 处理成功
  DS: include / Qwen: include
  冲突: 否

[等待10-20秒]

✅ 文献 2/5 处理成功
  DS: exclude / Qwen: exclude
  冲突: 否
...

📊 预期效果

修复前

  • ⏱️ 1秒完成20篇
  • 成功0
  • 筛选结果数0

修复后

  • ⏱️ 50-100秒完成5篇每篇10-20秒
  • 成功5
  • 筛选结果数5
  • 证据包含真实的AI分析
  • 证据不包含"模拟证据"

🔧 修改文件

  • backend/src/modules/asl/services/screeningService.ts
    • 添加PICOS字段映射
    • 添加模型名映射
    • 添加文献验证
    • 增强调试日志

💡 经验教训

1. 前后端数据格式一致性

  • 前端使用的展示格式 ≠ 后端API格式
  • 需要在集成层做映射

2. 防御性编程

  • 使用 || 提供默认值
  • 验证必需字段
  • 兼容多种格式

3. 调试日志的重要性

  • 显示映射后的值(不是原始值)
  • 输出所有关键参数
  • 帮助快速定位问题

🎯 后续优化

短期

  1. 字段映射(已完成)
  2. 模型名映射(已完成)
  3. 验证必需字段(已完成)

中期

  1. 统一前后端数据格式(使用 TypeScript 接口)
  2. 添加数据格式验证中间件
  3. 改进错误提示

长期

  1. 使用 tRPC 或 GraphQL 确保类型安全
  2. 自动化测试覆盖
  3. Schema验证

报告人: AI Assistant
日期: 2025-11-21
版本: v1.0.0