Files
AIclinicalresearch/backend/scripts/test-json-parser.ts
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

140 lines
3.3 KiB
TypeScript
Raw Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 测试JSON解析器的修复效果
*
* 测试目的:验证中文引号等格式问题是否能被正确处理
*/
import { parseJSON } from '../src/common/utils/jsonParser.js';
console.log('\n🧪 JSON解析器修复测试\n');
// 测试用例
const testCases = [
{
name: '正常JSONASCII引号',
input: '{"conclusion": "exclude", "confidence": 0.95}',
expectSuccess: true
},
{
name: '中文引号JSON',
input: '{"conclusion": "exclude", "confidence": 0.95}',
expectSuccess: true
},
{
name: '混合引号JSON',
input: '{"conclusion": "exclude", "confidence": 0.95}',
expectSuccess: true
},
{
name: 'JSON代码块中文引号',
input: `\`\`\`json
{
"judgment": {
"P": "match",
"I": "match"
},
"conclusion": "include",
"confidence": 0.85,
"reason": "虽然对照组不是安慰剂,但研究质量高"
}
\`\`\``,
expectSuccess: true
},
{
name: '带额外文字的JSON',
input: `这是筛选结果:
\`\`\`json
{"conclusion": "exclude", "confidence": 0.90}
\`\`\`
以上是我的判断。`,
expectSuccess: true
},
{
name: '全角逗号和冒号',
input: '{"conclusion""exclude""confidence"0.95}',
expectSuccess: true
},
{
name: '不完整的JSON应失败',
input: '{"conclusion": "exclude", "confidence":',
expectSuccess: false
},
{
name: '非JSON文本应失败',
input: 'This is not a JSON string at all.',
expectSuccess: false
},
{
name: '复杂嵌套JSON中文引号',
input: `{
"judgment": {
"P": "match",
"I": "partial",
"C": "mismatch",
"S": "match"
},
"evidence": {
"P": "研究对象为急性缺血性卒中患者",
"I": "干预措施为替格瑞洛",
"C": "对照组为氯吡格雷而非安慰剂",
"S": "随机对照试验"
},
"conclusion": "exclude",
"confidence": 0.92,
"reason": "虽然P、I、S维度匹配但对照组不符合要求"
}`,
expectSuccess: true
}
];
// 运行测试
let passed = 0;
let failed = 0;
testCases.forEach((testCase, index) => {
console.log(`[测试 ${index + 1}/${testCases.length}] ${testCase.name}`);
const result = parseJSON(testCase.input);
const success = result.success === testCase.expectSuccess;
if (success) {
console.log(' ✅ 通过');
if (result.success) {
console.log(` 📄 解析结果: ${JSON.stringify(result.data).substring(0, 100)}...`);
}
passed++;
} else {
console.log(' ❌ 失败');
console.log(` 期望: ${testCase.expectSuccess ? '成功' : '失败'}`);
console.log(` 实际: ${result.success ? '成功' : '失败'}`);
if (!result.success) {
console.log(` 错误: ${result.error}`);
}
failed++;
}
console.log('');
});
// 总结
console.log('='.repeat(60));
console.log('📊 测试总结\n');
console.log(`✅ 通过: ${passed}/${testCases.length}`);
console.log(`❌ 失败: ${failed}/${testCases.length}`);
console.log(`📈 成功率: ${(passed / testCases.length * 100).toFixed(1)}%`);
if (passed === testCases.length) {
console.log('\n🎉 所有测试通过JSON解析器修复成功');
} else {
console.log('\n⚠ 部分测试失败,需要进一步调试。');
}
console.log('='.repeat(60) + '\n');