| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\OpenAI; |
| 4 |
|
| 5 |
use AATXT\App\AIProviders\AIProviderInterface; |
| 6 |
use AATXT\App\AIProviders\Contracts\RequiresAuthentication; |
| 7 |
use AATXT\App\AIProviders\Contracts\SupportsImageValidation; |
| 8 |
use AATXT\App\Configuration\AIProviderConfig; |
| 9 |
use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 10 |
use AATXT\App\Infrastructure\Http\HttpClientInterface; |
| 11 |
use AATXT\Config\Constants; |
| 12 |
|
| 13 |
/** |
| 14 |
* Abstract base class for OpenAI-based providers. |
| 15 |
* |
| 16 |
* Implements SupportsImageValidation and RequiresAuthentication interfaces |
| 17 |
* following the Interface Segregation Principle. |
| 18 |
*/ |
| 19 |
abstract class OpenAIResponse implements AIProviderInterface, SupportsImageValidation, RequiresAuthentication |
| 20 |
{ |
| 21 |
/** |
| 22 |
* @var HttpClientInterface |
| 23 |
*/ |
| 24 |
protected $httpClient; |
| 25 |
|
| 26 |
/** |
| 27 |
* @var AIProviderConfig |
| 28 |
*/ |
| 29 |
protected $config; |
| 30 |
|
| 31 |
/** |
| 32 |
* Constructor for OpenAI-based providers. |
| 33 |
* |
| 34 |
* @param HttpClientInterface $httpClient HTTP client for API calls |
| 35 |
* @param AIProviderConfig $config Configuration object with API key, prompt, and model |
| 36 |
*/ |
| 37 |
public function __construct(HttpClientInterface $httpClient, AIProviderConfig $config) |
| 38 |
{ |
| 39 |
$this->httpClient = $httpClient; |
| 40 |
$this->config = $config; |
| 41 |
} |
| 42 |
|
| 43 |
abstract public function response(string $imageUrl): string; |
| 44 |
|
| 45 |
/** |
| 46 |
* Get the list of supported MIME types for OpenAI Vision. |
| 47 |
* |
| 48 |
* @return array<string> List of supported MIME types |
| 49 |
*/ |
| 50 |
public function getSupportedMimeTypes(): array |
| 51 |
{ |
| 52 |
return Constants::AATXT_OPENAI_ALLOWED_MIME_TYPES; |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Check if a specific MIME type is supported. |
| 57 |
* |
| 58 |
* @param string $mimeType The MIME type to check |
| 59 |
* @return bool True if supported, false otherwise |
| 60 |
*/ |
| 61 |
public function supportsImage(string $mimeType): bool |
| 62 |
{ |
| 63 |
return in_array($mimeType, $this->getSupportedMimeTypes(), true); |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Validate that valid credentials are configured. |
| 68 |
* |
| 69 |
* @return bool True if credentials are valid |
| 70 |
*/ |
| 71 |
public function validateCredentials(): bool |
| 72 |
{ |
| 73 |
$apiKey = $this->config->getApiKey(); |
| 74 |
return !empty($apiKey) && strlen($apiKey) > 10; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Check if an API key is configured. |
| 79 |
* |
| 80 |
* @return bool True if API key is set |
| 81 |
*/ |
| 82 |
public function hasApiKey(): bool |
| 83 |
{ |
| 84 |
return !empty($this->config->getApiKey()); |
| 85 |
} |
| 86 |
|
| 87 |
/** |
| 88 |
* Send the request to the OpenAI APIs and return the decoded response |
| 89 |
* |
| 90 |
* @param array $requestBody |
| 91 |
* @param string $endpoint |
| 92 |
* @return array |
| 93 |
* @throws OpenAIException |
| 94 |
*/ |
| 95 |
protected function decodedResponseBody(array $requestBody, string $endpoint): array |
| 96 |
{ |
| 97 |
$headers = [ |
| 98 |
'Authorization' => 'Bearer ' . $this->config->getApiKey(), |
| 99 |
'Content-Type' => 'application/json; charset=utf-8', |
| 100 |
]; |
| 101 |
|
| 102 |
try { |
| 103 |
$decodedBody = $this->httpClient->post($endpoint, $headers, $requestBody); |
| 104 |
} catch (\Exception $e) { |
| 105 |
throw new OpenAIException("HTTP request failed: " . $e->getMessage()); |
| 106 |
} |
| 107 |
|
| 108 |
// Check for OpenAI-specific errors in the response |
| 109 |
if (isset($decodedBody['error'])) { |
| 110 |
throw new OpenAIException( |
| 111 |
'Error type: ' . $decodedBody['error']['type'] . |
| 112 |
' - Error code: ' . $decodedBody['error']['code'] . |
| 113 |
' - ' . $decodedBody['error']['message'] |
| 114 |
); |
| 115 |
} |
| 116 |
|
| 117 |
return $decodedBody; |
| 118 |
} |
| 119 |
|
| 120 |
/** |
| 121 |
* Return the main OpenAI prompt |
| 122 |
* |
| 123 |
* @return string |
| 124 |
*/ |
| 125 |
protected function prompt(): string |
| 126 |
{ |
| 127 |
$prompt = $this->config->getPrompt(); |
| 128 |
return !empty($prompt) ? $prompt : Constants::AATXT_OPENAI_DEFAULT_PROMPT; |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Clean response string from unwanted characters. |
| 133 |
* |
| 134 |
* @param string $text |
| 135 |
* @return string |
| 136 |
*/ |
| 137 |
protected function cleanString(string $text): string |
| 138 |
{ |
| 139 |
$patterns = array( |
| 140 |
'/\"/', // Double quotes |
| 141 |
'/\s\s+/', // Double or more consecutive white spaces |
| 142 |
'/"/' // HTML sequence for double quotes |
| 143 |
); |
| 144 |
|
| 145 |
return trim(preg_replace($patterns, '', $text)); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Prepare the request body for OpenAI API. |
| 150 |
* |
| 151 |
* @param string $model |
| 152 |
* @param string $prompt |
| 153 |
* @param string $imageUrl |
| 154 |
* @return array |
| 155 |
*/ |
| 156 |
protected function prepareRequestBody(string $model, string $prompt, string $imageUrl): array |
| 157 |
{ |
| 158 |
return [ |
| 159 |
'model' => $model, |
| 160 |
'input' => [ |
| 161 |
[ |
| 162 |
'role' => 'user', |
| 163 |
'content' => [ |
| 164 |
[ |
| 165 |
"type" => "input_text", |
| 166 |
"text" => $prompt |
| 167 |
], |
| 168 |
[ |
| 169 |
"type" => "input_image", |
| 170 |
"image_url" => $imageUrl |
| 171 |
] |
| 172 |
], |
| 173 |
] |
| 174 |
], |
| 175 |
]; |
| 176 |
} |
| 177 |
} |
| 178 |
|