| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\OpenAI; |
| 4 |
|
| 5 |
use AATXT\App\Configuration\AIProviderConfig; |
| 6 |
use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 7 |
use AATXT\App\Infrastructure\Http\HttpClientInterface; |
| 8 |
use AATXT\Config\Constants; |
| 9 |
|
| 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 |
class OpenAIVision extends OpenAIResponse |
| 17 |
{ |
| 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) |
| 25 |
{ |
| 26 |
parent::__construct($httpClient, $config); |
| 27 |
} |
| 28 |
|
| 29 |
/** |
| 30 |
* Make a request to OpenAI Chat APIs to retrieve a description for the image passed |
| 31 |
* @param string $imageUrl |
| 32 |
* @return string |
| 33 |
* @throws OpenAIException |
| 34 |
*/ |
| 35 |
public function response(string $imageUrl): string |
| 36 |
{ |
| 37 |
$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 ''; |
| 48 |
} |
| 49 |
} |