| 1 |
<?php |
| 2 |
namespace AATXT\App\AIProviders\OpenAI; |
| 3 |
|
| 4 |
use OpenAI; |
| 5 |
use OpenAI\Client; |
| 6 |
use AATXT\App\Admin\PluginOptions; |
| 7 |
use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 8 |
use AATXT\Config\Constants; |
| 9 |
|
| 10 |
class OpenAITextCompletionResponse extends OpenAIResponse |
| 11 |
{ |
| 12 |
private function __construct() { |
| 13 |
|
| 14 |
} |
| 15 |
|
| 16 |
public static function make(): OpenAITextCompletionResponse |
| 17 |
{ |
| 18 |
return new self(); |
| 19 |
} |
| 20 |
|
| 21 |
/** |
| 22 |
* Make a request to OpenAI Text Completion APIs to retrieve a description for the image passed |
| 23 |
* @param string $imageUrl |
| 24 |
* @return string |
| 25 |
* @throws OpenAIException |
| 26 |
*/ |
| 27 |
public function response(string $imageUrl): string |
| 28 |
{ |
| 29 |
$prompt = parent::prompt(); |
| 30 |
$model = PluginOptions::model(); |
| 31 |
|
| 32 |
$requestBody = [ |
| 33 |
'model' => $model, |
| 34 |
'prompt' => $prompt, |
| 35 |
'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS, |
| 36 |
'temperature' => Constants::AATXT_OPENAI_TEXT_COMPLETION_TEMPERATURE, |
| 37 |
]; |
| 38 |
|
| 39 |
$decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_TEXT_COMPLETION_ENDPOINT); |
| 40 |
|
| 41 |
return $this->cleanString($decodedBody['choices'][0]['text']); |
| 42 |
} |
| 43 |
} |