PluginProbe
Auto Alt Text / 2.7.0
Auto Alt Text v2.7.0
3.0.3 2.8.2 1.3.1 1.3.2 2.0.0 2.1.0 2.1.1 2.2.0 2.3.0 2.3.1 2.3.2 2.3.3 2.3.4 2.4.0 2.4.1 2.4.2 2.5.0 2.5.1 2.5.2 2.5.3 2.6.0 2.6.1 2.7.0 2.8.0 2.8.1 All 28 releases
auto-alt-text / src / App / AIProviders / OpenAI / OpenAIVision.php

OpenAIVision.php in Auto Alt Text 2.7.0, at src/App/AIProviders/OpenAI/OpenAIVision.php

49 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }