erp-backend/app/Http/Requests/SwitchModelRequest.php
2026-04-01 17:07:04 +08:00

116 lines
3.6 KiB
PHP

<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class SwitchModelRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
$availableServices = ['openai', 'azure_openai', 'anthropic', 'aliyun_qwen', 'local'];
$modelRules = [
'openai' => ['gpt-4', 'gpt-4-turbo', 'gpt-3.5-turbo', 'gpt-3.5-turbo-instruct'],
'azure_openai' => ['gpt-4', 'gpt-35-turbo'],
'anthropic' => ['claude-3-opus-20240229', 'claude-3-sonnet-20240229', 'claude-3-haiku-20240307'],
'aliyun_qwen' => ['qwen-max', 'qwen-plus', 'qwen-turbo', 'qwen-7b-chat', 'qwen-14b-chat'],
'local' => ['local-model'],
];
$service = $this->input('service');
$allowedModels = $modelRules[$service] ?? [];
return [
'service' => 'required|string|in:' . implode(',', $availableServices),
'model' => 'required|string|in:' . implode(',', $allowedModels),
];
}
/**
* Get custom messages for validator errors.
*/
public function messages(): array
{
return [
'service.required' => '服务类型不能为空',
'service.in' => '不支持的服务类型',
'model.required' => '模型名称不能为空',
'model.in' => '不支持该服务的模型',
];
}
/**
* Prepare the data for validation.
*/
protected function prepareForValidation()
{
// 确保服务名称小写
if ($this->has('service')) {
$this->merge([
'service' => strtolower($this->service),
]);
}
// 确保模型名称小写
if ($this->has('model')) {
$this->merge([
'model' => strtolower($this->model),
]);
}
}
/**
* Get validated data with defaults.
*/
public function validatedData(): array
{
$validated = parent::validated();
// 添加服务显示名称
$serviceNames = [
'openai' => 'OpenAI',
'azure_openai' => 'Azure OpenAI',
'anthropic' => 'Anthropic Claude',
'aliyun_qwen' => '阿里云通义千问',
'local' => '本地模型',
];
$validated['service_display_name'] = $serviceNames[$validated['service']] ?? $validated['service'];
// 添加模型显示名称
$modelNames = [
'gpt-4' => 'GPT-4',
'gpt-4-turbo' => 'GPT-4 Turbo',
'gpt-3.5-turbo' => 'GPT-3.5 Turbo',
'gpt-3.5-turbo-instruct' => 'GPT-3.5 Instruct',
'gpt-35-turbo' => 'GPT-3.5 Turbo (Azure)',
'claude-3-opus-20240229' => 'Claude 3 Opus',
'claude-3-sonnet-20240229' => 'Claude 3 Sonnet',
'claude-3-haiku-20240307' => 'Claude 3 Haiku',
'qwen-max' => '通义千问 Max',
'qwen-plus' => '通义千问 Plus',
'qwen-turbo' => '通义千问 Turbo',
'qwen-7b-chat' => '通义千问 7B',
'qwen-14b-chat' => '通义千问 14B',
'local-model' => '本地模型',
];
$validated['model_display_name'] = $modelNames[$validated['model']] ?? $validated['model'];
return $validated;
}
}