Table of Contents

Class OpenAIService

Namespace
Mythosia.AI.Services.OpenAI
Assembly
Mythosia.AI.dll
public class OpenAIService : OpenAICompatibleService, IAIService, IFunctionRegisterable, IImageGenerationService
Inheritance
OpenAIService
Implements
Inherited Members
Extension Methods

Constructors

OpenAIService(string, HttpClient)

public OpenAIService(string apiKey, HttpClient httpClient)

Parameters

apiKey string
httpClient HttpClient

OpenAIService(string, string, HttpClient)

Creates a OpenAIService with a specific model.

public OpenAIService(string apiKey, string model, HttpClient httpClient)

Parameters

apiKey string
model string
httpClient HttpClient

Properties

DefaultImageModel

The image model used when a request does not specify one explicitly. This is independent from the chat model exposed by Model.

public string DefaultImageModel { get; }

Property Value

string

Gpt5ReasoningEffort

GPT-5 reasoning effort level. GPT-5 defaults to Medium.

public Gpt5Reasoning Gpt5ReasoningEffort { get; set; }

Property Value

Gpt5Reasoning

Gpt5ReasoningSummary

GPT-5 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_1ReasoningEffort

GPT-5.1 reasoning effort level. GPT-5.1 defaults to None.

public Gpt5_1Reasoning Gpt5_1ReasoningEffort { get; set; }

Property Value

Gpt5_1Reasoning

Gpt5_1ReasoningSummary

GPT-5.1 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_1ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_1Verbosity

GPT-5.1 verbosity level. GPT-5.1 defaults to Medium.

public Verbosity? Gpt5_1Verbosity { get; set; }

Property Value

Verbosity?

Gpt5_2ReasoningEffort

GPT-5.2 reasoning effort level. GPT-5.2 defaults to None. GPT-5.2 Pro defaults to Medium.

public Gpt5_2Reasoning Gpt5_2ReasoningEffort { get; set; }

Property Value

Gpt5_2Reasoning

Gpt5_2ReasoningSummary

GPT-5.2 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_2ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_2Verbosity

GPT-5.2 verbosity level. GPT-5.2 defaults to Medium.

public Verbosity? Gpt5_2Verbosity { get; set; }

Property Value

Verbosity?

Gpt5_3ReasoningEffort

GPT-5.3 reasoning effort level. GPT-5.3 Codex defaults to Medium.

public Gpt5_3Reasoning Gpt5_3ReasoningEffort { get; set; }

Property Value

Gpt5_3Reasoning

Gpt5_3ReasoningSummary

GPT-5.3 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_3ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_3Verbosity

GPT-5.3 verbosity level. GPT-5.3 defaults to Medium.

public Verbosity? Gpt5_3Verbosity { get; set; }

Property Value

Verbosity?

Gpt5_4ReasoningEffort

GPT-5.4 reasoning effort level. GPT-5.4 defaults to None. GPT-5.4 Pro defaults to Medium.

public Gpt5_4Reasoning Gpt5_4ReasoningEffort { get; set; }

Property Value

Gpt5_4Reasoning

Gpt5_4ReasoningSummary

GPT-5.4 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_4ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_4Verbosity

GPT-5.4 verbosity level. GPT-5.4 defaults to Medium.

public Verbosity? Gpt5_4Verbosity { get; set; }

Property Value

Verbosity?

Gpt5_5ReasoningEffort

GPT-5.5 reasoning effort level. GPT-5.5 defaults to Medium. GPT-5.5 Pro defaults to High.

public Gpt5_5Reasoning Gpt5_5ReasoningEffort { get; set; }

Property Value

Gpt5_5Reasoning

Gpt5_5ReasoningSummary

GPT-5.5 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_5ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_5Verbosity

GPT-5.5 verbosity level. GPT-5.5 defaults to Medium.

public Verbosity? Gpt5_5Verbosity { get; set; }

Property Value

Verbosity?

Gpt5_6ReasoningEffort

GPT-5.6 reasoning effort level. The model default is Medium.

public Gpt5_6Reasoning Gpt5_6ReasoningEffort { get; set; }

Property Value

Gpt5_6Reasoning

Gpt5_6ReasoningMode

GPT-5.6 reasoning execution mode. Pro mode is an API parameter, not a separate model ID.

public Gpt5_6ReasoningMode Gpt5_6ReasoningMode { get; set; }

Property Value

Gpt5_6ReasoningMode

Gpt5_6ReasoningSummary

GPT-5.6 reasoning summary mode. Defaults to Auto. Set to null to disable reasoning summaries.

public ReasoningSummary? Gpt5_6ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Gpt5_6Verbosity

GPT-5.6 verbosity level. The model default is Medium.

public Verbosity? Gpt5_6Verbosity { get; set; }

Property Value

Verbosity?

LastReasoningSummary

Contains the reasoning summary from the last non-streaming API call when the provider returns a reasoning output item. Remains null when summaries are disabled or the provider omits the optional summary output despite accepting reasoning.summary.

public string? LastReasoningSummary { get; }

Property Value

string

O3ReasoningSummary

o3 reasoning summary mode. This is opt-in because OpenAI requires a verified organization for reasoning summaries. Leave null to request ordinary o3 reasoning without a summary.

public ReasoningSummary? O3ReasoningSummary { get; set; }

Property Value

ReasoningSummary?

Provider

The AI provider for this service

public override string Provider { get; }

Property Value

string

Methods

ApplyProviderSpecificRequestProfile(AIRequestProfile)

protected override Action ApplyProviderSpecificRequestProfile(AIRequestProfile profile)

Parameters

profile AIRequestProfile

Returns

Action

ApplyRequestProfile(AIRequestProfile)

protected override Action ApplyRequestProfile(AIRequestProfile profile)

Parameters

profile AIRequestProfile

Returns

Action

CreateFunctionMessageRequest()

Creates HTTP request with function definitions

protected override HttpRequestMessage CreateFunctionMessageRequest()

Returns

HttpRequestMessage

CreateMessageRequest()

Creates the HTTP request message for the AI service

protected override HttpRequestMessage CreateMessageRequest()

Returns

HttpRequestMessage

CreateStreamParseFailure(string, Exception, StreamDiagnostics)

Lets providers turn a malformed SSE data event into an explicit terminal error. Returning null preserves the tolerant behavior used by legacy compatible providers.

protected override StreamingContent? CreateStreamParseFailure(string jsonData, Exception exception, StreamDiagnostics diagnostics)

Parameters

jsonData string
exception Exception
diagnostics StreamDiagnostics

Returns

StreamingContent

EditImagesAsync(ImageEditRequest, CancellationToken)

Generates one or more images using existing images as references.

public Task<ImageGenerationResult> EditImagesAsync(ImageEditRequest request, CancellationToken cancellationToken = default)

Parameters

request ImageEditRequest
cancellationToken CancellationToken

Returns

Task<ImageGenerationResult>

EnrichStreamAssistantMessage(Message)

Allows a provider to attach state collected while streaming before the assistant message is added to the conversation history.

protected override void EnrichStreamAssistantMessage(Message assistantMessage)

Parameters

assistantMessage Message

ExtractFunctionCalls(string)

Extracts every function call from one API response.

protected override (string content, FunctionCallBatch functionCalls) ExtractFunctionCalls(string response)

Parameters

response string

Returns

(string content, FunctionCallBatch functionCalls)

ExtractResponseContent(string)

Extracts the response content from the API response

protected override string ExtractResponseContent(string responseContent)

Parameters

responseContent string

Returns

string

GenerateImagesAsync(ImageGenerationRequest, CancellationToken)

Generates one or more images from a text prompt.

public Task<ImageGenerationResult> GenerateImagesAsync(ImageGenerationRequest request, CancellationToken cancellationToken = default)

Parameters

request ImageGenerationRequest
cancellationToken CancellationToken

Returns

Task<ImageGenerationResult>

GetCompletionAsync(Message)

public override Task<string> GetCompletionAsync(Message message)

Parameters

message Message

Returns

Task<string>

GetCompletionWithImageAsync(string, string)

public override Task<string> GetCompletionWithImageAsync(string prompt, string imagePath)

Parameters

prompt string
imagePath string

Returns

Task<string>

GetInputTokenCountAsync()

Gets the token count for the current conversation

public override Task<uint> GetInputTokenCountAsync()

Returns

Task<uint>

GetInputTokenCountAsync(string)

Gets the token count for a specific prompt

public override Task<uint> GetInputTokenCountAsync(string prompt)

Parameters

prompt string

Returns

Task<uint>

GetModelMaxOutputTokens()

Returns the maximum output tokens allowed for the current model. Override in each service to provide model-specific limits.

protected override uint GetModelMaxOutputTokens()

Returns

uint

GetSpeechAsync(string, string, string)

Generates speech audio from text using OpenAI's TTS model

public Task<byte[]> GetSpeechAsync(string inputText, string voice = "alloy", string model = "tts-1")

Parameters

inputText string
voice string
model string

Returns

Task<byte[]>

OnStreamRoundFailed()

Lets providers discard state collected during a failed streaming round.

protected override void OnStreamRoundFailed()

OnStreamRoundStarting()

Allows a provider to reset state scoped to a single streaming round.

protected override void OnStreamRoundStarting()

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 override OpenAICompatibleService.OpenAIStreamChunk ParseStreamChunk(string jsonData, StreamOptions options)

Parameters

jsonData string
options StreamOptions

Returns

OpenAICompatibleService.OpenAIStreamChunk

ResolveRequestTimeoutSeconds(FunctionCallingPolicy)

OpenAI timeout policy. GPT-5 and slow pro reasoning workloads (legacy *-pro model IDs and GPT-5.6 reasoning.mode=pro) can legitimately take longer than the 100s default, so when the default is in effect they get longer timeouts. Explicit non-default timeouts are respected.

protected override int? ResolveRequestTimeoutSeconds(FunctionCallingPolicy policy)

Parameters

policy FunctionCallingPolicy

Returns

int?

StreamParseJson(string)

Parses streaming JSON data

protected override string StreamParseJson(string jsonData)

Parameters

jsonData string

Returns

string

TranscribeAudioAsync(byte[], string, string?)

Transcribes audio to text using OpenAI's Whisper model

public Task<string> TranscribeAudioAsync(byte[] audioData, string fileName, string? language = null)

Parameters

audioData byte[]
fileName string
language string

Returns

Task<string>

ValidateStreamTermination(bool, bool, StreamDiagnostics)

Lets providers require an explicit successful terminal event before committing a streamed assistant message or executing a collected function call.

protected override StreamingContent? ValidateStreamTermination(bool doneMarkerReceived, bool completionEventReceived, StreamDiagnostics diagnostics)

Parameters

doneMarkerReceived bool
completionEventReceived bool
diagnostics StreamDiagnostics

Returns

StreamingContent

WithGpt5Parameters(Gpt5Reasoning, ReasoningSummary?)

Sets GPT-5 specific parameters. Reasoning effort: Minimal, Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5Parameters(Gpt5Reasoning reasoningEffort = Gpt5Reasoning.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5Reasoning
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_1Parameters(Gpt5_1Reasoning, Verbosity, ReasoningSummary?)

Sets GPT-5.1 specific parameters. Reasoning effort: None (default), Low, Medium, High. Verbosity: Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5_1Parameters(Gpt5_1Reasoning reasoningEffort = Gpt5_1Reasoning.None, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5_1Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_2Parameters(Gpt5_2Reasoning, Verbosity, ReasoningSummary?)

Sets GPT-5.2 specific parameters. Reasoning effort: None (default), Low, Medium, High, XHigh. GPT-5.2 Pro supports Medium, High, XHigh. GPT-5.2 Codex supports Low, Medium (default), High, XHigh. Verbosity: Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5_2Parameters(Gpt5_2Reasoning reasoningEffort = Gpt5_2Reasoning.None, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5_2Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_3Parameters(Gpt5_3Reasoning, Verbosity, ReasoningSummary?)

Sets GPT-5.3 specific parameters. Reasoning effort: None (default for Instant), Low, Medium (default for Codex), High, XHigh. GPT-5.3 Codex supports Low, Medium (default), High, XHigh. Verbosity: Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5_3Parameters(Gpt5_3Reasoning reasoningEffort = Gpt5_3Reasoning.None, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5_3Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_4Parameters(Gpt5_4Reasoning, Verbosity, ReasoningSummary?)

Sets GPT-5.4 specific parameters. Reasoning effort: None (default), Low, Medium, High, XHigh. GPT-5.4 Pro supports Medium, High, XHigh. Verbosity: Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5_4Parameters(Gpt5_4Reasoning reasoningEffort = Gpt5_4Reasoning.None, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5_4Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_5Parameters(Gpt5_5Reasoning, Verbosity, ReasoningSummary?)

Sets GPT-5.5 specific parameters. Reasoning effort: None, Low, Medium (default), High, XHigh. GPT-5.5 Pro defaults to High. Verbosity: Low, Medium (default), High. Reasoning summary: Auto (default), Concise, Detailed, or null to disable.

public OpenAIService WithGpt5_5Parameters(Gpt5_5Reasoning reasoningEffort = Gpt5_5Reasoning.Auto, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto)

Parameters

reasoningEffort Gpt5_5Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithGpt5_6Parameters(Gpt5_6Reasoning, Verbosity, ReasoningSummary?, Gpt5_6ReasoningMode)

Sets GPT-5.6 specific parameters. Reasoning effort: None, Low, Medium (default), High, XHigh, Max. Verbosity: Low, Medium (default), High. Pro is a reasoning mode and does not change the selected model ID.

public OpenAIService WithGpt5_6Parameters(Gpt5_6Reasoning reasoningEffort = Gpt5_6Reasoning.Medium, Verbosity verbosity = Verbosity.Medium, ReasoningSummary? reasoningSummary = ReasoningSummary.Auto, Gpt5_6ReasoningMode reasoningMode = Gpt5_6ReasoningMode.Standard)

Parameters

reasoningEffort Gpt5_6Reasoning
verbosity Verbosity
reasoningSummary ReasoningSummary?
reasoningMode Gpt5_6ReasoningMode

Returns

OpenAIService

WithO3Parameters(Gpt5Reasoning, ReasoningSummary?)

Sets o3 reasoning parameters. Reasoning summaries are disabled by default and require an OpenAI organization that is verified for summary generation.

public OpenAIService WithO3Parameters(Gpt5Reasoning reasoningEffort = Gpt5Reasoning.Medium, ReasoningSummary? reasoningSummary = null)

Parameters

reasoningEffort Gpt5Reasoning
reasoningSummary ReasoningSummary?

Returns

OpenAIService

WithOpenAIParameters(float?, float?, int?)

Fine-tunes the response with specific OpenAI parameters

public OpenAIService WithOpenAIParameters(float? presencePenalty = null, float? frequencyPenalty = null, int? bestOf = null)

Parameters

presencePenalty float?
frequencyPenalty float?
bestOf int?

Returns

OpenAIService