diff --git a/includes/Classifai/Helpers.php b/includes/Classifai/Helpers.php index 05bdee8bf..3247cbb5c 100644 --- a/includes/Classifai/Helpers.php +++ b/includes/Classifai/Helpers.php @@ -956,3 +956,23 @@ function safe_wp_remote_post( string $url, array $args = [] ) { return safe_wp_remote_request( 'POST', $url, $args ); } +/** + * Get the temperature for the request. + * + * We increase the base temperature proportionally + * to the number of results, ensuring it never exceeds 2. + * + * The goal here is to get more diverse results when + * we are requesting more results. + * + * @param float $temperature The temperature. + * @param int $results The number of results. + * @return float The temperature. + */ +function get_temperature( float $temperature, int $results = 1 ): float { + if ( 1 === $results ) { + return $temperature; + } + + return (float) min( 2.0, $temperature + ( $results / 10 ) ); +} diff --git a/includes/Classifai/Providers/Azure/OpenAI.php b/includes/Classifai/Providers/Azure/OpenAI.php index 4b4acec95..15335ba00 100644 --- a/includes/Classifai/Providers/Azure/OpenAI.php +++ b/includes/Classifai/Providers/Azure/OpenAI.php @@ -17,6 +17,7 @@ use function Classifai\get_default_prompt; use function Classifai\sanitize_number_of_responses_field; use function Classifai\safe_wp_remote_post; +use function Classifai\get_temperature; class OpenAI extends Provider { @@ -553,7 +554,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { 'classifai_azure_openai_title_request_body', [ 'messages' => $this->get_request_messages( $post_id, $prompt, $message_content ), - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'n' => absint( $args['num'] ), ], $post_id @@ -660,7 +661,7 @@ public function resize_content( int $post_id, array $args = array() ) { 'content' => '"""' . esc_html( $args['content'] ) . '"""', ], ], - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'n' => absint( $args['num'] ), ], $post_id diff --git a/includes/Classifai/Providers/OpenAI/ChatGPT.php b/includes/Classifai/Providers/OpenAI/ChatGPT.php index 41156620a..21c9373d4 100644 --- a/includes/Classifai/Providers/OpenAI/ChatGPT.php +++ b/includes/Classifai/Providers/OpenAI/ChatGPT.php @@ -21,6 +21,7 @@ use function Classifai\sanitize_number_of_responses_field; use function Classifai\get_modified_image_source_url; use function Classifai\get_largest_size_and_dimensions_image_url; +use function Classifai\get_temperature; class ChatGPT extends Provider { @@ -762,7 +763,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { [ 'model' => $this->chatgpt_model, 'messages' => $this->get_request_messages( $post_id, $prompt, $message_content ), - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'n' => absint( $args['num'] ), ], $post_id @@ -867,7 +868,7 @@ public function resize_content( int $post_id, array $args = array() ) { 'content' => '"""' . esc_html( $args['content'] ) . '"""', ], ], - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'n' => absint( $args['num'] ), ], $post_id diff --git a/includes/Classifai/Providers/XAI/Grok.php b/includes/Classifai/Providers/XAI/Grok.php index 7d6f2ba34..8a8e9e00b 100644 --- a/includes/Classifai/Providers/XAI/Grok.php +++ b/includes/Classifai/Providers/XAI/Grok.php @@ -18,6 +18,7 @@ use function Classifai\sanitize_number_of_responses_field; use function Classifai\get_modified_image_source_url; use function Classifai\get_largest_size_and_dimensions_image_url; +use function Classifai\get_temperature; class Grok extends Provider { /** @@ -628,7 +629,7 @@ public function generate_titles( int $post_id = 0, array $args = [] ) { [ 'model' => $this->get_model(), 'messages' => $this->get_request_messages( $post_id, $prompt, $message_content ), - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'stream' => false, 'n' => absint( $args['num'] ), ], @@ -733,7 +734,7 @@ public function resize_content( int $post_id, array $args = array() ) { 'content' => '"""' . esc_html( $args['content'] ) . '"""', ], ], - 'temperature' => 0.9, + 'temperature' => get_temperature( 0.9, absint( $args['num'] ) ), 'stream' => false, 'n' => absint( $args['num'] ), ], diff --git a/src/js/settings/components/provider-settings/azure-openai.js b/src/js/settings/components/provider-settings/azure-openai.js index 9a6b9b016..8280d43e5 100644 --- a/src/js/settings/components/provider-settings/azure-openai.js +++ b/src/js/settings/components/provider-settings/azure-openai.js @@ -107,6 +107,8 @@ export const AzureOpenAISettings = ( { onChange( { number_of_suggestions: value } ) diff --git a/src/js/settings/components/provider-settings/openai-chatgpt.js b/src/js/settings/components/provider-settings/openai-chatgpt.js index f1f2c8b86..9d656fc07 100644 --- a/src/js/settings/components/provider-settings/openai-chatgpt.js +++ b/src/js/settings/components/provider-settings/openai-chatgpt.js @@ -97,6 +97,8 @@ export const OpenAIChatGPTSettings = ( { isConfigured = false } ) => { onChange( { number_of_suggestions: value } ) diff --git a/src/js/settings/components/provider-settings/xai-grok.js b/src/js/settings/components/provider-settings/xai-grok.js index 1559e4dd7..cc69aa2f8 100644 --- a/src/js/settings/components/provider-settings/xai-grok.js +++ b/src/js/settings/components/provider-settings/xai-grok.js @@ -153,6 +153,8 @@ export const XAIGrokSettings = ( { isConfigured = false } ) => { onChange( { number_of_suggestions: value } )