Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/AI.Tests/GrokTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public async Task GrokInvokesTools()
{ "user", "What day is today?" },
};

var chat = new GrokChatClient(Configuration["XAI_API_KEY"]!);
var chat = new GrokChatClient(Configuration["XAI_API_KEY"]!, "grok-3");

var options = new GrokChatOptions
{
Expand Down Expand Up @@ -143,15 +143,12 @@ public async Task GrokThinksHard()
var messages = new Chat()
{
{ "system", "You are an intelligent AI assistant that's an expert on financial matters." },
{ "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire?" },
{ "user", "If you have a debt of 100k and accumulate a compounding 5% debt on top of it every year, how long before you are a negative millonaire? (round up to full integer value)" },
};

var grok = new GrokClient(Configuration["XAI_API_KEY"]!)
.GetChatClient("grok-3")
.AsIChatClient()
.AsBuilder()
.UseFunctionInvocation()
.Build();
.AsIChatClient();

var options = new GrokChatOptions
{
Expand Down
11 changes: 2 additions & 9 deletions src/AI/Grok/GrokChatClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,15 @@ namespace Devlooped.Extensions.AI;
public class GrokChatClient : IChatClient
{
readonly ConcurrentDictionary<string, IChatClient> clients = new();
readonly string apiKey;
readonly string modelId;
readonly ClientPipeline pipeline;
readonly OpenAIClientOptions options;

/// <summary>
/// Initializes the client with the specified API key and the default model ID "grok-3-mini".
/// </summary>
public GrokChatClient(string apiKey) : this(apiKey, "grok-3-mini", null) { }

/// <summary>
/// Initializes the client with the specified API key, model ID, and optional OpenAI client options.
/// </summary>
public GrokChatClient(string apiKey, string modelId, OpenAIClientOptions? options = default)
{
this.apiKey = apiKey;
this.modelId = modelId;
this.options = options ?? new();
this.options.Endpoint ??= new Uri("https://api.x.ai/v1");
Expand Down Expand Up @@ -79,9 +72,9 @@ IChatClient GetChatClient(string modelId) => clients.GetOrAdd(modelId, model
{
result.ReasoningEffortLevel = grok.ReasoningEffort switch
{
ReasoningEffort.Low => OpenAI.Chat.ChatReasoningEffortLevel.Low,
ReasoningEffort.High => OpenAI.Chat.ChatReasoningEffortLevel.High,
_ => throw new ArgumentException($"Unsupported reasoning effort {grok.ReasoningEffort}")
// Grok does not support Medium, so we map it to Low too
_ => OpenAI.Chat.ChatReasoningEffortLevel.Low,
};
}

Expand Down
8 changes: 0 additions & 8 deletions src/AI/GrokExtensions.cs

This file was deleted.

Loading