| 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 |
} |