One Hat Cyber Team
Your IP :
104.23.197.194
Server IP :
104.21.51.23
Server :
Linux 128-201-239-36.cprapid.com 3.10.0-1160.41.1.el7.x86_64 #1 SMP Tue Aug 31 14:52:47 UTC 2021 x86_64
Server Software :
Apache
PHP Version :
7.4.33
Buat File
|
Buat Folder
Eksekusi
Dir :
~
/
home
/
juscatamarca
/
www
/
juzgadosdepaz
/
app
/
services
/
View File Name :
OpenAIClient.php
<?php declare(strict_types=1); final class OpenAIClient { private string $apiKey; private string $model; private int $timeout; private int $maxOutputTokens; private float $temperature; /** @var callable(string):void */ private $logger; /** * @param array<string, mixed> $config */ public function __construct(array $config, ?callable $logger = null) { $this->apiKey = (string)($config['api_key'] ?? ''); $this->model = (string)($config['model'] ?? 'gpt-5'); $this->timeout = (int)($config['timeout'] ?? 12); $this->maxOutputTokens = (int)($config['max_output_tokens'] ?? 320); $this->temperature = (float)($config['temperature'] ?? 0.2); $this->logger = $logger ?? static function (string $message): void { error_log($message); }; } public function hasCredentials(): bool { return $this->apiKey !== ''; } /** * @param array<string, mixed> $request * @return array{ok:bool,text:string,response_id:?string,error:?string} */ public function createResponse(array $request): array { if (!$this->hasCredentials()) { return [ 'ok' => false, 'text' => '', 'response_id' => null, 'error' => 'openai_api_key_missing', ]; } $payload = [ 'model' => (string)($request['model'] ?? $this->model), 'instructions' => (string)($request['instructions'] ?? ''), 'input' => (string)($request['input'] ?? ''), 'stream' => false, 'truncation' => 'auto', 'max_output_tokens' => (int)($request['max_output_tokens'] ?? $this->maxOutputTokens), 'temperature' => (float)($request['temperature'] ?? $this->temperature), ]; if (!empty($request['previous_response_id'])) { $payload['previous_response_id'] = (string)$request['previous_response_id']; } $ch = curl_init('https://api.openai.com/v1/responses'); if ($ch === false) { return [ 'ok' => false, 'text' => '', 'response_id' => null, 'error' => 'curl_init_failed', ]; } curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_HTTPHEADER => [ 'Authorization: Bearer ' . $this->apiKey, 'Content-Type: application/json', ], CURLOPT_POSTFIELDS => json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), CURLOPT_TIMEOUT => $this->timeout, ]); $raw = curl_exec($ch); $curlError = curl_error($ch); $httpCode = (int)curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($raw === false) { ($this->logger)('[chatbot-openai] network_error: ' . $curlError); return [ 'ok' => false, 'text' => '', 'response_id' => null, 'error' => 'network_error', ]; } $decoded = json_decode((string)$raw, true); if (!is_array($decoded)) { ($this->logger)('[chatbot-openai] invalid_json_response'); return [ 'ok' => false, 'text' => '', 'response_id' => null, 'error' => 'invalid_json_response', ]; } if ($httpCode >= 400) { $errorMessage = (string)($decoded['error']['message'] ?? 'openai_http_error'); ($this->logger)('[chatbot-openai] http_error_' . $httpCode . ': ' . $errorMessage); return [ 'ok' => false, 'text' => '', 'response_id' => (string)($decoded['id'] ?? ''), 'error' => 'http_' . $httpCode, ]; } $text = $this->extractText($decoded); if ($text === '') { ($this->logger)('[chatbot-openai] empty_text_response'); return [ 'ok' => false, 'text' => '', 'response_id' => (string)($decoded['id'] ?? ''), 'error' => 'empty_text_response', ]; } return [ 'ok' => true, 'text' => $text, 'response_id' => (string)($decoded['id'] ?? ''), 'error' => null, ]; } /** * @param array<string, mixed> $decoded */ private function extractText(array $decoded): string { if (isset($decoded['output_text']) && is_string($decoded['output_text'])) { return trim($decoded['output_text']); } $texts = []; if (!empty($decoded['output']) && is_array($decoded['output'])) { foreach ($decoded['output'] as $item) { if (!is_array($item)) { continue; } if (!empty($item['content']) && is_array($item['content'])) { foreach ($item['content'] as $content) { if (!is_array($content)) { continue; } if (isset($content['text']) && is_string($content['text'])) { $texts[] = trim($content['text']); continue; } if (($content['type'] ?? '') === 'output_text' && isset($content['value']) && is_string($content['value'])) { $texts[] = trim($content['value']); } } } } } return trim(implode("\n", array_filter($texts, static fn(string $s): bool => $s !== ''))); } }