|
| 1 | +using System.ClientModel; |
| 2 | +using System.ClientModel.Primitives; |
| 3 | +using System.Collections.Concurrent; |
| 4 | +using System.Text.Json; |
| 5 | +using Microsoft.Extensions.AI; |
| 6 | +using OpenAI; |
| 7 | + |
| 8 | +namespace Devlooped.Extensions.AI; |
| 9 | + |
| 10 | +public class GrokClient(string apiKey, GrokClientOptions options) |
| 11 | + : OpenAIClient(new ApiKeyCredential(apiKey), options), IChatClient |
| 12 | +{ |
| 13 | + readonly GrokClientOptions clientOptions = options; |
| 14 | + readonly ConcurrentDictionary<string, IChatClient> clients = new(); |
| 15 | + |
| 16 | + public GrokClient(string apiKey) |
| 17 | + : this(apiKey, new()) |
| 18 | + { |
| 19 | + } |
| 20 | + |
| 21 | + void IDisposable.Dispose() { } |
| 22 | + object? IChatClient.GetService(Type serviceType, object? serviceKey) => default; |
| 23 | + |
| 24 | + async Task<ChatResponse> IChatClient.GetResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellation) |
| 25 | + => SetResponse(await GetClient(options).GetResponseAsync(messages, SetOptions(options), cancellation)); |
| 26 | + |
| 27 | + IAsyncEnumerable<ChatResponseUpdate> IChatClient.GetStreamingResponseAsync(IEnumerable<ChatMessage> messages, ChatOptions? options, CancellationToken cancellation) |
| 28 | + => GetClient(options).GetStreamingResponseAsync(messages, SetOptions(options), cancellation); |
| 29 | + |
| 30 | + IChatClient GetClient(ChatOptions? options) => clients.GetOrAdd( |
| 31 | + options?.ModelId ?? clientOptions.Model, |
| 32 | + model => base.GetChatClient(model).AsIChatClient()); |
| 33 | + |
| 34 | + ChatResponse SetResponse(ChatResponse response) |
| 35 | + { |
| 36 | + return response; |
| 37 | + } |
| 38 | + |
| 39 | + ChatOptions? SetOptions(ChatOptions? options) |
| 40 | + { |
| 41 | + if (options is null || options is not GrokChatOptions grok) |
| 42 | + return null; |
| 43 | + |
| 44 | + options.RawRepresentationFactory = _ => |
| 45 | + { |
| 46 | + var result = new GrokCompletionOptions |
| 47 | + { |
| 48 | + Search = grok.Search |
| 49 | + }; |
| 50 | + |
| 51 | + if (grok.ReasoningEffort != null) |
| 52 | + { |
| 53 | + result.ReasoningEffortLevel = grok.ReasoningEffort switch |
| 54 | + { |
| 55 | + GrokReasoningEffort.Low => OpenAI.Chat.ChatReasoningEffortLevel.Low, |
| 56 | + GrokReasoningEffort.High => OpenAI.Chat.ChatReasoningEffortLevel.High, |
| 57 | + _ => throw new ArgumentException($"Unsupported reasoning effort {grok.ReasoningEffort}") |
| 58 | + }; |
| 59 | + } |
| 60 | + |
| 61 | + return result; |
| 62 | + }; |
| 63 | + |
| 64 | + return options; |
| 65 | + } |
| 66 | + |
| 67 | + class SearchParameters |
| 68 | + { |
| 69 | + public GrokSearch Mode { get; set; } = GrokSearch.Auto; |
| 70 | + } |
| 71 | + |
| 72 | + class GrokCompletionOptions : OpenAI.Chat.ChatCompletionOptions |
| 73 | + { |
| 74 | + public GrokSearch Search { get; set; } = GrokSearch.Auto; |
| 75 | + |
| 76 | + protected override void JsonModelWriteCore(Utf8JsonWriter writer, ModelReaderWriterOptions options) |
| 77 | + { |
| 78 | + base.JsonModelWriteCore(writer, options); |
| 79 | + |
| 80 | + // "search_parameters": { "mode": "auto" } |
| 81 | + writer.WritePropertyName("search_parameters"); |
| 82 | + writer.WriteStartObject(); |
| 83 | + writer.WriteString("mode", Search.ToString().ToLowerInvariant()); |
| 84 | + writer.WriteEndObject(); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
0 commit comments