更新
This commit is contained in:
@@ -10,6 +10,7 @@ use app\common\logic\BaseLogic;
|
||||
use app\common\model\tcm\Diagnosis;
|
||||
use app\common\model\tcm\DiagnosisAiReport;
|
||||
use app\common\service\DifyChatService;
|
||||
use app\common\service\NihaixiaClinicalSkill;
|
||||
use think\facade\Db;
|
||||
use think\facade\Log;
|
||||
|
||||
@@ -21,11 +22,11 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
/** @var string Safe machine-readable code for the current assistant request. */
|
||||
private static $assistantErrorCode = 'AI_ASSISTANT_FAILED';
|
||||
|
||||
private const PROMPT_VERSION = 'patient-context-case-explain-nihaixia-v3';
|
||||
private const PROMPT_VERSION = 'patient-context-case-explain-nihaixia-ancient-v4';
|
||||
|
||||
private const ASSISTANT_PROMPT_VERSION = 'patient-context-assistant-nihaixia-v3';
|
||||
private const ASSISTANT_PROMPT_VERSION = 'patient-context-assistant-nihaixia-ancient-v4';
|
||||
|
||||
private const ANALYSIS_PROMPT_VERSION = 'patient-context-analysis-nihaixia-v4';
|
||||
private const ANALYSIS_PROMPT_VERSION = 'patient-context-analysis-nihaixia-ancient-v5';
|
||||
|
||||
private const MAX_ASSISTANT_PROMPT_LENGTH = 500;
|
||||
|
||||
@@ -419,6 +420,7 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
self::ASSISTANT_PROMPT_VERSION
|
||||
),
|
||||
'query' => self::buildAssistantPrompt($context, $task, $prompt),
|
||||
'knowledge_source' => NihaixiaClinicalSkill::knowledgeSource(),
|
||||
'user' => 'admin-diagnosis-assistant-' . $adminId,
|
||||
'admin_id' => $adminId,
|
||||
'files' => is_array($context['files'] ?? null) ? $context['files'] : [],
|
||||
@@ -559,6 +561,8 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
$payload = [
|
||||
'diagnosis_id' => (int) ($prepared['diagnosis_id'] ?? 0),
|
||||
'answer' => $content,
|
||||
'knowledge_source' => NihaixiaClinicalSkill::knowledgeSource(),
|
||||
'ancient_book_sources' => is_array($result['ancient_book_sources'] ?? null) ? $result['ancient_book_sources'] : [],
|
||||
'model_key' => (string) ($prepared['profile'] ?? ''),
|
||||
'model_label' => (string) ($prepared['model_label'] ?? ''),
|
||||
'model_name' => (string) ($prepared['model_name'] ?? ''),
|
||||
@@ -830,6 +834,8 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
}
|
||||
|
||||
return array_merge($analysis, [
|
||||
'knowledge_source' => NihaixiaClinicalSkill::knowledgeSource(),
|
||||
'ancient_book_sources' => is_array($result['ancient_book_sources'] ?? null) ? $result['ancient_book_sources'] : [],
|
||||
'diagnosis_id' => $diagnosisId,
|
||||
'model_key' => $profile,
|
||||
'model_label' => $modelLabel,
|
||||
@@ -950,7 +956,8 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
$modelLabel,
|
||||
$content,
|
||||
(string) ($result['message_id'] ?? ''),
|
||||
$adminId
|
||||
$adminId,
|
||||
is_array($result['ancient_book_sources'] ?? null) ? $result['ancient_book_sources'] : []
|
||||
);
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('diagnosis ai report persist failed', [
|
||||
@@ -1047,7 +1054,7 @@ class DiagnosisAiLogic extends BaseLogic
|
||||
$context = self::buildCaseContext($diagnosis, $adminId, $adminInfo);
|
||||
return [
|
||||
'diagnosis_id' => $id,
|
||||
'report' => self::formatReportRow($report->toArray(), $context['fingerprint']),
|
||||
'report' => self::formatReportRow($report->toArray(), $context['fingerprint'], $context),
|
||||
'can_edit' => self::hasPermission($adminId, $adminInfo, self::PERMISSION_EDIT),
|
||||
'can_refresh' => self::hasPermission($adminId, $adminInfo, self::PERMISSION_REFRESH),
|
||||
];
|
||||
@@ -1612,7 +1619,8 @@ PROMPT;
|
||||
string $modelLabel,
|
||||
string $content,
|
||||
string $messageId,
|
||||
int $adminId
|
||||
int $adminId,
|
||||
array $ancientBookSources = []
|
||||
): int {
|
||||
$now = time();
|
||||
$row = [
|
||||
@@ -1622,6 +1630,7 @@ PROMPT;
|
||||
'model_label' => self::cleanText($modelLabel, 50),
|
||||
'report_content' => $content,
|
||||
'message_id' => self::cleanText($messageId, 191),
|
||||
'ancient_book_sources_json' => json_encode($ancientBookSources, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) ?: '[]',
|
||||
'prompt_version' => self::PROMPT_VERSION,
|
||||
'case_fingerprint' => (string) $context['fingerprint'],
|
||||
'generated_by' => $adminId,
|
||||
@@ -1637,6 +1646,7 @@ PROMPT;
|
||||
'model_label',
|
||||
'report_content',
|
||||
'message_id',
|
||||
'ancient_book_sources_json',
|
||||
'prompt_version',
|
||||
'case_fingerprint',
|
||||
'generated_by',
|
||||
@@ -1677,7 +1687,8 @@ PROMPT;
|
||||
if (isset($rowsByModel[$modelKey])) {
|
||||
$reports[] = self::formatReportRow(
|
||||
$rowsByModel[$modelKey],
|
||||
(string) $context['fingerprint']
|
||||
(string) $context['fingerprint'],
|
||||
$context
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1710,11 +1721,14 @@ PROMPT;
|
||||
* @param array<string,mixed> $row
|
||||
* @return array<string,mixed>
|
||||
*/
|
||||
private static function formatReportRow(array $row, string $currentFingerprint): array
|
||||
private static function formatReportRow(array $row, string $currentFingerprint, array $context = []): array
|
||||
{
|
||||
$content = (string) ($row['report_content'] ?? '');
|
||||
$generatedTime = (int) ($row['generated_time'] ?? 0);
|
||||
$editedTime = (int) ($row['edited_time'] ?? 0);
|
||||
// The fingerprint includes the source summary and diagnosis IDs. Reuse
|
||||
// current provenance only while it still matches the saved report.
|
||||
$sameSources = hash_equals($currentFingerprint, (string) ($row['case_fingerprint'] ?? ''));
|
||||
|
||||
return [
|
||||
'id' => (int) ($row['id'] ?? 0),
|
||||
@@ -1725,13 +1739,16 @@ PROMPT;
|
||||
'content' => $content,
|
||||
'report' => self::parseReport($content),
|
||||
'message_id' => (string) ($row['message_id'] ?? ''),
|
||||
'knowledge_source' => NihaixiaClinicalSkill::knowledgeSourceForPromptVersion((string) ($row['prompt_version'] ?? '')),
|
||||
'ancient_book_sources' => self::decodeAncientBookSources($row['ancient_book_sources_json'] ?? ''),
|
||||
'prompt_version' => (string) ($row['prompt_version'] ?? ''),
|
||||
'case_fingerprint' => (string) ($row['case_fingerprint'] ?? ''),
|
||||
'prescription_fingerprint' => (string) ($row['case_fingerprint'] ?? ''),
|
||||
'is_stale' => !hash_equals(
|
||||
$currentFingerprint,
|
||||
(string) ($row['case_fingerprint'] ?? '')
|
||||
),
|
||||
'is_stale' => !$sameSources,
|
||||
'source_summary' => $sameSources && is_array($context['source_summary'] ?? null)
|
||||
? $context['source_summary'] : [],
|
||||
'source_diagnosis_ids' => $sameSources && is_array($context['source_diagnosis_ids'] ?? null)
|
||||
? array_values(array_map('intval', $context['source_diagnosis_ids'])) : [],
|
||||
'generated_by' => (int) ($row['generated_by'] ?? 0),
|
||||
'generated_time' => $generatedTime,
|
||||
'generated_at' => $generatedTime > 0 ? date('Y-m-d H:i:s', $generatedTime) : '',
|
||||
@@ -1742,6 +1759,13 @@ PROMPT;
|
||||
];
|
||||
}
|
||||
|
||||
/** @return list<array<string,mixed>> */
|
||||
private static function decodeAncientBookSources(mixed $value): array
|
||||
{
|
||||
$decoded = is_string($value) ? json_decode($value, true) : null;
|
||||
return is_array($decoded) && array_is_list($decoded) ? array_values(array_filter($decoded, 'is_array')) : [];
|
||||
}
|
||||
|
||||
/** @return array<string,array<string,mixed>> */
|
||||
private static function modelConfigs(): array
|
||||
{
|
||||
@@ -1779,8 +1803,24 @@ PROMPT;
|
||||
return null;
|
||||
}
|
||||
|
||||
// Qwen occasionally drops letters from this key while keeping the other
|
||||
// two contract keys intact. Accept only the two observed spellings, and
|
||||
// never choose between conflicting diagnosis fields.
|
||||
if (!array_key_exists('diagnosis_advice', $decoded)) {
|
||||
$aliases = array_intersect(['diagnosis_adive', 'diagnosis_ad'], array_keys($decoded));
|
||||
if (count($aliases) !== 1) {
|
||||
return null;
|
||||
}
|
||||
$diagnosisField = $decoded[reset($aliases)];
|
||||
} else {
|
||||
if (array_key_exists('diagnosis_adive', $decoded) || array_key_exists('diagnosis_ad', $decoded)) {
|
||||
return null;
|
||||
}
|
||||
$diagnosisField = $decoded['diagnosis_advice'];
|
||||
}
|
||||
|
||||
$diagnosisAdvice = self::validatedAnalysisText(
|
||||
$decoded['diagnosis_advice'] ?? null,
|
||||
$diagnosisField,
|
||||
self::MAX_ANALYSIS_ADVICE_LENGTH,
|
||||
true
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user