76 lines
3.8 KiB
PHP
76 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
require dirname(__DIR__) . '/vendor/autoload.php';
|
|
|
|
use app\common\service\ClinicalKnowledgeReference;
|
|
use app\common\service\NihaixiaClinicalSkill;
|
|
use app\common\service\TcmAncientBooksReference;
|
|
|
|
function ancientExpect(bool $condition, string $message): void
|
|
{
|
|
if (!$condition) {
|
|
fwrite(STDERR, "FAIL: {$message}\n");
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
$indexProperty = new ReflectionProperty(TcmAncientBooksReference::class, 'index');
|
|
$indexProperty->setValue(null, [
|
|
'commit' => TcmAncientBooksReference::COMMIT,
|
|
'index_version' => TcmAncientBooksReference::INDEX_VERSION,
|
|
'terms' => [
|
|
'消渴' => [[
|
|
'title' => '测试古籍', 'chapter' => '消渴篇',
|
|
'path' => '123-测试古籍.txt', 'line' => 42,
|
|
'excerpt' => '原文中谈到消渴的历史记载。', 'score' => 15,
|
|
]],
|
|
],
|
|
'chapter_terms' => [
|
|
'痛风' => [[
|
|
'title' => '测试古籍二', 'chapter' => '痛风',
|
|
'path' => '124-测试古籍二.txt', 'line' => 88,
|
|
'excerpt' => '痛风相关的原文。', 'score' => 12,
|
|
]],
|
|
],
|
|
]);
|
|
|
|
$query = '患者糖尿病、高血糖,请分析病症。';
|
|
$sources = TcmAncientBooksReference::sourcesForText($query);
|
|
ancientExpect(count($sources) === 1, 'matching patient symptoms select one excerpt');
|
|
ancientExpect($sources[0]['title'] === '测试古籍' && $sources[0]['chapter'] === '消渴篇', 'book and chapter retained');
|
|
ancientExpect($sources[0]['url'] === '' && $sources[0]['path'] === '123-测试古籍.txt' && $sources[0]['line'] === 42,
|
|
'local citation retains file and line without an external URL');
|
|
ancientExpect($sources[0]['excerpt'] === '原文中谈到消渴的历史记载。', 'local citation retains verifiable original text');
|
|
ancientExpect(TcmAncientBooksReference::sourcesForText('请分析耳鸣。') === [], 'unmatched symptoms have no invented citation');
|
|
ancientExpect(TcmAncientBooksReference::sourcesForText('患者出现痛风症状。')[0]['line'] === 88, 'unlisted diseases can match indexed chapter headings');
|
|
|
|
$augmented = ClinicalKnowledgeReference::augmentQuery($query);
|
|
ancientExpect(str_contains($augmented, TcmAncientBooksReference::COMMIT), 'query includes ancient-book version');
|
|
ancientExpect(str_contains($augmented, NihaixiaClinicalSkill::VERSION), 'existing clinical skill remains');
|
|
ancientExpect(str_ends_with($augmented, $query), 'original patient question remains intact');
|
|
ancientExpect(ClinicalKnowledgeReference::augmentQuery($augmented) === $augmented, 'retry does not duplicate references');
|
|
|
|
foreach ([$query, '患者出现痛风症状。', '患者有未收录的症状。', '阶段=text 患者糖尿病', '阶段=final 患者糖尿病'] as $case) {
|
|
$selected = ClinicalKnowledgeReference::ancientBookSources($case);
|
|
ancientExpect(
|
|
ClinicalKnowledgeReference::augmentQueryWithAncientBookSources($case, $selected)
|
|
=== ClinicalKnowledgeReference::augmentQuery($case),
|
|
'selected sources preserve the exact legacy prompt for each clinical case'
|
|
);
|
|
}
|
|
ancientExpect(
|
|
ClinicalKnowledgeReference::augmentQueryWithAncientBookSources('患者有未收录的症状。', [])
|
|
=== ClinicalKnowledgeReference::augmentQuery('患者有未收录的症状。'),
|
|
'an unrelated request receives no preceding patient citations'
|
|
);
|
|
|
|
$messages = [['role' => 'user', 'content' => $query]];
|
|
$wire = ClinicalKnowledgeReference::augmentMessages($messages);
|
|
ancientExpect(count($wire) === 2 && str_contains($wire[0]['content'], '测试古籍'), 'chat receives reference in system message');
|
|
ancientExpect(ClinicalKnowledgeReference::augmentMessages($wire) === $wire, 'stream retry does not duplicate reference');
|
|
|
|
$indexProperty->setValue(null, null);
|
|
echo "TCM ancient-books reference contract passed.\n";
|