PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.8.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.8.2
4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 3.5.2 All 199 releases
betterdocs / includes / AI / Providers / OpenAIProvider.php

OpenAIProvider.php in BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot 4.8.2, at includes/AI/Providers/OpenAIProvider.php

68 lines 2.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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