Skip to content

Commit 155a52c

Browse files
authored
Added support for gpt-5, gpt-5-mini, and gpt-5-nano Chat Completions models.
1 parent a178f7d commit 155a52c

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

class-gwiz-gf-openai.php

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -261,16 +261,25 @@ public function init() {
261261
public function get_openai_models() {
262262
$models = array(
263263
'chat/completions' => array(
264+
'gpt-5' => array(
265+
'description' => __( 'OpenAI\'s flagship model for coding, reasoning, and agentic tasks across domains. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-5" target="_blank">More Details</a>', 'gravityforms-openai' ),
266+
),
267+
'gpt-5-mini' => array(
268+
'description' => __( 'OpenAI\'s faster, more cost-efficient version of GPT-5. It\'s great for well-defined tasks and precise prompts. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-5-mini" target="_blank">More Details</a>', 'gravityforms-openai' ),
269+
),
270+
'gpt-5-nano' => array(
271+
'description' => __( 'OpenAI\'s fastest, cheapest version of GPT-5. It\'s great for summarization and classification tasks. It\'s great for well-defined tasks and precise prompts. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-5-mini" target="_blank">More Details</a>', 'gravityforms-openai' ),
272+
),
264273
'gpt-4o' => array(
265274
'description' => __( 'OpenAI\'s fastest and most affordable flagship model. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-4o" target="_blank">More Details</a>', 'gravityforms-openai' ),
266275
),
267-
'gpt-4-turbo' => array(
276+
'gpt-4-turbo' => array(
268277
'description' => __( 'OpenAI\'s previous high-intelligence model. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-4-turbo-and-gpt-4" target="_blank">More Details</a>', 'gravityforms-openai' ),
269278
),
270-
'gpt-4o-mini' => array(
279+
'gpt-4o-mini' => array(
271280
'description' => __( 'OpenAI\'s most cost-efficient small model. Context length: 128k. <a href="https://platform.openai.com/docs/models/gpt-4o-mini" target="_blank">More Details</a>', 'gravityforms-openai' ),
272281
),
273-
'gpt-3.5-turbo' => array(
282+
'gpt-3.5-turbo' => array(
274283
'description' => __( 'Inexpensive model for simple tasks. Context length: 16k. <a href="https://platform.openai.com/docs/models/gpt-3-5-turbo" target="_blank">More Details</a>', 'gravityforms-openai' ),
275284
),
276285
),
@@ -1174,7 +1183,13 @@ public function get_text_from_response( $response ) {
11741183
return trim( rgars( $response, 'choices/0/message/content' ) );
11751184
}
11761185

1177-
return trim( rgar( $response, 'text' ) );
1186+
$text = rgar( $response, 'text', '' );
1187+
if ( ! empty( $response ) ) {
1188+
// safetly check as trim() doesn't accept null values
1189+
$text = trim( $text );
1190+
}
1191+
1192+
return $text;
11781193
}
11791194

11801195
/**
@@ -1373,11 +1388,36 @@ public function make_request( $endpoint, $body, $feed ) {
13731388

13741389
switch ( $endpoint ) {
13751390
case 'chat/completions':
1376-
$body['max_tokens'] = (float) rgar( $feed['meta'], $endpoint . '_' . 'max_tokens', $this->default_settings['chat/completions']['max_tokens'] );
1377-
$body['temperature'] = (float) rgar( $feed['meta'], $endpoint . '_' . 'temperature', $this->default_settings['chat/completions']['temperature'] );
1378-
$body['top_p'] = (float) rgar( $feed['meta'], $endpoint . '_' . 'top_p', $this->default_settings['chat/completions']['top_p'] );
1379-
$body['frequency_penalty'] = (float) rgar( $feed['meta'], $endpoint . '_' . 'frequency_penalty', $this->default_settings['chat/completions']['frequency_penalty'] );
1380-
$body['presence_penalty'] = (float) rgar( $feed['meta'], $endpoint . '_' . 'presence_penalty', $this->default_settings['chat/completions']['presence_penalty'] );
1391+
$body['max_completion_tokens'] = (float) rgar(
1392+
$feed['meta'],
1393+
$endpoint . '_max_tokens',
1394+
$this->default_settings['chat/completions']['max_tokens']
1395+
);
1396+
1397+
// temperature is deprecated in certain models, so only set if the user has set an explicit value.
1398+
$temperature = rgar( $feed['meta'], $endpoint . '_temperature' );
1399+
if ( $temperature !== null && $temperature !== '' ) {
1400+
$body['temperature'] = (float) $temperature;
1401+
}
1402+
1403+
// top_p is deprecated in certain models, so only set if the user has set an explicit value.
1404+
$top_p = rgar( $feed['meta'], $endpoint . '_top_p' );
1405+
if ( $top_p !== null && $top_p !== '' ) {
1406+
$body['top_p'] = (float) $top_p;
1407+
}
1408+
1409+
// frequency_penalty is deprecated in certain models, so only set if the user has set an explicit value.
1410+
$frequency_penalty = rgar( $feed['meta'], $endpoint . '_frequency_penalty' );
1411+
if ( $frequency_penalty !== null && $frequency_penalty !== '' ) {
1412+
$body['frequency_penalty'] = (float) $frequency_penalty;
1413+
}
1414+
1415+
// presence_penalty is deprecated in certain models, so only set if the user has set an explicit value.
1416+
$presence_penalty = rgar( $feed['meta'], $endpoint . '_presence_penalty' );
1417+
if ( $presence_penalty !== null && $presence_penalty !== '' ) {
1418+
$body['presence_penalty'] = (float) $presence_penalty;
1419+
}
1420+
13811421
break;
13821422
}
13831423

0 commit comments

Comments
 (0)