# auto-alt-text/1.3.2/src/App/AIProviders/OpenAI/OpenAIChatCompletionResponse.php

Auto Alt Text, version 1.3.2. 44 lines.

- Page: https://pluginprobe.com/plugins/auto-alt-text/1.3.2/code/src/App/AIProviders/OpenAI/OpenAIChatCompletionResponse.php
- Raw: https://pluginprobe.com/plugins/auto-alt-text/1.3.2/raw/src/App/AIProviders/OpenAI/OpenAIChatCompletionResponse.php
- Modified: 2024-03-10T14:55:34+00:00

Line numbers below start at 1. Link to a line or a range by appending a fragment to the
page URL, for example `https://pluginprobe.com/plugins/auto-alt-text/1.3.2/code/src/App/AIProviders/OpenAI/OpenAIChatCompletionResponse.php#L10-L20`.

```php
<?php
namespace AATXT\App\AIProviders\OpenAI;

use OpenAI;
use OpenAI\Client;
use OpenAI\Exceptions\ErrorException;
use AATXT\App\Admin\PluginOptions;
use AATXT\App\Exceptions\OpenAI\OpenAIException;
use AATXT\Config\Constants;

class OpenAIChatCompletionResponse extends OpenAIResponse
{
    public static function make(): OpenAIChatCompletionResponse
    {
        return new self();
    }

    /**
     *  Make a request to OpenAI Chat APIs to retrieve a description for the image file name passed
     * @param string $imageUrl
     * @return string
     * @throws OpenAIException
     */
    public function response(string $imageUrl): string
    {
        $model = PluginOptions::model();
        $prompt = parent::fallbackPrompt($imageUrl);

        $requestBody = [
            'model' => $model,
            'messages' => [
                [
                    'role' => 'user',
                    'content' => $prompt
                ],
            ],
            'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS,
        ];

        $decodedBody = parent::decodedResponseBody($requestBody, Constants::AATXT_OPENAI_CHAT_COMPLETION_ENDPOINT);

        return $this->cleanString($decodedBody['choices'][0]['message']['content']);
    }
}
```
