PluginProbe
Auto Alt Text / 2.5.2
Auto Alt Text v2.5.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 / OpenAIResponse.php

OpenAIResponse.php in Auto Alt Text 2.5.2, at src/App/AIProviders/OpenAI/OpenAIResponse.php

104 lines 3.1 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 AATXT\App\Admin\PluginOptions;
5 use AATXT\App\AIProviders\AIProviderInterface;
6 use AATXT\App\Exceptions\OpenAI\OpenAIException;
7 use AATXT\Config\Constants;
8
9 abstract class OpenAIResponse implements AIProviderInterface
10 {
11 abstract public function response(string $imageUrl): string;
12
13 /**
14 * Send the request to the OpenAI APIs and return the decoded response
15 * @param array $requestBody
16 * @param string $endpoint
17 * @return array
18 * @throws OpenAIException
19 */
20 protected function decodedResponseBody(array $requestBody, string $endpoint): array
21 {
22
23 $apiKey = PluginOptions::apiKeyOpenAI();
24
25 $headers = [
26 'Authorization' => 'Bearer ' . $apiKey,
27 'Content-Type' => 'application/json; charset=utf-8',
28 ];
29
30 $args = [
31 'headers' => $headers,
32 'timeout' => 90,
33 'body' => json_encode($requestBody),
34 'method' => 'POST',
35 'data_format' => 'body',
36 ];
37
38 $response = wp_remote_post($endpoint, $args);
39
40 if (is_wp_error($response)) {
41 $error_message = $response->get_error_message();
42 throw new OpenAIException("Something went wrong: $error_message");
43 }
44
45 $responseBody = wp_remote_retrieve_body($response);
46 $decodedBody = json_decode($responseBody, true);
47
48 if (isset($decodedBody['error'])) {
49 throw new OpenAIException('Error type: ' . $decodedBody['error']['type'] . ' - Error code: ' . $decodedBody['error']['code'] . ' - ' . $decodedBody['error']['message']);
50 }
51
52 return $decodedBody;
53
54 }
55
56 /**
57 * Return the main OpenAI prompt
58 * @return string
59 */
60 protected function prompt(): string
61 {
62 return PluginOptions::openAiPrompt() ?: Constants::AATXT_OPENAI_DEFAULT_PROMPT;
63 }
64
65 /**
66 * @param string $text
67 * @return string
68 */
69 protected function cleanString(string $text): string
70 {
71 $patterns = array(
72 '/\"/', // Double quotes
73 '/\s\s+/', // Double or more consecutive white spaces
74 '/&quot;/' // HTML sequence for double quotes
75 );
76
77 return trim(preg_replace($patterns, '', $text));
78 }
79
80 protected function prepareRequestBody(string $model, string $prompt, string $imageUrl): array
81 {
82 return [
83 'model' => Constants::AATXT_OPENAI_VISION_MODEL,
84 'messages' => [
85 [
86 'role' => 'user',
87 'content' => [
88 [
89 "type" => "text",
90 "text" => $prompt
91 ],
92 [
93 "type" => "image_url",
94 "image_url" => [
95 "url" => $imageUrl
96 ]
97 ]
98 ]
99 ],
100 ],
101 'max_tokens' => Constants::AATXT_OPENAI_MAX_TOKENS,
102 ];
103 }
104 }