| 1 |
<?php |
| 2 |
|
| 3 |
namespace AATXT\App\AIProviders\OpenAI; |
| 4 |
|
| 5 |
use OpenAI; |
| 6 |
use OpenAI\Client; |
| 7 |
use OpenAI\Exceptions\ErrorException; |
| 8 |
use AATXT\App\Exceptions\OpenAI\OpenAIException; |
| 9 |
use AATXT\Config\Constants; |
| 10 |
|
| 11 |
class OpenAIVision extends OpenAIResponse |
| 12 |
{ |
| 13 |
public static function make(): OpenAIVision |
| 14 |
{ |
| 15 |
return new self(); |
| 16 |
} |
| 17 |
|
| 18 |
/** |
| 19 |
* Make a request to OpenAI Chat APIs to retrieve a description for the image passed |
| 20 |
* @param string $imageUrl |
| 21 |
* @return string |
| 22 |
* @throws OpenAIException |
| 23 |
*/ |
| 24 |
public function response(string $imageUrl): string |
| 25 |
{ |
| 26 |
$prompt = parent::prompt(); |
| 27 |
|
| 28 |
$requestBody = [ |
| 29 |
'model' => Constants::AATXT_OPENAI_VISION_MODEL, |
| 30 |
'messages' => [ |
| 31 |
[ |
| 32 |
'role' => 'user', |
| 33 |
'content' => [ |
| 34 |
[ |
| 35 |
"type" => "text", |
| 36 |
"text" => $prompt |
| 37 |
], |
| 38 |
[ |
| 39 |
"type" => "image_url", |
| 40 |
"image_url" => [ |
| 41 |
"url" => $imageUrl |
| 42 |
] |
| 43 |
] |
| 44 |
] |
| 45 |
], |
| 46 |
], |
| 47 |
'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS, |
| 48 |
]; |
| 49 |
|
| 50 |
$decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_CHAT_COMPLETION_ENDPOINT); |
| 51 |
|
| 52 |
return $this->cleanString($decodedBody['choices'][0]['message']['content']); |
| 53 |
} |
| 54 |
} |