'Bearer ' . $this->api_key ); } /** * OpenAI models scheduled for shutdown, mapped to OpenAI's recommended replacement. * * @see https://developers.openai.com/api/docs/deprecations * * @since 2.5.0 * @return array Retired model ID => replacement model ID. */ public function get_retired_models(): array { return array( 'gpt-5.1' => 'gpt-6-sol', // Shutdown April 1, 2027. 'gpt-5.4-nano' => 'gpt-6-luna', // Shutdown April 1, 2027. 'gpt-4.1-nano' => 'gpt-5.6-luna', // Shutdown October 23, 2026. 'gpt-4-turbo' => 'gpt-5.6-sol', // Shutdown October 23, 2026. 'gpt-4' => 'gpt-5.6-sol', // Shutdown October 23, 2026. 'gpt-3.5-turbo' => 'gpt-5.6-terra', // Shutdown October 23, 2026. ); } /** * Fetch available OpenAI models dynamically. * * @param bool $force Bypass the cached model list. * * @since 1.0.0 * @return array|\WP_Error List of models. */ public function get_models( bool $force = false ) { $cache_key = 'aicw_openai_models_' . md5( $this->api_key ); $cached = $force ? false : get_transient( $cache_key ); if ( false !== $cached ) { return $cached; } $response = $this->get_request( 'models' ); if ( isset( $response['error'] ) ) { return new \WP_Error( 'aicw_openai_api_error', $this->get_response_error( $response, esc_html__( 'API key not valid. Please pass a valid API key.', 'ai-content-writer' ) ) ); } if ( empty( $response['data'] ) ) { return new \WP_Error( 'aicw_openai_no_models', esc_html__( 'No models found or API key not valid. Please pass a valid API key.', 'ai-content-writer' ) ); } $models = array(); $allowed_models = array( 'gpt-6-astra', 'gpt-6.1-sol', 'gpt-6-sol', 'gpt-6-luna', 'gpt-5.6-sol', 'gpt-5.6-terra', 'gpt-5.6-luna', 'gpt-5.5', 'gpt-5.4', 'gpt-5.4-mini', 'gpt-5.2', 'gpt-5', 'gpt-5-mini', 'gpt-5-nano', 'gpt-4.1', 'gpt-4.1-mini', 'gpt-4o', ); foreach ( $response['data'] as $model ) { if ( ! isset( $model['id'] ) ) { continue; } if ( ! in_array( $model['id'], $allowed_models, true ) ) { continue; } $model_id = sanitize_text_field( $model['id'] ); // Display name is the same as ID unless you want formatting. $models[ $model_id ] = esc_html( $model_id ); } // Update the option to store the models for the settings page. update_option( 'aicw_chatgpt_models', $models ); // Cache for 24 hours. set_transient( $cache_key, $models, DAY_IN_SECONDS ); return $models; } /** * Generate text for a prompt and return the plain text output. * * @param string $prompt The user prompt. * @param array $args Optional. `model`, `system`, `max_tokens`, `temperature`, `timeout`, `json` (bool — force a JSON object response). * * @since 2.3.0 * @return string|\WP_Error The generated text or WP_Error on failure. */ public function generate( string $prompt, array $args = array() ) { if ( ! $this->is_configured() ) { return $this->not_configured_error(); } $model = ! empty( $args['model'] ) ? $this->replace_retired_model( (string) $args['model'] ) : $this->get_default_model(); $max_tokens = ! empty( $args['max_tokens'] ) ? absint( $args['max_tokens'] ) : 8000; $messages = array(); if ( ! empty( $args['system'] ) ) { $messages[] = array( 'role' => 'system', 'content' => (string) $args['system'], ); } $messages[] = array( 'role' => 'user', 'content' => $prompt, ); $data = array( 'model' => $model, 'messages' => $messages, ); // Enforce a JSON object response at the API level (JSON mode). if ( ! empty( $args['json'] ) ) { $data['response_format'] = array( 'type' => 'json_object' ); } // GPT-5 and GPT-6 family models reject the classic sampling parameters. if ( $this->is_reasoning_model( $model ) ) { $data['max_completion_tokens'] = max( $max_tokens, 4000 ); } else { $data['max_tokens'] = $max_tokens; $data['temperature'] = isset( $args['temperature'] ) ? floatval( $args['temperature'] ) : 0.7; $data['top_p'] = 1; $data['frequency_penalty'] = 0; $data['presence_penalty'] = 0; } $timeout = ! empty( $args['timeout'] ) ? absint( $args['timeout'] ) : 300; $response = $this->post_request( 'chat/completions', $data, $timeout ); if ( isset( $response['choices'][0]['message']['content'] ) ) { $content = (string) $response['choices'][0]['message']['content']; if ( '' !== trim( $content ) ) { return $content; } // An empty message is not an API error, so report why it came back blank. $finish_reason = isset( $response['choices'][0]['finish_reason'] ) ? (string) $response['choices'][0]['finish_reason'] : 'unknown'; return new \WP_Error( 'aicw_openai_empty_response', sprintf( /* translators: 1: Model ID, 2: finish_reason reported by the API. */ esc_html__( 'The %1$s model returned an empty message (finish_reason: %2$s). Try a larger token limit or a different model.', 'ai-content-writer' ), esc_html( $model ), esc_html( $finish_reason ) ) ); } return new \WP_Error( 'aicw_openai_generate_error', $this->get_response_error( $response ) ); } /** * Whether the model only supports the new-style completion parameters. * * @param string $model Model ID. * * @since 2.3.0 * @return bool */ protected function is_reasoning_model( string $model ): bool { return 0 === strpos( $model, 'gpt-5' ) || 0 === strpos( $model, 'gpt-6' ); } }