PluginProbe
Auto Alt Text / 1.3.2
Auto Alt Text v1.3.2
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 / OpenAIChatCompletionResponse.php

OpenAIChatCompletionResponse.php in Auto Alt Text 1.3.2, at src/App/AIProviders/OpenAI/OpenAIChatCompletionResponse.php

44 lines 1.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace AATXT\App\AIProviders\OpenAI;
3
4 use OpenAI;
5 use OpenAI\Client;
6 use OpenAI\Exceptions\ErrorException;
7 use AATXT\App\Admin\PluginOptions;
8 use AATXT\App\Exceptions\OpenAI\OpenAIException;
9 use AATXT\Config\Constants;
10
11 class OpenAIChatCompletionResponse extends OpenAIResponse
12 {
13 public static function make(): OpenAIChatCompletionResponse
14 {
15 return new self();
16 }
17
18 /**
19 * Make a request to OpenAI Chat APIs to retrieve a description for the image file name passed
20 * @param string $imageUrl
21 * @return string
22 * @throws OpenAIException
23 */
24 public function response(string $imageUrl): string
25 {
26 $model = PluginOptions::model();
27 $prompt = parent::fallbackPrompt($imageUrl);
28
29 $requestBody = [
30 'model' => $model,
31 'messages' => [
32 [
33 'role' => 'user',
34 'content' => $prompt
35 ],
36 ],
37 'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS,
38 ];
39
40 $decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_CHAT_COMPLETION_ENDPOINT);
41
42 return $this->cleanString($decodedBody['choices'][0]['message']['content']);
43 }
44 }