| @@ -1,93 +1,18 @@ | ||
| 1 | 1 | <?php |
| 2 | - | |
| 3 | 2 | namespace AATXT\App\AIProviders\OpenAI; |
| 4 | 3 | |
| 4 | +use AATXT\App\Admin\PluginOptions; | |
| 5 | 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 | 6 | use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 10 | -use AATXT\App\Infrastructure\Http\HttpClientInterface; | |
| 11 | 7 | use AATXT\Config\Constants; |
| 12 | 8 | |
| 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 | |
| 9 | +abstract class OpenAIResponse implements AIProviderInterface | |
| 20 | 10 | { |
| 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 | 11 | abstract public function response(string $imageUrl): string; |
| 44 | 12 | |
| 45 | 13 | /** |
| 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 | 14 | * Send the request to the OpenAI APIs and return the decoded response |
| 89 | - * | |
| 90 | 15 | * @param array $requestBody |
| 91 | 16 | * @param string $endpoint |
| 92 | 17 | * @return array |
| 93 | 18 | * @throws OpenAIException |
| @@ -93,45 +18,52 @@ | ||
| 93 | 18 | * @throws OpenAIException |
| 94 | 19 | */ |
| 95 | 20 | protected function decodedResponseBody(array $requestBody, string $endpoint): array |
| 96 | 21 | { |
| 22 | + | |
| 23 | + $apiKey = PluginOptions::apiKeyOpenAI(); | |
| 24 | + | |
| 97 | 25 | $headers = [ |
| 98 | - 'Authorization' => 'Bearer ' . $this->config->getApiKey(), | |
| 26 | + 'Authorization' => 'Bearer ' . $apiKey, | |
| 99 | 27 | 'Content-Type' => 'application/json; charset=utf-8', |
| 100 | 28 | ]; |
| 101 | 29 | |
| 102 | - try { | |
| 103 | - $decodedBody = $this->httpClient->post($endpoint, $headers, $requestBody); | |
| 104 | - } catch (\Exception $e) { | |
| 105 | - throw new OpenAIException("HTTP request failed: " . $e->getMessage()); | |
| 30 | + $args = [ | |
| 31 | + 'headers' => $headers, | |
| 32 | + 'timeout' => 90, | |
| 33 | + 'body' => json_encode($requestBody), | |
| 34 | + 'method' => 'POST', | |
| 35 | + 'data_format' => 'body', | |
| 36 | + ]; | |
| 37 | + | |
| 38 | + $response = wp_remote_post($endpoint, $args); | |
| 39 | + | |
| 40 | + if (is_wp_error($response)) { | |
| 41 | + $error_message = $response->get_error_message(); | |
| 42 | + throw new OpenAIException("Something went wrong: $error_message"); | |
| 106 | 43 | } |
| 107 | 44 | |
| 108 | - // Check for OpenAI-specific errors in the response | |
| 45 | + $responseBody = wp_remote_retrieve_body($response); | |
| 46 | + $decodedBody = json_decode($responseBody, true); | |
| 47 | + | |
| 109 | 48 | 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 | - ); | |
| 49 | + throw new OpenAIException('Error type: ' . $decodedBody['error']['type'] . ' - Error code: ' . $decodedBody['error']['code'] . ' - ' . $decodedBody['error']['message']); | |
| 115 | 50 | } |
| 116 | 51 | |
| 117 | 52 | return $decodedBody; |
| 53 | + | |
| 118 | 54 | } |
| 119 | 55 | |
| 120 | 56 | /** |
| 121 | 57 | * Return the main OpenAI prompt |
| 122 | - * | |
| 123 | 58 | * @return string |
| 124 | 59 | */ |
| 125 | 60 | protected function prompt(): string |
| 126 | 61 | { |
| 127 | - $prompt = $this->config->getPrompt(); | |
| 128 | - return !empty($prompt) ? $prompt : Constants::AATXT_OPENAI_DEFAULT_PROMPT; | |
| 62 | + return PluginOptions::openAiPrompt() ?: Constants::AATXT_OPENAI_DEFAULT_PROMPT; | |
| 129 | 63 | } |
| 130 | 64 | |
| 131 | 65 | /** |
| 132 | - * Clean response string from unwanted characters. | |
| 133 | - * | |
| 134 | 66 | * @param string $text |
| 135 | 67 | * @return string |
| 136 | 68 | */ |
| 137 | 69 | protected function cleanString(string $text): string |
| @@ -144,34 +76,29 @@ | ||
| 144 | 76 | |
| 145 | 77 | return trim(preg_replace($patterns, '', $text)); |
| 146 | 78 | } |
| 147 | 79 | |
| 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 | 80 | protected function prepareRequestBody(string $model, string $prompt, string $imageUrl): array |
| 157 | 81 | { |
| 158 | 82 | return [ |
| 159 | - 'model' => $model, | |
| 160 | - 'input' => [ | |
| 83 | + 'model' => Constants::AATXT_OPENAI_VISION_MODEL, | |
| 84 | + 'messages' => [ | |
| 161 | 85 | [ |
| 162 | 86 | 'role' => 'user', |
| 163 | 87 | 'content' => [ |
| 164 | 88 | [ |
| 165 | - "type" => "input_text", | |
| 89 | + "type" => "text", | |
| 166 | 90 | "text" => $prompt |
| 167 | 91 | ], |
| 168 | 92 | [ |
| 169 | - "type" => "input_image", | |
| 170 | - "image_url" => $imageUrl | |
| 93 | + "type" => "image_url", | |
| 94 | + "image_url" => [ | |
| 95 | + "url" => $imageUrl | |
| 96 | + ] | |
| 171 | 97 | ] |
| 172 | - ], | |
| 173 | - ] | |
| 98 | + ] | |
| 99 | + ], | |
| 174 | 100 | ], |
| 101 | + 'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS, | |
| 175 | 102 | ]; |
| 176 | 103 | } |
| 177 | -} | |
| 104 | +} | |