value ); if ('chat/completions' === $path && is_array($data)) { // Context Length configures the model's context window, not the output length, // so it must not be sent as `max_tokens` (which caps output tokens and would make // strict servers reject the request). `num_ctx` is passed through best-effort for // servers that read it directly from the request body; it's a no-op elsewhere. $context_length = (int) get_option('connectors_ai_openai_compatible_servers_context_length', 0); if ($context_length > 0 && !isset($data['num_ctx'])) { $data['num_ctx'] = $context_length; } // Only send the two broadly-recognized thinking-control conventions (Qwen/vLLM's // `thinking` flag and OpenAI's `reasoning_effort`). `max_thinking_tokens` is dropped: // it's the least standard of the three and the most likely to trip strict servers' // "unknown field" validation, without adding anything `thinking: false` doesn't // already convey. $disable_thinking = (bool) get_option('connectors_ai_openai_compatible_servers_disable_thinking', false); if ($disable_thinking) { $data['thinking'] = false; $data['reasoning_effort'] = 'low'; $data['chat_template_kwargs'] = [ 'enable_thinking' => false, ]; } // Normalize `response_format` structure for OpenAI-compatibility. // If the client sent the raw JSON schema directly inside `json_schema` instead // of wrapping it in `schema` and specifying a `name` parameter, wrap it properly. if (isset($data['response_format']) && is_array($data['response_format'])) { if (isset($data['response_format']['type']) && 'json_schema' === $data['response_format']['type']) { if ( isset($data['response_format']['json_schema']) && is_array($data['response_format']['json_schema']) ) { $json_schema = $data['response_format']['json_schema']; if (!isset($json_schema['schema']) && isset($json_schema['type'])) { $name = isset($json_schema['name']) ? $json_schema['name'] : 'response_schema'; $name = preg_replace('/[^a-zA-Z0-9_-]/', '_', $name); if (strlen($name) > 64) { $name = substr($name, 0, 64); } if ('' === $name) { $name = 'response_schema'; } $schema = $json_schema; unset($schema['name'], $schema['strict'], $schema['description']); $normalized_schema = [ 'name' => $name, 'schema' => $schema, ]; if (isset($json_schema['strict'])) { $normalized_schema['strict'] = (bool) $json_schema['strict']; } if (isset($json_schema['description'])) { $normalized_schema['description'] = (string) $json_schema['description']; } $data['response_format']['json_schema'] = $normalized_schema; } } } } /** * Filters the completions request data before it is sent to the inference server. * * @since 1.0.0 * * @param array $data The request payload data array. * @param string $modelId The model identifier (e.g. qwen3.6-27b). */ $data = apply_filters('connectors_ai_openai_compatible_servers_request_data', $data, $this->modelId); } $request_options = $this->getRequestOptions(); if (null === $request_options) { $request_options = new RequestOptions(); $request_options->setTimeout(120.0); $this->setRequestOptions($request_options); } else { $timeout = $request_options->getTimeout(); if (null === $timeout || $timeout < 120.0) { $request_options->setTimeout(120.0); } } return new Request( $method, OpenAiCompatibleServersProvider::url($path), $headers, $data, $request_options ); } /** * {@inheritDoc} * * @since 1.0.0 */ protected function prepareMessagesParam(array $messages, ?string $systemInstruction = null): array { $enable_r1 = (bool) get_option('connectors_ai_openai_compatible_servers_enable_r1_format', false); if ($enable_r1 && $systemInstruction) { // Prepend system prompt to the first user message instead of sending it with the 'system' role. if (!empty($messages)) { $first_message = $messages[0]; $parts = $first_message->getParts(); if (!empty($parts) && $parts[0]->getType()->isText()) { $original_text = $parts[0]->getText(); $new_text = $systemInstruction . "\n\n" . $original_text; // Reconstruct message part and update the first message. $new_part = new \WordPress\AiClient\Messages\DTO\MessagePart($new_text); $new_parts = array_merge([$new_part], array_slice($parts, 1)); $messages[0] = new \WordPress\AiClient\Messages\DTO\Message($first_message->getRole(), $new_parts); } } $systemInstruction = null; } return parent::prepareMessagesParam($messages, $systemInstruction); } }