| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPDeveloper\BetterDocs\AI\Providers; |
| 4 |
|
| 5 |
use WPDeveloper\BetterDocs\Utils\AIHelper; |
| 6 |
|
| 7 |
/** |
| 8 |
* OpenAI Chat Completions provider. |
| 9 |
* |
| 10 |
* Adds the GPT-5 family handling on top of the shared OpenAI-compatible base: |
| 11 |
* GPT-5 models require `max_completion_tokens`, reject a custom temperature, and |
| 12 |
* bill internal reasoning against the output budget — so we send a low |
| 13 |
* `reasoning_effort` to avoid empty completions. The original GPT-5 generation |
| 14 |
* accepts `minimal`; the gpt-5.x point releases (e.g. gpt-5.5) dropped it and |
| 15 |
* need `none` instead (see default_reasoning_effort()). |
| 16 |
* |
| 17 |
* @since 4.4.0 |
| 18 |
*/ |
| 19 |
class OpenAIProvider extends OpenAICompatibleProvider { |
| 20 |
|
| 21 |
public function id() { |
| 22 |
return 'openai'; |
| 23 |
} |
| 24 |
|
| 25 |
public function label() { |
| 26 |
return 'OpenAI'; |
| 27 |
} |
| 28 |
|
| 29 |
protected function base_url() { |
| 30 |
return 'https://api.openai.com/v1'; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* {@inheritDoc} |
| 35 |
*/ |
| 36 |
protected function build_payload( $model, $messages, $max_tokens, $temperature = null ) { |
| 37 |
if ( 0 === strpos( (string) $model, 'gpt-5' ) ) { |
| 38 |
return array( |
| 39 |
'model' => $model, |
| 40 |
'messages' => $messages, |
| 41 |
'max_completion_tokens' => (int) $max_tokens, |
| 42 |
'reasoning_effort' => apply_filters( 'betterdocs_openai_gpt5_reasoning_effort', $this->default_reasoning_effort( $model ), $model, $max_tokens ), |
| 43 |
); |
| 44 |
} |
| 45 |
|
| 46 |
return parent::build_payload( $model, $messages, $max_tokens, $temperature ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Default reasoning_effort for a gpt-5* model. |
| 51 |
* |
| 52 |
* The original GPT-5 generation (gpt-5, gpt-5-mini, gpt-5-nano) accepts |
| 53 |
* 'minimal'. The gpt-5.x point releases (e.g. gpt-5.5) dropped 'minimal' |
| 54 |
* from the API and only accept none|low|medium|high; sending 'minimal' |
| 55 |
* returns a 400 "Unsupported value: 'reasoning_effort'". For those we |
| 56 |
* default to 'none' — no reasoning tokens, the fastest option, leaving the |
| 57 |
* whole token budget for visible output (closest to gpt-5 'minimal' |
| 58 |
* behaviour). Override per model via the |
| 59 |
* betterdocs_openai_gpt5_reasoning_effort filter. |
| 60 |
* |
| 61 |
* @param string $model OpenAI model identifier. |
| 62 |
* @return string reasoning_effort value. |
| 63 |
*/ |
| 64 |
protected function default_reasoning_effort( $model ) { |
| 65 |
return AIHelper::is_gpt5_point_release( $model ) ? 'none' : 'minimal'; |
| 66 |
} |
| 67 |
} |
| 68 |
|