| Total Complexity | 114 |
| Total Lines | 838 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like OpenAiProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use OpenAiProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class OpenAiProvider implements AiProviderInterface, AiImageProviderInterface, AiVideoProviderInterface, AiDocumentProviderInterface |
||
| 19 | { |
||
| 20 | private array $providerConfig; |
||
| 21 | private string $apiKey; |
||
| 22 | private string $organizationId; |
||
| 23 | private int $monthlyTokenLimit; |
||
| 24 | |||
| 25 | // OpenAI Videos API constraints (validate early to avoid avoidable 400s). |
||
| 26 | private const ALLOWED_VIDEO_MODELS = ['sora-2', 'sora-2-pro']; |
||
| 27 | private const ALLOWED_VIDEO_SECONDS = ['4', '8', '12']; |
||
| 28 | private const ALLOWED_VIDEO_SIZES = ['720x1280', '1280x720', '1024x1792', '1792x1024']; |
||
| 29 | |||
| 30 | public function __construct( |
||
| 31 | private readonly HttpClientInterface $httpClient, |
||
| 32 | SettingsManager $settingsManager, |
||
| 33 | private readonly AiRequestsRepository $aiRequestsRepository, |
||
| 34 | private readonly Security $security |
||
| 35 | ) { |
||
| 36 | $configJson = $settingsManager->getSetting('ai_helpers.ai_providers', true); |
||
| 37 | $config = \is_string($configJson) ? (json_decode($configJson, true) ?? []) : (\is_array($configJson) ? $configJson : []); |
||
| 38 | |||
| 39 | if (!isset($config['openai']) || !\is_array($config['openai'])) { |
||
| 40 | throw new RuntimeException('OpenAI configuration is missing.'); |
||
| 41 | } |
||
| 42 | |||
| 43 | $this->providerConfig = $config['openai']; |
||
| 44 | |||
| 45 | $this->apiKey = (string) ($this->providerConfig['api_key'] ?? ''); |
||
| 46 | $this->organizationId = (string) ($this->providerConfig['organization_id'] ?? ''); |
||
| 47 | $this->monthlyTokenLimit = (int) ($this->providerConfig['monthly_token_limit'] ?? 0); |
||
| 48 | |||
| 49 | if ('' === trim($this->apiKey)) { |
||
| 50 | throw new RuntimeException('OpenAI API key is missing.'); |
||
| 51 | } |
||
| 52 | } |
||
| 53 | |||
| 54 | public function generateQuestions(string $topic, int $numQuestions, string $questionType, string $language): ?string |
||
| 75 | } |
||
| 76 | |||
| 77 | public function generateLearnPath(string $topic, int $chaptersCount, string $language, int $wordsCount, bool $addTests, int $numQuestions): ?array |
||
| 161 | ]; |
||
| 162 | } |
||
| 163 | |||
| 164 | public function gradeOpenAnswer(string $prompt, string $toolName): ?string |
||
| 165 | { |
||
| 166 | return $this->requestChatCompletion($prompt, $toolName, 'text'); |
||
| 167 | } |
||
| 168 | |||
| 169 | public function generateDocument(string $prompt, string $toolName, ?array $options = []): ?string |
||
| 172 | } |
||
| 173 | |||
| 174 | public function generateImage(string $prompt, string $toolName, ?array $options = []): string|array|null |
||
| 175 | { |
||
| 176 | $userId = $this->getUserId(); |
||
| 177 | if (!$userId) { |
||
|
|
|||
| 178 | throw new RuntimeException('User not authenticated.'); |
||
| 179 | } |
||
| 180 | |||
| 181 | $cfg = $this->getTypeConfig('image'); |
||
| 182 | $url = (string) ($cfg['url'] ?? 'https://api.openai.com/v1/images/generations'); |
||
| 183 | $model = (string) ($cfg['model'] ?? 'gpt-image-1'); |
||
| 184 | $size = (string) ($cfg['size'] ?? '1024x1024'); |
||
| 185 | $quality = (string) ($cfg['quality'] ?? 'standard'); |
||
| 186 | $n = (int) (($options['n'] ?? null) ?? ($cfg['n'] ?? 1)); |
||
| 187 | |||
| 188 | $promptTrimmed = trim($prompt); |
||
| 189 | $promptForLog = mb_substr($promptTrimmed, 0, 200); |
||
| 190 | |||
| 191 | if ('dall-e-3' === $model && $n !== 1) { |
||
| 192 | error_log(sprintf('[AI][OpenAI][image] Model "%s" only supports n=1. Forcing n from %d to 1.', $model, $n)); |
||
| 193 | $n = 1; |
||
| 194 | } |
||
| 195 | |||
| 196 | $payload = [ |
||
| 197 | 'model' => $model, |
||
| 198 | 'prompt' => $promptTrimmed, |
||
| 199 | 'size' => $size, |
||
| 200 | 'quality' => $quality, |
||
| 201 | 'n' => $n, |
||
| 202 | ]; |
||
| 203 | |||
| 204 | // Best-effort: allow response_format for any model that supports it. |
||
| 205 | $responseFormat = (string) ($cfg['response_format'] ?? 'b64_json'); |
||
| 206 | if ('' !== trim($responseFormat)) { |
||
| 207 | $payload['response_format'] = $responseFormat; |
||
| 208 | } |
||
| 209 | |||
| 210 | error_log(sprintf( |
||
| 211 | '[AI][OpenAI][image] Request: userId=%d tool=%s url=%s model=%s size=%s quality=%s n=%d prompt="%s"', |
||
| 212 | $userId, |
||
| 213 | $toolName, |
||
| 214 | $url, |
||
| 215 | $model, |
||
| 216 | $size, |
||
| 217 | $quality, |
||
| 218 | $n, |
||
| 219 | $promptForLog |
||
| 220 | )); |
||
| 221 | |||
| 222 | try { |
||
| 223 | $response = $this->httpClient->request('POST', $url, [ |
||
| 224 | 'headers' => $this->buildAuthHeaders(true), |
||
| 225 | 'json' => $payload, |
||
| 226 | ]); |
||
| 227 | |||
| 228 | $status = $response->getStatusCode(); |
||
| 229 | $headers = $response->getHeaders(false); |
||
| 230 | $requestId = $this->extractRequestId($headers); |
||
| 231 | |||
| 232 | $raw = $response->getContent(false); |
||
| 233 | $rawForLog = mb_substr((string) $raw, 0, 2000); |
||
| 234 | |||
| 235 | if ($status >= 400) { |
||
| 236 | $decoded = json_decode((string) $raw, true); |
||
| 237 | $msg = $decoded['error']['message'] ?? null; |
||
| 238 | $type = $decoded['error']['type'] ?? null; |
||
| 239 | $code = $decoded['error']['code'] ?? null; |
||
| 240 | $param = $decoded['error']['param'] ?? null; |
||
| 241 | |||
| 242 | $finalMsg = $msg ?: 'OpenAI returned an error response.'; |
||
| 243 | error_log(sprintf( |
||
| 244 | '[AI][OpenAI][image] HTTP %d request_id=%s type=%s code=%s param=%s body=%s', |
||
| 245 | $status, |
||
| 246 | (string) $requestId, |
||
| 247 | (string) $type, |
||
| 248 | (string) $code, |
||
| 249 | (string) $param, |
||
| 250 | $rawForLog |
||
| 251 | )); |
||
| 252 | |||
| 253 | return 'Error: '.$finalMsg; |
||
| 254 | } |
||
| 255 | |||
| 256 | $data = json_decode((string) $raw, true); |
||
| 257 | if (!is_array($data)) { |
||
| 258 | error_log(sprintf( |
||
| 259 | '[AI][OpenAI][image] Invalid JSON response. HTTP %d request_id=%s body=%s', |
||
| 260 | $status, |
||
| 261 | (string) $requestId, |
||
| 262 | $rawForLog |
||
| 263 | )); |
||
| 264 | |||
| 265 | return 'Error: Invalid JSON response from OpenAI.'; |
||
| 266 | } |
||
| 267 | |||
| 268 | if (!isset($data['data'][0]) || !is_array($data['data'][0])) { |
||
| 269 | error_log(sprintf( |
||
| 270 | '[AI][OpenAI][image] Missing image data[0]. HTTP %d request_id=%s keys=%s body=%s', |
||
| 271 | $status, |
||
| 272 | (string) $requestId, |
||
| 273 | implode(',', array_keys($data)), |
||
| 274 | $rawForLog |
||
| 275 | )); |
||
| 276 | |||
| 277 | return 'Error: OpenAI response missing image data.'; |
||
| 278 | } |
||
| 279 | |||
| 280 | $item = $data['data'][0]; |
||
| 281 | |||
| 282 | $result = [ |
||
| 283 | 'content' => null, |
||
| 284 | 'url' => null, |
||
| 285 | 'is_base64' => false, |
||
| 286 | 'content_type' => 'image/png', |
||
| 287 | 'revised_prompt' => $item['revised_prompt'] ?? null, |
||
| 288 | ]; |
||
| 289 | |||
| 290 | if (isset($item['b64_json']) && \is_string($item['b64_json']) && '' !== $item['b64_json']) { |
||
| 291 | $result['content'] = $item['b64_json']; |
||
| 292 | $result['is_base64'] = true; |
||
| 293 | } elseif (isset($item['url']) && \is_string($item['url']) && '' !== $item['url']) { |
||
| 294 | $result['url'] = $item['url']; |
||
| 295 | $result['is_base64'] = false; |
||
| 296 | } else { |
||
| 297 | error_log(sprintf( |
||
| 298 | '[AI][OpenAI][image] Response did not include b64_json or url. HTTP %d request_id=%s body=%s', |
||
| 299 | $status, |
||
| 300 | (string) $requestId, |
||
| 301 | $rawForLog |
||
| 302 | )); |
||
| 303 | |||
| 304 | return 'Error: OpenAI response did not include image content.'; |
||
| 305 | } |
||
| 306 | |||
| 307 | $this->saveAiRequest( |
||
| 308 | $userId, |
||
| 309 | $toolName, |
||
| 310 | $promptTrimmed, |
||
| 311 | 'openai', |
||
| 312 | (int) ($data['usage']['prompt_tokens'] ?? 0), |
||
| 313 | (int) ($data['usage']['completion_tokens'] ?? 0), |
||
| 314 | (int) ($data['usage']['total_tokens'] ?? 0) |
||
| 315 | ); |
||
| 316 | |||
| 317 | error_log(sprintf( |
||
| 318 | '[AI][OpenAI][image] Success. HTTP %d request_id=%s revised_prompt=%s returned_base64=%s returned_url=%s', |
||
| 319 | $status, |
||
| 320 | (string) $requestId, |
||
| 321 | isset($result['revised_prompt']) ? (string) $result['revised_prompt'] : '', |
||
| 322 | $result['is_base64'] ? 'yes' : 'no', |
||
| 323 | !empty($result['url']) ? 'yes' : 'no' |
||
| 324 | )); |
||
| 325 | |||
| 326 | return $result; |
||
| 327 | } catch (Exception $e) { |
||
| 328 | error_log('[AI][OpenAI][image] Exception: '.$e->getMessage()); |
||
| 329 | return 'Error: '.$e->getMessage(); |
||
| 330 | } |
||
| 331 | } |
||
| 332 | |||
| 333 | public function generateVideo(string $prompt, string $toolName, ?array $options = []): string|array|null |
||
| 334 | { |
||
| 335 | $userId = $this->getUserId(); |
||
| 336 | if (!$userId) { |
||
| 337 | throw new RuntimeException('User not authenticated.'); |
||
| 338 | } |
||
| 339 | |||
| 340 | $cfg = $this->getTypeConfig('video'); |
||
| 341 | $url = (string) ($cfg['url'] ?? 'https://api.openai.com/v1/videos'); |
||
| 342 | $model = (string) ($cfg['model'] ?? 'sora-2'); |
||
| 343 | $seconds = (string) (($options['seconds'] ?? null) ?? ($cfg['seconds'] ?? '8')); |
||
| 344 | $size = (string) (($options['size'] ?? null) ?? ($cfg['size'] ?? '720x1280')); |
||
| 345 | |||
| 346 | $model = strtolower(trim($model)); |
||
| 347 | $seconds = trim((string) $seconds); |
||
| 348 | $size = trim((string) $size); |
||
| 349 | |||
| 350 | if (!in_array($model, self::ALLOWED_VIDEO_MODELS, true)) { |
||
| 351 | return 'Error: Invalid value for model. Expected one of: '.implode(', ', self::ALLOWED_VIDEO_MODELS).'.'; |
||
| 352 | } |
||
| 353 | |||
| 354 | if (!in_array($seconds, self::ALLOWED_VIDEO_SECONDS, true)) { |
||
| 355 | return 'Error: Invalid value for seconds. Expected one of: '.implode(', ', self::ALLOWED_VIDEO_SECONDS).'.'; |
||
| 356 | } |
||
| 357 | |||
| 358 | if (!in_array($size, self::ALLOWED_VIDEO_SIZES, true)) { |
||
| 359 | return 'Error: Invalid value for size. Expected one of: '.implode(', ', self::ALLOWED_VIDEO_SIZES).'.'; |
||
| 360 | } |
||
| 361 | |||
| 362 | $promptTrimmed = trim($prompt); |
||
| 363 | $promptForLog = mb_substr($promptTrimmed, 0, 200); |
||
| 364 | |||
| 365 | error_log(sprintf( |
||
| 366 | '[AI][OpenAI][video] Request: userId=%d tool=%s url=%s model=%s seconds=%s size=%s prompt="%s"', |
||
| 367 | $userId, |
||
| 368 | $toolName, |
||
| 369 | $url, |
||
| 370 | $model, |
||
| 371 | $seconds, |
||
| 372 | $size, |
||
| 373 | $promptForLog |
||
| 374 | )); |
||
| 375 | |||
| 376 | try { |
||
| 377 | $fields = [ |
||
| 378 | 'model' => $model, |
||
| 379 | 'prompt' => $promptTrimmed, |
||
| 380 | 'seconds' => $seconds, |
||
| 381 | 'size' => $size, |
||
| 382 | ]; |
||
| 383 | |||
| 384 | if ( |
||
| 385 | isset($options['input_reference_path']) |
||
| 386 | && is_string($options['input_reference_path']) |
||
| 387 | && '' !== $options['input_reference_path'] |
||
| 388 | ) { |
||
| 389 | $path = $options['input_reference_path']; |
||
| 390 | if (is_readable($path)) { |
||
| 391 | $fields['input_reference'] = DataPart::fromPath($path); |
||
| 392 | } else { |
||
| 393 | error_log('[AI][OpenAI][video] input_reference_path is not readable: '.$path); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | $formData = new FormDataPart($fields); |
||
| 398 | |||
| 399 | $headers = array_merge( |
||
| 400 | $this->buildAuthHeaders(false), |
||
| 401 | $formData->getPreparedHeaders()->toArray(), |
||
| 402 | [ |
||
| 403 | 'Accept' => 'application/json', |
||
| 404 | ] |
||
| 405 | ); |
||
| 406 | |||
| 407 | $response = $this->httpClient->request('POST', $url, [ |
||
| 408 | 'headers' => $headers, |
||
| 409 | 'body' => $formData->bodyToIterable(), |
||
| 410 | ]); |
||
| 411 | |||
| 412 | $status = $response->getStatusCode(); |
||
| 413 | $respHeaders = $response->getHeaders(false); |
||
| 414 | $requestId = $this->extractRequestId($respHeaders); |
||
| 415 | |||
| 416 | $raw = (string) $response->getContent(false); |
||
| 417 | $rawForLog = mb_substr($raw, 0, 2000); |
||
| 418 | |||
| 419 | if ($status >= 400) { |
||
| 420 | $err = $this->decodeOpenAiError($raw); |
||
| 421 | $finalMsg = (string) ($err['message'] ?? ''); |
||
| 422 | |||
| 423 | if ('' === trim($finalMsg)) { |
||
| 424 | $finalMsg = sprintf( |
||
| 425 | 'OpenAI returned HTTP %d. Ensure your project/org has access to model "%s" and the organization is verified if required.', |
||
| 426 | $status, |
||
| 427 | $model |
||
| 428 | ); |
||
| 429 | } |
||
| 430 | |||
| 431 | error_log(sprintf( |
||
| 432 | '[AI][OpenAI][video] HTTP %d request_id=%s type=%s code=%s param=%s error="%s" body=%s', |
||
| 433 | $status, |
||
| 434 | (string) $requestId, |
||
| 435 | (string) ($err['type'] ?? ''), |
||
| 436 | (string) ($err['code'] ?? ''), |
||
| 437 | (string) ($err['param'] ?? ''), |
||
| 438 | $finalMsg, |
||
| 439 | $rawForLog !== '' ? $rawForLog : '(empty body)' |
||
| 440 | )); |
||
| 441 | |||
| 442 | return 'Error: '.$finalMsg; |
||
| 443 | } |
||
| 444 | |||
| 445 | $data = json_decode($raw, true); |
||
| 446 | if (!is_array($data)) { |
||
| 447 | error_log(sprintf( |
||
| 448 | '[AI][OpenAI][video] Invalid JSON response. HTTP %d request_id=%s body=%s', |
||
| 449 | $status, |
||
| 450 | (string) $requestId, |
||
| 451 | $rawForLog !== '' ? $rawForLog : '(empty body)' |
||
| 452 | )); |
||
| 453 | return 'Error: Invalid JSON response from OpenAI.'; |
||
| 454 | } |
||
| 455 | |||
| 456 | if (!isset($data['id']) || !is_string($data['id']) || '' === trim($data['id'])) { |
||
| 457 | error_log(sprintf( |
||
| 458 | '[AI][OpenAI][video] Missing "id" in response. HTTP %d request_id=%s keys=%s body=%s', |
||
| 459 | $status, |
||
| 460 | (string) $requestId, |
||
| 461 | implode(',', array_keys($data)), |
||
| 462 | $rawForLog |
||
| 463 | )); |
||
| 464 | return 'Error: OpenAI response missing "id".'; |
||
| 465 | } |
||
| 466 | |||
| 467 | $result = [ |
||
| 468 | 'id' => $data['id'], |
||
| 469 | 'status' => (string) ($data['status'] ?? ''), |
||
| 470 | 'content' => null, |
||
| 471 | 'url' => null, |
||
| 472 | 'is_base64' => false, |
||
| 473 | 'content_type' => 'video/mp4', |
||
| 474 | 'revised_prompt' => null, |
||
| 475 | 'job' => $data, |
||
| 476 | ]; |
||
| 477 | |||
| 478 | $this->saveAiRequest($userId, $toolName, $promptTrimmed, 'openai', 0, 0, 0); |
||
| 479 | |||
| 480 | error_log(sprintf( |
||
| 481 | '[AI][OpenAI][video] Job created. HTTP %d request_id=%s id=%s status=%s', |
||
| 482 | $status, |
||
| 483 | (string) $requestId, |
||
| 484 | (string) $result['id'], |
||
| 485 | (string) $result['status'] |
||
| 486 | )); |
||
| 487 | |||
| 488 | return $result; |
||
| 489 | } catch (Exception $e) { |
||
| 490 | error_log('[AI][OpenAI][video] Exception: '.$e->getMessage()); |
||
| 491 | return 'Error: '.$e->getMessage(); |
||
| 492 | } |
||
| 493 | } |
||
| 494 | |||
| 495 | public function getVideoJobStatus(string $jobId): ?array |
||
| 496 | { |
||
| 497 | $cfg = $this->getTypeConfig('video'); |
||
| 498 | $statusUrlTpl = (string) ($cfg['status_url'] ?? 'https://api.openai.com/v1/videos/{id}'); |
||
| 499 | $statusUrl = str_replace('{id}', rawurlencode($jobId), $statusUrlTpl); |
||
| 500 | |||
| 501 | try { |
||
| 502 | $response = $this->httpClient->request('GET', $statusUrl, [ |
||
| 503 | 'headers' => $this->buildAuthHeaders(false), |
||
| 504 | ]); |
||
| 505 | |||
| 506 | $status = $response->getStatusCode(); |
||
| 507 | $headers = $response->getHeaders(false); |
||
| 508 | $requestId = $this->extractRequestId($headers); |
||
| 509 | |||
| 510 | $raw = (string) $response->getContent(false); |
||
| 511 | |||
| 512 | if ($status >= 400) { |
||
| 513 | $msg = $this->extractOpenAiErrorMessage($raw); |
||
| 514 | error_log(sprintf( |
||
| 515 | '[AI][OpenAI][video] Status HTTP %d request_id=%s id=%s error="%s" body=%s', |
||
| 516 | $status, |
||
| 517 | $requestId, |
||
| 518 | $jobId, |
||
| 519 | $msg, |
||
| 520 | $this->safeTruncate($raw, 2000) |
||
| 521 | )); |
||
| 522 | return [ |
||
| 523 | 'id' => $jobId, |
||
| 524 | 'status' => 'failed', |
||
| 525 | 'error' => $msg, |
||
| 526 | 'job' => null, |
||
| 527 | ]; |
||
| 528 | } |
||
| 529 | |||
| 530 | $data = json_decode($raw, true); |
||
| 531 | if (!is_array($data)) { |
||
| 532 | return [ |
||
| 533 | 'id' => $jobId, |
||
| 534 | 'status' => '', |
||
| 535 | 'error' => 'Invalid JSON response from OpenAI.', |
||
| 536 | 'job' => null, |
||
| 537 | ]; |
||
| 538 | } |
||
| 539 | |||
| 540 | return [ |
||
| 541 | 'id' => (string) ($data['id'] ?? $jobId), |
||
| 542 | 'status' => (string) ($data['status'] ?? ''), |
||
| 543 | 'error' => null, |
||
| 544 | 'job' => $data, |
||
| 545 | ]; |
||
| 546 | } catch (Exception $e) { |
||
| 547 | error_log('[AI][OpenAI][video] getVideoJobStatus exception: '.$e->getMessage()); |
||
| 548 | return [ |
||
| 549 | 'id' => $jobId, |
||
| 550 | 'status' => '', |
||
| 551 | 'error' => $e->getMessage(), |
||
| 552 | 'job' => null, |
||
| 553 | ]; |
||
| 554 | } |
||
| 555 | } |
||
| 556 | |||
| 557 | public function getVideoJobContentAsBase64(string $jobId, int $maxBytes = 15728640): ?array |
||
| 654 | ]; |
||
| 655 | } |
||
| 656 | } |
||
| 657 | |||
| 658 | private function requestChatCompletion(string $prompt, string $toolName, string $type): ?string |
||
| 659 | { |
||
| 660 | $userId = $this->getUserId(); |
||
| 661 | if (!$userId) { |
||
| 662 | throw new RuntimeException('User not authenticated.'); |
||
| 663 | } |
||
| 664 | |||
| 665 | $cfg = $this->getTypeConfig($type); |
||
| 666 | if ('document' === $type && empty($cfg)) { |
||
| 667 | $cfg = $this->getTypeConfig('text'); |
||
| 668 | } |
||
| 669 | |||
| 670 | if (empty($cfg)) { |
||
| 671 | error_log('[AI][OpenAI] Missing config for type: '.$type); |
||
| 672 | return null; |
||
| 673 | } |
||
| 674 | |||
| 675 | $url = (string) ($cfg['url'] ?? 'https://api.openai.com/v1/chat/completions'); |
||
| 676 | $model = (string) ($cfg['model'] ?? 'gpt-4o-mini'); |
||
| 677 | $temperature = (float) ($cfg['temperature'] ?? 0.7); |
||
| 678 | $maxTokens = (int) ($cfg['max_tokens'] ?? 1000); |
||
| 679 | |||
| 680 | $payload = [ |
||
| 681 | 'model' => $model, |
||
| 682 | 'messages' => [ |
||
| 683 | ['role' => 'system', 'content' => 'You are a helpful AI assistant that generates structured educational content.'], |
||
| 684 | ['role' => 'user', 'content' => $prompt], |
||
| 685 | ], |
||
| 686 | 'temperature' => $temperature, |
||
| 687 | 'max_tokens' => $maxTokens, |
||
| 688 | ]; |
||
| 689 | |||
| 690 | try { |
||
| 691 | $response = $this->httpClient->request('POST', $url, [ |
||
| 692 | 'headers' => $this->buildAuthHeaders(true), |
||
| 693 | 'json' => $payload, |
||
| 694 | ]); |
||
| 695 | |||
| 696 | $data = $response->toArray(false); |
||
| 697 | |||
| 698 | $generatedContent = $data['choices'][0]['message']['content'] ?? null; |
||
| 699 | if (!\is_string($generatedContent) || '' === trim($generatedContent)) { |
||
| 700 | error_log('[AI][OpenAI] Empty content returned for type: '.$type); |
||
| 701 | return null; |
||
| 702 | } |
||
| 703 | |||
| 704 | $this->saveAiRequest( |
||
| 705 | $userId, |
||
| 706 | $toolName, |
||
| 707 | $prompt, |
||
| 708 | 'openai', |
||
| 709 | (int) ($data['usage']['prompt_tokens'] ?? 0), |
||
| 710 | (int) ($data['usage']['completion_tokens'] ?? 0), |
||
| 711 | (int) ($data['usage']['total_tokens'] ?? 0) |
||
| 712 | ); |
||
| 713 | |||
| 714 | return $generatedContent; |
||
| 715 | } catch (Exception $e) { |
||
| 716 | error_log('[AI][OpenAI] Exception: '.$e->getMessage()); |
||
| 717 | return null; |
||
| 718 | } |
||
| 719 | } |
||
| 720 | |||
| 721 | private function filterValidAikenQuestions(string $quizContent): array |
||
| 722 | { |
||
| 723 | $questions = preg_split('/\n{2,}/', trim($quizContent)); |
||
| 724 | |||
| 725 | $validQuestions = []; |
||
| 726 | foreach ($questions as $questionBlock) { |
||
| 727 | $lines = explode("\n", trim($questionBlock)); |
||
| 728 | |||
| 729 | if (\count($lines) < 6) { |
||
| 730 | continue; |
||
| 731 | } |
||
| 732 | |||
| 733 | $options = \array_slice($lines, 1, 4); |
||
| 734 | $validOptions = array_filter($options, static fn ($line) => (bool) preg_match('/^[A-D]\. .+/', $line)); |
||
| 735 | |||
| 736 | $answerLine = end($lines); |
||
| 737 | if (4 === \count($validOptions) && \is_string($answerLine) && preg_match('/^ANSWER: [A-D]$/', $answerLine)) { |
||
| 738 | $validQuestions[] = implode("\n", $lines); |
||
| 739 | } |
||
| 740 | } |
||
| 741 | |||
| 742 | return $validQuestions; |
||
| 743 | } |
||
| 744 | |||
| 745 | private function getTypeConfig(string $type): array |
||
| 746 | { |
||
| 747 | $cfg = $this->providerConfig[$type] ?? null; |
||
| 748 | return \is_array($cfg) ? $cfg : []; |
||
| 749 | } |
||
| 750 | |||
| 751 | private function buildAuthHeaders(bool $json): array |
||
| 752 | { |
||
| 753 | $headers = [ |
||
| 754 | 'Authorization' => 'Bearer '.$this->apiKey, |
||
| 755 | ]; |
||
| 756 | |||
| 757 | if ('' !== trim($this->organizationId)) { |
||
| 758 | $headers['OpenAI-Organization'] = $this->organizationId; |
||
| 759 | } |
||
| 760 | |||
| 761 | $projectId = (string) ($this->providerConfig['project_id'] ?? ''); |
||
| 762 | if ('' !== trim($projectId)) { |
||
| 763 | $headers['OpenAI-Project'] = $projectId; |
||
| 764 | } |
||
| 765 | |||
| 766 | if ($json) { |
||
| 767 | $headers['Content-Type'] = 'application/json'; |
||
| 768 | } |
||
| 769 | |||
| 770 | return $headers; |
||
| 771 | } |
||
| 772 | |||
| 773 | private function saveAiRequest( |
||
| 774 | int $userId, |
||
| 775 | string $toolName, |
||
| 776 | string $requestText, |
||
| 777 | string $provider, |
||
| 778 | int $promptTokens = 0, |
||
| 779 | int $completionTokens = 0, |
||
| 780 | int $totalTokens = 0 |
||
| 781 | ): void { |
||
| 782 | try { |
||
| 783 | $aiRequest = new AiRequests(); |
||
| 784 | $aiRequest |
||
| 785 | ->setUserId($userId) |
||
| 786 | ->setToolName($toolName) |
||
| 787 | ->setRequestText($requestText) |
||
| 788 | ->setPromptTokens($promptTokens) |
||
| 789 | ->setCompletionTokens($completionTokens) |
||
| 790 | ->setTotalTokens($totalTokens) |
||
| 791 | ->setAiProvider($provider) |
||
| 792 | ; |
||
| 793 | |||
| 794 | $this->aiRequestsRepository->save($aiRequest); |
||
| 795 | } catch (Exception $e) { |
||
| 796 | error_log('[AI] Failed to save AiRequests record: '.$e->getMessage()); |
||
| 797 | } |
||
| 798 | } |
||
| 799 | |||
| 800 | private function getUserId(): ?int |
||
| 801 | { |
||
| 802 | $user = $this->security->getUser(); |
||
| 803 | return $user instanceof UserInterface ? $user->getId() : null; |
||
| 804 | } |
||
| 805 | |||
| 806 | private function extractRequestId(array $headers): string |
||
| 807 | { |
||
| 808 | return (string) ($headers['x-request-id'][0] ?? $headers['request-id'][0] ?? ''); |
||
| 809 | } |
||
| 810 | |||
| 811 | private function extractOpenAiErrorMessage(string $raw): string |
||
| 812 | { |
||
| 813 | $decoded = json_decode($raw, true); |
||
| 814 | if (is_array($decoded)) { |
||
| 815 | $msg = $decoded['error']['message'] ?? null; |
||
| 816 | if (is_string($msg) && trim($msg) !== '') { |
||
| 817 | return trim($msg); |
||
| 818 | } |
||
| 819 | } |
||
| 820 | return 'OpenAI returned an error response.'; |
||
| 821 | } |
||
| 822 | |||
| 823 | private function safeTruncate(string $s, int $max = 2000): string |
||
| 824 | { |
||
| 825 | return mb_substr($s, 0, $max); |
||
| 826 | } |
||
| 827 | |||
| 828 | private function decodeOpenAiError(string $raw): array |
||
| 856 | ]; |
||
| 857 | } |
||
| 858 | } |
||
| 859 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: