Class XAIService
public class XAIService : OpenAICompatibleService, IAIService, IFunctionRegisterable
- Inheritance
-
XAIService
- Implements
- Inherited Members
- Extension Methods
Constructors
XAIService(string, HttpClient)
public XAIService(string apiKey, HttpClient httpClient)
Parameters
apiKeystringhttpClientHttpClient
XAIService(string, string, HttpClient)
Creates a XAIService with a specific model.
public XAIService(string apiKey, string model, HttpClient httpClient)
Parameters
apiKeystringmodelstringhttpClientHttpClient
Properties
Provider
The AI provider for this service
public override string Provider { get; }
Property Value
ReasoningEffort
Reasoning effort level for Grok reasoning models (grok-3-mini, grok-4, grok-4-1-fast). Off: No reasoning_effort parameter sent (default). Low/High: Explicit reasoning effort levels.
public GrokReasoning ReasoningEffort { get; set; }
Property Value
Methods
ApplyProviderSpecificRequestProfile(AIRequestProfile)
protected override Action ApplyProviderSpecificRequestProfile(AIRequestProfile profile)
Parameters
profileAIRequestProfile
Returns
CreateFunctionMessageRequest()
Creates HTTP request with function definitions
protected override HttpRequestMessage CreateFunctionMessageRequest()
Returns
CreateMessageRequest()
Creates the HTTP request message for the AI service
protected override HttpRequestMessage CreateMessageRequest()
Returns
ExtractFunctionCall(string)
Extracts function call from API response
protected override (string content, FunctionCall functionCall) ExtractFunctionCall(string response)
Parameters
responsestring
Returns
ExtractResponseContent(string)
Extracts the response content from the API response
protected override string ExtractResponseContent(string responseContent)
Parameters
responseContentstring
Returns
GenerateImageAsync(string, string)
xAI Grok doesn't support image generation
public override Task<byte[]> GenerateImageAsync(string prompt, string size = "1024x1024")
Parameters
Returns
GenerateImageUrlAsync(string, string)
xAI Grok doesn't support image generation
public override Task<string> GenerateImageUrlAsync(string prompt, string size = "1024x1024")
Parameters
Returns
GetCompletionAsync(Message)
public override Task<string> GetCompletionAsync(Message message)
Parameters
messageMessage
Returns
GetCompletionWithCoTAsync(string)
Gets completion with Chain of Thought prompting
public Task<string> GetCompletionWithCoTAsync(string prompt)
Parameters
promptstring
Returns
GetCompletionWithImageAsync(string, string)
xAI Grok supports vision (image inputs)
public override Task<string> GetCompletionWithImageAsync(string prompt, string imagePath)
Parameters
Returns
GetInputTokenCountAsync()
Gets the token count for the current conversation
public override Task<uint> GetInputTokenCountAsync()
Returns
GetInputTokenCountAsync(string)
Gets the token count for a specific prompt
public override Task<uint> GetInputTokenCountAsync(string prompt)
Parameters
promptstring
Returns
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
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
jsonDatastringoptionsStreamOptions
Returns
StreamParseJson(string)
Parses streaming JSON data
protected override string StreamParseJson(string jsonData)
Parameters
jsonDatastring
Returns
UseGrok4FastModel()
Switches to Grok 4.1 Fast model for speed-optimized tasks
public XAIService UseGrok4FastModel()
Returns
UseGrok4Model()
Switches to Grok 4 flagship reasoning model
public XAIService UseGrok4Model()
Returns
UseMiniModel()
Switches to Grok 3 Mini for faster, lightweight reasoning
public XAIService UseMiniModel()
Returns
WithCodeGenerationMode(string)
Sets Grok-specific parameters for code generation
public XAIService WithCodeGenerationMode(string language = "python")
Parameters
languagestring
Returns
WithGrokParameters(GrokReasoning)
Sets Grok reasoning parameters. Reasoning effort: Low or High. Supported on grok-3-mini, grok-4, grok-4-1-fast.
public XAIService WithGrokParameters(GrokReasoning reasoningEffort = GrokReasoning.High)
Parameters
reasoningEffortGrokReasoning