| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | * Handles interactions with the OpenAI API for LearnPress. |
| 14 | 14 | * |
| 15 | 15 | * @package LearnPress\Services |
| 16 | 16 | * @since 4.3.0 |
| 17 | - * @version 1.0.0 | |
| 17 | + * @version 1.0.1 | |
| 18 | 18 | */ |
| 19 | 19 | class OpenAiService { |
| 20 | 20 | use Singleton; |
| 21 | 21 | |
| @@ -21,9 +21,8 @@ | ||
| 21 | 21 | |
| 22 | 22 | public string $baseUrl = 'https://api.openai.com/v1/'; |
| 23 | 23 | public string $urlChartCompletion; |
| 24 | 24 | public string $urlResponses; |
| 25 | - public string $urlCompletionLegacy; | |
| 26 | 25 | public string $urlImage; |
| 27 | 26 | |
| 28 | 27 | public string $secret_key; |
| 29 | 28 | public string $text_model_type; |
| @@ -33,12 +32,11 @@ | ||
| 33 | 32 | public float $creativity_level; |
| 34 | 33 | public int $max_token; |
| 35 | 34 | |
| 36 | 35 | public function init() { |
| 37 | - $this->urlChartCompletion = $this->baseUrl . 'chat/completions'; | |
| 38 | - $this->urlCompletionLegacy = $this->baseUrl . 'completions'; | |
| 39 | - $this->urlResponses = $this->baseUrl . 'responses'; | |
| 40 | - $this->urlImage = $this->baseUrl . 'images/generations'; | |
| 36 | + $this->urlChartCompletion = $this->baseUrl . 'chat/completions'; | |
| 37 | + $this->urlResponses = $this->baseUrl . 'responses'; | |
| 38 | + $this->urlImage = $this->baseUrl . 'images/generations'; | |
| 41 | 39 | $this->get_settings(); |
| 42 | 40 | } |
| 43 | 41 | |
| 44 | 42 | /** |
| @@ -61,9 +59,9 @@ | ||
| 61 | 59 | |
| 62 | 60 | public function get_settings() { |
| 63 | 61 | $this->secret_key = LP_Settings::get_option( 'open_ai_secret_key', '' ); |
| 64 | 62 | $this->text_model_type = LP_Settings::get_option( 'open_ai_text_model_type', 'gpt-4.1' ); |
| 65 | - $this->image_model_type = LP_Settings::get_option( 'open_ai_image_model_type', 'dall-e-3' ); | |
| 63 | + $this->image_model_type = LP_Settings::get_option( 'open_ai_image_model_type', 'gpt-image-1' ); | |
| 66 | 64 | $this->frequency_penalty = LP_Settings::get_option( 'open_ai_frequency_penalty_level', 0.0 ); |
| 67 | 65 | $this->presence_penalty = LP_Settings::get_option( 'open_ai_presence_penalty_level', 0.0 ); |
| 68 | 66 | $this->creativity_level = LP_Settings::get_option( 'open_ai_creativity_level', 1.0 ); |
| 69 | 67 | $this->max_token = LP_Settings::get_option( 'open_ai_max_token', 0 ); |
| @@ -74,25 +72,11 @@ | ||
| 74 | 72 | * |
| 75 | 73 | * @throws Exception |
| 76 | 74 | */ |
| 77 | 75 | public function send_request( array $args ): array { |
| 78 | - // Handle args before send request | |
| 79 | - $url = ''; | |
| 76 | + $url = $this->urlResponses; | |
| 77 | + $args = $this->handle_params_for_send_responses( $args ); | |
| 80 | 78 | |
| 81 | - if ( in_array( | |
| 82 | - $this->text_model_type, | |
| 83 | - [ 'chatgpt-4o-latest', 'gpt-4o', 'gpt-4o-mini', 'gpt-3.5-turbo' ] | |
| 84 | - ) ) { | |
| 85 | - $url = $this->urlChartCompletion; | |
| 86 | - $args = $this->handle_params_for_send_chat_completion( $args ); | |
| 87 | - } elseif ( $this->text_model_type == 'gpt-3.5-turbo-instruct' ) { | |
| 88 | - $url = $this->urlCompletionLegacy; | |
| 89 | - $args = $this->handle_params_for_send_completion_legacy( $args ); | |
| 90 | - } else { | |
| 91 | - $url = $this->urlResponses; | |
| 92 | - $args = $this->handle_params_for_send_responses( $args ); | |
| 93 | - } | |
| 94 | - | |
| 95 | 79 | $response = wp_remote_post( |
| 96 | 80 | $url, |
| 97 | 81 | [ |
| 98 | 82 | 'headers' => [ |
| @@ -184,12 +168,12 @@ | ||
| 184 | 168 | if ( $model_type === 'gpt-image-1' ) { |
| 185 | 169 | unset( $args['n'] ); |
| 186 | 170 | } |
| 187 | 171 | |
| 188 | - if ( $model_type == 'dall-e-3' ) { | |
| 172 | + /*if ( $model_type == 'dall-e-3' ) { | |
| 189 | 173 | // only n=1 is supported. |
| 190 | 174 | $args['n'] = 1; |
| 191 | - } | |
| 175 | + }*/ | |
| 192 | 176 | |
| 193 | 177 | $response = wp_remote_post( |
| 194 | 178 | $this->urlImage, |
| 195 | 179 | [ |
| @@ -270,31 +254,8 @@ | ||
| 270 | 254 | unset( $result['max_completion_tokens'] ); |
| 271 | 255 | } |
| 272 | 256 | |
| 273 | 257 | return $result; |
| 274 | - } | |
| 275 | - | |
| 276 | - /** | |
| 277 | - * Handle params for send chat completion | |
| 278 | - * | |
| 279 | - * @docs https://platform.openai.com/docs/api-reference/completions/create | |
| 280 | - * | |
| 281 | - * @throws Exception | |
| 282 | - */ | |
| 283 | - public function handle_params_for_send_completion_legacy( $params ): array { | |
| 284 | - $params = [ | |
| 285 | - 'model' => $this->text_model_type, | |
| 286 | - 'temperature' => $this->creativity_level, | |
| 287 | - 'max_tokens' => $this->max_token, | |
| 288 | - 'n' => $params['outputs'] ?? 1, | |
| 289 | - 'prompt' => $params['prompt'] ?? '', | |
| 290 | - ]; | |
| 291 | - | |
| 292 | - if ( $this->max_token === 0 ) { | |
| 293 | - unset( $params['max_tokens'] ); | |
| 294 | - } | |
| 295 | - | |
| 296 | - return $params; | |
| 297 | 258 | } |
| 298 | 259 | |
| 299 | 260 | /** |
| 300 | 261 | * Handle params for send chat completion |