|
| 1 | +# Microsoft.Extensions.AI Integration |
| 2 | + |
| 3 | +The AI21 SDK implements `IChatClient` from [Microsoft.Extensions.AI](https://learn.microsoft.com/en-us/dotnet/ai/microsoft-extensions-ai), providing a unified interface for chat completions with text, streaming, tool calling, and reasoning support. |
| 4 | + |
| 5 | +## Installation |
| 6 | + |
| 7 | +```bash |
| 8 | +dotnet add package tryAGI.AI21 |
| 9 | +``` |
| 10 | + |
| 11 | +## Chat Completions |
| 12 | + |
| 13 | +`Ai21Client` directly implements `IChatClient`, so you can use it with the standard MEAI interface without any adapters. |
| 14 | + |
| 15 | +```csharp |
| 16 | +using Microsoft.Extensions.AI; |
| 17 | +using AI21; |
| 18 | + |
| 19 | +IChatClient client = new Ai21Client(apiKey: Environment.GetEnvironmentVariable("AI21_API_KEY")!); |
| 20 | + |
| 21 | +var response = await client.GetResponseAsync( |
| 22 | + "What is the capital of France?", |
| 23 | + new ChatOptions { ModelId = "jamba-1.5-mini" }); |
| 24 | + |
| 25 | +Console.WriteLine(response.Text); |
| 26 | +``` |
| 27 | + |
| 28 | +## Streaming |
| 29 | + |
| 30 | +```csharp |
| 31 | +using Microsoft.Extensions.AI; |
| 32 | +using AI21; |
| 33 | + |
| 34 | +IChatClient client = new Ai21Client(apiKey: Environment.GetEnvironmentVariable("AI21_API_KEY")!); |
| 35 | + |
| 36 | +await foreach (var update in client.GetStreamingResponseAsync( |
| 37 | + "Explain quantum computing in simple terms.", |
| 38 | + new ChatOptions { ModelId = "jamba-1.5-mini" })) |
| 39 | +{ |
| 40 | + Console.Write(update.Text); |
| 41 | +} |
| 42 | +``` |
| 43 | + |
| 44 | +## Tool Calling |
| 45 | + |
| 46 | +```csharp |
| 47 | +using Microsoft.Extensions.AI; |
| 48 | +using AI21; |
| 49 | + |
| 50 | +IChatClient client = new Ai21Client(apiKey: Environment.GetEnvironmentVariable("AI21_API_KEY")!); |
| 51 | + |
| 52 | +var tool = AIFunctionFactory.Create( |
| 53 | + (string location) => $"The weather in {location} is sunny, 22C.", |
| 54 | + "GetWeather", |
| 55 | + "Gets the current weather for a given location."); |
| 56 | + |
| 57 | +var options = new ChatOptions |
| 58 | +{ |
| 59 | + ModelId = "jamba-1.5-mini", |
| 60 | + Tools = [tool], |
| 61 | +}; |
| 62 | + |
| 63 | +var messages = new List<ChatMessage> |
| 64 | +{ |
| 65 | + new(ChatRole.User, "What is the weather in London?"), |
| 66 | +}; |
| 67 | + |
| 68 | +// The loop handles automatic tool invocation |
| 69 | +while (true) |
| 70 | +{ |
| 71 | + var response = await client.GetResponseAsync(messages, options); |
| 72 | + messages.AddRange(response.ToChatMessages()); |
| 73 | + |
| 74 | + if (response.FinishReason == ChatFinishReason.ToolCalls) |
| 75 | + { |
| 76 | + var results = await response.CallToolsAsync(options); |
| 77 | + messages.AddRange(results); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + Console.WriteLine(response.Text); |
| 82 | + break; |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +## Reasoning |
| 87 | + |
| 88 | +AI21 Jamba models support reasoning content. When reasoning is present, it is returned as `TextReasoningContent` in the response message. |
| 89 | + |
| 90 | +```csharp |
| 91 | +using Microsoft.Extensions.AI; |
| 92 | +using AI21; |
| 93 | + |
| 94 | +IChatClient client = new Ai21Client(apiKey: Environment.GetEnvironmentVariable("AI21_API_KEY")!); |
| 95 | + |
| 96 | +var response = await client.GetResponseAsync( |
| 97 | + "If a train leaves at 3pm going 60mph and another leaves at 4pm going 90mph, when do they meet?", |
| 98 | + new ChatOptions { ModelId = "jamba-1.5-mini" }); |
| 99 | + |
| 100 | +foreach (var content in response.Messages.SelectMany(m => m.Contents)) |
| 101 | +{ |
| 102 | + switch (content) |
| 103 | + { |
| 104 | + case TextReasoningContent reasoning: |
| 105 | + Console.WriteLine($"Reasoning: {reasoning.Text}"); |
| 106 | + break; |
| 107 | + case TextContent text: |
| 108 | + Console.WriteLine($"Answer: {text.Text}"); |
| 109 | + break; |
| 110 | + } |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Dependency Injection |
| 115 | + |
| 116 | +```csharp |
| 117 | +using Microsoft.Extensions.AI; |
| 118 | +using AI21; |
| 119 | + |
| 120 | +var builder = WebApplication.CreateBuilder(args); |
| 121 | + |
| 122 | +builder.Services.AddSingleton<IChatClient>( |
| 123 | + new Ai21Client(apiKey: builder.Configuration["AI21:ApiKey"]!)); |
| 124 | +``` |
| 125 | + |
| 126 | +## Provider Metadata |
| 127 | + |
| 128 | +```csharp |
| 129 | +var metadata = client.GetService<ChatClientMetadata>(); |
| 130 | +Console.WriteLine($"Provider: {metadata?.ProviderName}"); // "Ai21Client" |
| 131 | +Console.WriteLine($"Endpoint: {metadata?.ProviderUri}"); |
| 132 | +``` |
0 commit comments