Table of Contents

Class XAIService

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

Constructors

XAIService(string, HttpClient)

public XAIService(string apiKey, HttpClient httpClient)

Parameters

apiKey string
httpClient HttpClient

XAIService(string, string, HttpClient)

Creates a XAIService with a specific model.

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

Parameters

apiKey string
model string
httpClient HttpClient

Properties

Provider

The AI provider for this service

public override string Provider { get; }

Property Value

string

ReasoningEffort

Reasoning effort for configurable Grok models. Auto leaves the provider default intact. Grok 4.3 accepts None, Low, Medium, and High; Grok 4.5 accepts Low, Medium, and High.

public GrokReasoning ReasoningEffort { get; set; }

Property Value

GrokReasoning

Methods

ApplyProviderSpecificRequestProfile(AIRequestProfile)

protected override Action ApplyProviderSpecificRequestProfile(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

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

GetCompletionAsync(Message)

public override Task<string> GetCompletionAsync(Message message)

Parameters

message Message

Returns

Task<string>

GetCompletionWithCoTAsync(string)

Gets completion with Chain of Thought prompting

public Task<string> GetCompletionWithCoTAsync(string prompt)

Parameters

prompt string

Returns

Task<string>

GetCompletionWithImageAsync(string, string)

xAI Grok supports vision (image inputs)

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

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

StreamParseJson(string)

Parses streaming JSON data

protected override string StreamParseJson(string jsonData)

Parameters

jsonData string

Returns

string

UseGrok4FastModel()

Switches to Grok 4.3 for fast general-purpose workloads.

public XAIService UseGrok4FastModel()

Returns

XAIService

UseGrok4Model()

Switches to the current Grok flagship reasoning model (grok-4.5).

public XAIService UseGrok4Model()

Returns

XAIService

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

WithCodeGenerationMode(string)

Sets Grok-specific parameters for code generation

public XAIService WithCodeGenerationMode(string language = "python")

Parameters

language string

Returns

XAIService

WithGrokParameters(GrokReasoning)

Sets the model-specific Grok reasoning effort.

public XAIService WithGrokParameters(GrokReasoning reasoningEffort = GrokReasoning.High)

Parameters

reasoningEffort GrokReasoning

Returns

XAIService