- [4.1 认证 API](#41-认证-api)
- [4.1.1 获取访问令牌](#411-获取访问令牌)
- [4.2 客户用户 API](#42-客户用户-api)
- [4.2.1 发送提示词请求](#421-发送提示词请求)
- [4.3 管理员 API](#43-管理员-api)
- [4.3.1 LLM 提供商管理](#431-llm-提供商管理)
- [4.3.1.1 新增 LLM 提供商](#4311-新增-llm-提供商)
- [4.3.1.2 修改 LLM 提供商](#4312-修改-llm-提供商)
- [4.3.1.3 删除 LLM 提供商](#4313-删除-llm-提供商)
- [4.3.1.4 获取 LLM 提供商列表](#4314-获取-llm-提供商列表)
149 lines
4.8 KiB
PHP
149 lines
4.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Controllers\Api;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Models\Client;
|
|
use App\Services\LogService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\RateLimiter;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class LlmController extends Controller
|
|
{
|
|
private const REQUEST_TIMEOUT = 30; // 请求超时时间(秒)
|
|
private const MAX_ATTEMPTS = 60; // 每分钟最大请求次数
|
|
private const DECAY_MINUTES = 1; // 重置时间(分钟)
|
|
|
|
public function __construct(
|
|
private readonly LogService $logService
|
|
) {}
|
|
|
|
/**
|
|
* Send prompt request to LLM provider
|
|
*
|
|
* @param Request $request
|
|
* @return JsonResponse
|
|
*
|
|
* Request headers:
|
|
* - Content-Type: application/json
|
|
* - Authorization: Bearer {access_token}
|
|
*
|
|
* Request body:
|
|
* {
|
|
* "prompt": "string"
|
|
* }
|
|
*
|
|
* Success response:
|
|
* {
|
|
* "response": "LLM 提供商返回的响应内容"
|
|
* }
|
|
*
|
|
* Error response:
|
|
* {
|
|
* "error": "error_code",
|
|
* "message": "错误描述"
|
|
* }
|
|
*/
|
|
public function request(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$clientId = $request->get('client_id');
|
|
|
|
// 检查请求频率限制
|
|
$key = 'llm_request_' . $clientId;
|
|
if (RateLimiter::tooManyAttempts($key, self::MAX_ATTEMPTS)) {
|
|
$seconds = RateLimiter::availableIn($key);
|
|
return response()->json([
|
|
'error' => 'too_many_requests',
|
|
'message' => "请求过于频繁,请在 {$seconds} 秒后重试。",
|
|
], 429);
|
|
}
|
|
|
|
$validated = $request->validate([
|
|
'prompt' => 'required|string|max:4096',
|
|
]);
|
|
|
|
$client = Client::with('llmProvider')->findOrFail($clientId);
|
|
$provider = $client->llmProvider;
|
|
|
|
// Send request to LLM provider with timeout
|
|
$response = Http::timeout(self::REQUEST_TIMEOUT)
|
|
->withToken($provider->api_token)
|
|
->withHeaders([
|
|
'Content-Type' => 'application/json',
|
|
'Accept' => 'application/json',
|
|
])
|
|
->post($provider->api_url, [
|
|
'prompt' => $validated['prompt'],
|
|
]);
|
|
|
|
if (!$response->successful()) {
|
|
Log::error('LLM provider request failed', [
|
|
'status' => $response->status(),
|
|
'body' => $response->body(),
|
|
'provider' => $provider->name,
|
|
]);
|
|
|
|
throw new \Exception('LLM provider request failed: ' . $response->body());
|
|
}
|
|
|
|
// 记录成功的请求
|
|
RateLimiter::hit($key, self::DECAY_MINUTES * 60);
|
|
|
|
$this->logService->logOperation(
|
|
'client',
|
|
$client->id,
|
|
'Sent prompt to LLM provider: ' . $provider->name
|
|
);
|
|
|
|
return response()->json([
|
|
'response' => $response->json(),
|
|
]);
|
|
|
|
} catch (ValidationException $e) {
|
|
return response()->json([
|
|
'error' => 'validation_error',
|
|
'message' => '请求参数验证失败。',
|
|
'details' => $e->errors(),
|
|
], 422);
|
|
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
|
|
return response()->json([
|
|
'error' => 'not_found',
|
|
'message' => '客户用户不存在。',
|
|
], 404);
|
|
} catch (\Illuminate\Http\Client\ConnectionException $e) {
|
|
Log::error('LLM provider connection timeout', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
return response()->json([
|
|
'error' => 'provider_timeout',
|
|
'message' => 'LLM 提供商响应超时,请稍后重试。',
|
|
], 504);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error processing LLM request', [
|
|
'error' => $e->getMessage(),
|
|
'trace' => $e->getTraceAsString(),
|
|
]);
|
|
|
|
if (str_contains($e->getMessage(), 'LLM provider request failed')) {
|
|
return response()->json([
|
|
'error' => 'provider_error',
|
|
'message' => 'LLM 提供商服务异常,请稍后重试。',
|
|
], 502);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => 'server_error',
|
|
'message' => '服务器内部错误。',
|
|
], 500);
|
|
}
|
|
}
|
|
}
|