// Copyright (c) Microsoft. All rights reserved. using Microsoft.SemanticKernel.ChatCompletion; using Microsoft.SemanticKernel.Connectors.OpenAI; namespace ChatCompletion; #pragma warning disable SKEXP0010 // OpenAIPromptExecutionSettings.ExtraBody is experimental. /// /// is an escape hatch that injects additional fields /// into the request body sent to the OpenAI-compatible endpoint. Use it to pass vendor-specific or preview /// parameters that are not modeled by (for example, /// Qwen3's enable_thinking flag, ChatGLM thinking modes, or NVIDIA NIM custom knobs). /// /// Key syntax: /// /// A plain key (without leading $.) is treated as a literal top-level field name. /// A key starting with $. is interpreted as a JSONPath expression and applied as a deep patch onto the request body. /// /// public class OpenAI_ChatCompletionExtraBody(ITestOutputHelper output) : BaseTest(output) { /// /// Adds a flat top-level field to the outgoing chat completion request: /// { "messages": [...], "model": "qwen-plus", "enable_thinking": false, ... } /// [Fact] public async Task FlatFieldExampleAsync() { OpenAIChatCompletionService chatCompletionService = new( TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey); var settings = new OpenAIPromptExecutionSettings { ExtraBody = new Dictionary { // Required by Qwen3 open-source models for non-streaming calls. ["enable_thinking"] = false, }, }; var chatHistory = new ChatHistory("You are a helpful assistant."); chatHistory.AddUserMessage("Who are you?"); var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings); Console.WriteLine(reply.Content); } /// /// Uses a $.-prefixed JSONPath key to surgically patch a nested field in the request body /// (for example, into thinking.enabled). This avoids having to specify the entire nested object. /// [Fact] public async Task DeepPatchExampleAsync() { OpenAIChatCompletionService chatCompletionService = new( TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey); var settings = new OpenAIPromptExecutionSettings { ExtraBody = new Dictionary { // Emits { "thinking": { "enabled": false } } in the request body. ["$.thinking.enabled"] = false, }, }; var chatHistory = new ChatHistory("You are a helpful assistant."); chatHistory.AddUserMessage("Summarize the plot of Hamlet in one sentence."); var reply = await chatCompletionService.GetChatMessageContentAsync(chatHistory, settings); Console.WriteLine(reply.Content); } }