This commit is contained in:
Your Name
2026-10-08 18:03:11 +08:00
parent 4dac39c4c2
commit 19ea2dadeb
26 changed files with 1131 additions and 28 deletions
@@ -11,6 +11,7 @@ use app\common\model\auth\AdminDept;
use app\common\model\dept\Dept;
use app\common\model\tcm\PatientAiReport;
use app\common\service\DifyChatService;
use app\common\service\NihaixiaClinicalSkill;
use app\common\service\FileService;
use think\facade\Db;
use think\facade\Log;
@@ -41,7 +42,7 @@ class PatientAiReportLogic extends BaseLogic
private const PERMISSION_GENERATE = 'tcm.diagnosis/generatepatientaireport';
private const PROMPT_VERSION = 'patient-longitudinal-report-nihaixia-v3';
private const PROMPT_VERSION = 'patient-longitudinal-report-nihaixia-ancient-v4';
/** @var array<int,string> */
private const MODEL_KEYS = ['qwen', 'openai'];
@@ -246,6 +247,8 @@ class PatientAiReportLogic extends BaseLogic
'source_snapshot' => $sourceJson,
'source_summary_json' => self::encodeJson($sourceSummary),
'source_diagnosis_ids_json' => self::encodeJson($diagnosisIds),
'ancient_book_sources_json' => self::encodeJson(is_array($upstream['ancient_book_sources'] ?? null)
? $upstream['ancient_book_sources'] : []),
'source_hash' => $sourceHash,
'prompt_version' => self::PROMPT_VERSION,
'message_id' => self::cleanText($upstream['message_id'] ?? '', 191),
@@ -276,6 +279,8 @@ class PatientAiReportLogic extends BaseLogic
'report' => $generatedReport,
'disclaimer' => self::DISCLAIMER,
'source_summary' => $generatedReport['source_summary'] ?? self::emptySourceSummary(),
'source_diagnosis_ids' => $generatedReport['source_diagnosis_ids'] ?? [],
'knowledge_source' => $generatedReport['knowledge_source'] ?? null,
];
}
@@ -988,6 +993,7 @@ class PatientAiReportLogic extends BaseLogic
$chunkCount = count($chunks);
$inputs = self::sourceInputsForUpstream($snapshot['source_summary'] ?? []);
$files = self::collectUpstreamFiles($snapshot);
$ancientBookSources = [];
if ($chunkCount === 1) {
$result = DifyChatService::chat(
@@ -1025,6 +1031,7 @@ class PatientAiReportLogic extends BaseLogic
if (empty($part['ok']) || trim((string) ($part['content'] ?? '')) === '') {
throw new \RuntimeException('Patient evidence chunk analysis failed');
}
$ancientBookSources = self::mergeAncientBookSources($ancientBookSources, $part);
$summaries[] = [
'part' => $index + 1,
'total' => $chunkCount,
@@ -1057,6 +1064,7 @@ class PatientAiReportLogic extends BaseLogic
if (empty($part['ok']) || trim((string) ($part['content'] ?? '')) === '') {
throw new \RuntimeException('Patient evidence reduction failed');
}
$ancientBookSources = self::mergeAncientBookSources($ancientBookSources, $part);
$reduced[] = [
'part' => $index + 1,
'total' => count($summaryChunks),
@@ -1084,11 +1092,39 @@ class PatientAiReportLogic extends BaseLogic
'patient-longitudinal-report'
);
$result['analysis_chunk_count'] = $chunkCount;
$result['ancient_book_sources'] = self::mergeAncientBookSources($ancientBookSources, $result);
$result['analysis_reduction_rounds'] = $reductionRounds;
$result['analyzed_source_bytes'] = strlen($sourceJson);
return $result;
}
/** @param list<array<string,mixed>> $accumulated
* @param array<string,mixed> $response
* @return list<array<string,mixed>>
*/
private static function mergeAncientBookSources(array $accumulated, array $response): array
{
$sources = $response['ancient_book_sources'] ?? [];
if (!is_array($sources)) {
return $accumulated;
}
$seen = [];
foreach ($accumulated as $source) {
$seen[(string) ($source['url'] ?? '')] = true;
}
foreach ($sources as $source) {
if (!is_array($source) || !is_string($source['url'] ?? null) || isset($seen[$source['url']])) {
continue;
}
$seen[$source['url']] = true;
$accumulated[] = $source;
if (count($accumulated) >= 12) {
break;
}
}
return $accumulated;
}
/** @param array<string,mixed> $snapshot */
private static function buildPrompt(array $snapshot): string
{
@@ -1427,6 +1463,8 @@ class PatientAiReportLogic extends BaseLogic
'report' => $generatedReport,
'disclaimer' => self::DISCLAIMER,
'source_summary' => is_array($newest) ? ($newest['source_summary'] ?? self::emptySourceSummary()) : self::emptySourceSummary(),
'source_diagnosis_ids' => is_array($newest) ? ($newest['source_diagnosis_ids'] ?? []) : [],
'knowledge_source' => is_array($newest) ? ($newest['knowledge_source'] ?? null) : null,
];
}
@@ -1465,6 +1503,9 @@ class PatientAiReportLogic extends BaseLogic
'disclaimer' => self::DISCLAIMER,
'source_hash' => (string) ($row['source_hash'] ?? ''),
'source_summary' => $sourceSummary !== [] ? $sourceSummary : self::emptySourceSummary(),
'source_diagnosis_ids' => self::decodeJsonArray($row['source_diagnosis_ids_json'] ?? ''),
'knowledge_source' => NihaixiaClinicalSkill::knowledgeSourceForPromptVersion((string) ($row['prompt_version'] ?? '')),
'ancient_book_sources' => self::decodeJsonArray($row['ancient_book_sources_json'] ?? ''),
'prompt_version' => (string) ($row['prompt_version'] ?? ''),
'version' => max((int) ($row['version'] ?? 1), 1),
'generated_time' => $generatedTime,