| @@ -1,34 +1,24 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace AATXT\App\AIProviders\OpenAI; |
| 4 | 4 | |
| 5 | -use AATXT\App\Configuration\AIProviderConfig; | |
| 5 | +use AATXT\App\Admin\PluginOptions; | |
| 6 | +use OpenAI; | |
| 7 | +use OpenAI\Client; | |
| 8 | +use OpenAI\Exceptions\ErrorException; | |
| 6 | 9 | use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 7 | -use AATXT\App\Infrastructure\Http\HttpClientInterface; | |
| 8 | 10 | use AATXT\Config\Constants; |
| 9 | 11 | |
| 10 | -/** | |
| 11 | - * OpenAI Vision provider for generating alt text using GPT models with vision capabilities. | |
| 12 | - * | |
| 13 | - * This class uses dependency injection to receive HTTP client and configuration, | |
| 14 | - * removing static dependencies on PluginOptions. | |
| 15 | - */ | |
| 16 | 12 | class OpenAIVision extends OpenAIResponse |
| 17 | 13 | { |
| 18 | - /** | |
| 19 | - * Constructor. | |
| 20 | - * | |
| 21 | - * @param HttpClientInterface $httpClient HTTP client for API calls | |
| 22 | - * @param AIProviderConfig $config Configuration with API key, prompt, and model | |
| 23 | - */ | |
| 24 | - public function __construct(HttpClientInterface $httpClient, AIProviderConfig $config) | |
| 14 | + public static function make(): OpenAIVision | |
| 25 | 15 | { |
| 26 | - parent::__construct($httpClient, $config); | |
| 16 | + return new self(); | |
| 27 | 17 | } |
| 28 | 18 | |
| 29 | 19 | /** |
| 30 | - * Make a request to OpenAI Chat APIs to retrieve a description for the image passed | |
| 20 | + * Make a request to OpenAI Chat APIs to retrieve a description for the image passed | |
| 31 | 21 | * @param string $imageUrl |
| 32 | 22 | * @return string |
| 33 | 23 | * @throws OpenAIException |
| 34 | 24 | */ |
| @@ -34,16 +24,9 @@ | ||
| 34 | 24 | */ |
| 35 | 25 | public function response(string $imageUrl): string |
| 36 | 26 | { |
| 37 | 27 | $prompt = parent::prompt(); |
| 38 | - $requestBody = parent::prepareRequestBody($this->config->getModel(), $prompt, $imageUrl); | |
| 39 | - $decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_RESPONSES_API_ENDPOINT); | |
| 40 | - | |
| 41 | - foreach($decodedBody['output'] as $output) { | |
| 42 | - if($output['type'] === 'message') { | |
| 43 | - return $this->cleanString($output['content'][0]['text']); | |
| 44 | - } | |
| 45 | - } | |
| 46 | - | |
| 47 | - return ''; | |
| 28 | + $requestBody = parent::prepareRequestBody(PluginOptions::openAiModel(), $prompt, $imageUrl); | |
| 29 | + $decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_CHAT_COMPLETION_ENDPOINT); | |
| 30 | + return $this->cleanString($decodedBody['choices'][0]['message']['content']); | |
| 48 | 31 | } |
| 49 | 32 | } |