This commit is contained in:
Your Name
2026-11-26 15:46:20 +08:00
parent db6d327e91
commit be79e09c83
170 changed files with 21159 additions and 13217 deletions
+44 -16
View File
@@ -15,7 +15,7 @@ use think\facade\Log;
class DifyChatService
{
/** @var array<int,string> */
private const ALLOWED_PROFILES = ['qwen', 'openai'];
private const ALLOWED_PROFILES = ['qwen', 'openai', 'doubao'];
private const MIN_TIMEOUT = 1;
@@ -74,7 +74,7 @@ class DifyChatService
return self::error('INVALID_PROFILE', '不支持的 AI 模型');
}
$baseUrl = trim((string) ($config['base_url'] ?? ''));
$baseUrl = self::profileBaseUrl($config, $modelConfig);
$rawApiKey = (string) ($modelConfig['api_key'] ?? '');
$apiKey = trim($rawApiKey);
if ($baseUrl === '' || $apiKey === '') {
@@ -112,10 +112,10 @@ class DifyChatService
$ancientBookSources = $evidenceOnly || $caseReferences ? [] : ClinicalKnowledgeReference::ancientBookSources($query);
try {
if (!$evidenceOnly) {
$query = $consultationReferences && ($options['response_contract'] ?? '') === 'live_consultation'
? ClinicalKnowledgeReference::augmentLiveConsultationQuery($query, (string) ($options['analysis_scope'] ?? 'authorized_case'))
: ($caseReferences ? ClinicalKnowledgeReference::augmentCaseQuery($query, $consultationReferences)
: ClinicalKnowledgeReference::augmentQueryWithAncientBookSources($query, $ancientBookSources));
$query = $consultationReferences && ($options['response_contract'] ?? '') === 'live_consultation'
? ClinicalKnowledgeReference::augmentLiveConsultationQuery($query, (string) ($options['analysis_scope'] ?? 'authorized_case'))
: ($caseReferences ? ClinicalKnowledgeReference::augmentCaseQuery($query, $consultationReferences)
: ClinicalKnowledgeReference::augmentQueryWithAncientBookSources($query, $ancientBookSources));
}
} catch (\RuntimeException $error) {
return self::error('SKILL_UNAVAILABLE', '临床 AI 参考资料不可用');
@@ -159,7 +159,8 @@ class DifyChatService
$user,
false,
$attempt['files'],
$attempt['omitted']
$attempt['omitted'],
self::openAiPayloadOptions($modelConfig)
);
$lastResponse = null;
$lastSpec = [];
@@ -286,7 +287,7 @@ class DifyChatService
return self::error('INVALID_PROFILE', '不支持的 AI 模型');
}
$baseUrl = trim((string) ($config['base_url'] ?? ''));
$baseUrl = self::profileBaseUrl($config, $modelConfig);
$rawApiKey = (string) ($modelConfig['api_key'] ?? '');
$apiKey = trim($rawApiKey);
if ($baseUrl === '' || $apiKey === '') {
@@ -320,10 +321,10 @@ class DifyChatService
$ancientBookSources = $evidenceOnly || $caseReferences ? [] : ClinicalKnowledgeReference::ancientBookSources($query);
try {
if (!$evidenceOnly) {
$query = $consultationReferences && ($options['response_contract'] ?? '') === 'live_consultation'
? ClinicalKnowledgeReference::augmentLiveConsultationQuery($query, (string) ($options['analysis_scope'] ?? 'authorized_case'))
: ($caseReferences ? ClinicalKnowledgeReference::augmentCaseQuery($query, $consultationReferences)
: ClinicalKnowledgeReference::augmentQueryWithAncientBookSources($query, $ancientBookSources));
$query = $consultationReferences && ($options['response_contract'] ?? '') === 'live_consultation'
? ClinicalKnowledgeReference::augmentLiveConsultationQuery($query, (string) ($options['analysis_scope'] ?? 'authorized_case'))
: ($caseReferences ? ClinicalKnowledgeReference::augmentCaseQuery($query, $consultationReferences)
: ClinicalKnowledgeReference::augmentQueryWithAncientBookSources($query, $ancientBookSources));
}
} catch (\RuntimeException $error) {
return self::error('SKILL_UNAVAILABLE', '临床 AI 参考资料不可用');
@@ -365,7 +366,8 @@ class DifyChatService
$user,
true,
$attempt['files'],
$attempt['omitted']
$attempt['omitted'],
self::openAiPayloadOptions($modelConfig)
);
$lastResponse = null;
$lastSpec = [];
@@ -849,7 +851,7 @@ class DifyChatService
if (preg_match('/^[a-zA-Z0-9_-]{1,128}$/', $taskId) !== 1 || $user === '') return false;
$config = config('prescription_ai') ?: [];
$model = self::resolveProfileConfig($config, $profile);
$base = trim((string) ($config['base_url'] ?? ''));
$base = is_array($model) ? self::profileBaseUrl($config, $model) : '';
$key = (string) ($model['api_key'] ?? '');
if (!self::isValidBaseUrl($base) || trim($key) === '' || strpbrk($key, "\r\n") !== false) return false;
$endpoint = self::buildEndpoint(rtrim($base, '/'), 'chat-messages');
@@ -1307,6 +1309,31 @@ class DifyChatService
return is_array($modelConfig) ? $modelConfig : null;
}
/**
* 模型可声明自己的上游地址(如豆包直连火山方舟),未声明时沿用共享的 Dify 地址。
*
* @param array<string,mixed> $config
* @param array<string,mixed> $modelConfig
*/
private static function profileBaseUrl(array $config, array $modelConfig): string
{
$own = trim((string) ($modelConfig['base_url'] ?? ''));
return $own !== '' ? $own : trim((string) ($config['base_url'] ?? ''));
}
/**
* 仅 OpenAI 兼容协议追加的模型参数。目前只有方舟的 thinking 开关。
*
* @param array<string,mixed> $modelConfig
* @return array<string,mixed>
*/
private static function openAiPayloadOptions(array $modelConfig): array
{
$thinking = (string) ($modelConfig['thinking'] ?? '');
return in_array($thinking, ['enabled', 'disabled', 'auto'], true)
? ['thinking' => ['type' => $thinking]] : [];
}
/**
* @param array<string,mixed> $inputs
* @param array<int,array<string,string>> $files 随请求送达的附件
@@ -1321,7 +1348,8 @@ class DifyChatService
string $user,
bool $streaming = false,
array $files = [],
array $omitted = []
array $omitted = [],
array $openAiOptions = []
): array {
$baseUrl = rtrim($baseUrl, '/');
$path = strtolower((string) (parse_url($baseUrl, PHP_URL_PATH) ?? ''));
@@ -1363,7 +1391,7 @@ class DifyChatService
['role' => 'user', 'content' => $openAiContent],
],
'stream' => $streaming,
],
] + $openAiOptions,
];
if (!$streaming) {