Table of Contents

Class OpenAICompatibleService

Namespace
Mythosia.AI.Services.Base
Assembly
Mythosia.AI.dll

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

apiKey string
baseUrl string
httpClient HttpClient

Methods

ParseOpenAICompatibleUsage(JsonElement)

Parses OpenAI-compatible usage JSON (handles both prompt_tokens/input_tokens variants).

protected static TokenUsage ParseOpenAICompatibleUsage(JsonElement usage)

Parameters

usage JsonElement

Returns

TokenUsage

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

jsonData string
options StreamOptions

Returns

OpenAICompatibleService.OpenAIStreamChunk

StreamCompletionAsync(Message, Func<string, Task>)

public override Task StreamCompletionAsync(Message message, Func<string, Task> messageReceivedAsync)

Parameters

message Message
messageReceivedAsync Func<string, Task>

Returns

Task

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

options StreamOptions
useFunctions bool
policy FunctionCallingPolicy
cancellationToken CancellationToken

Returns

IAsyncEnumerable<StreamingContent>