Class OpenAICompatibleService
Base class for providers that follow the OpenAI-compatible streaming format (SSE with "data:" prefix, "[DONE]" terminator, choices/delta structure). Used by ChatGPT, Grok, Qwen, and other compatible providers. Providers override ParseStreamChunk(string, StreamOptions) to handle provider-specific parsing.
public abstract class OpenAICompatibleService : AIService, IAIService, IFunctionRegisterable
- Inheritance
-
OpenAICompatibleService
- Implements
- Derived
- Inherited Members
- Extension Methods
Constructors
OpenAICompatibleService(string, string, HttpClient)
protected OpenAICompatibleService(string apiKey, string baseUrl, HttpClient httpClient)
Parameters
apiKeystringbaseUrlstringhttpClientHttpClient
Methods
ParseOpenAICompatibleUsage(JsonElement)
Parses OpenAI-compatible usage JSON (handles both prompt_tokens/input_tokens variants).
protected static TokenUsage ParseOpenAICompatibleUsage(JsonElement usage)
Parameters
usageJsonElement
Returns
ParseStreamChunk(string, StreamOptions)
Parses a single SSE JSON chunk into a provider-neutral stream chunk. Each provider overrides this to handle its specific JSON format.
protected abstract OpenAICompatibleService.OpenAIStreamChunk ParseStreamChunk(string jsonData, StreamOptions options)
Parameters
jsonDatastringoptionsStreamOptions
Returns
StreamCompletionAsync(Message, Func<string, Task>)
public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)
Parameters
Returns
StreamRoundAsync(StreamOptions, bool, FunctionCallingPolicy, CancellationToken)
Executes a single streaming round: sends an HTTP request, reads the SSE stream, yields chunks, and handles function execution if detected. Yield a FunctionResult to signal the template to continue to the next round; otherwise the stream ends.
protected override IAsyncEnumerable<StreamingContent> StreamRoundAsync(StreamOptions options, bool useFunctions, FunctionCallingPolicy policy, CancellationToken cancellationToken)
Parameters
optionsStreamOptionsuseFunctionsboolpolicyFunctionCallingPolicycancellationTokenCancellationToken