Ai21Client directly implements IChatClient, so you can use it with the standard MEAI interface without any adapters.
1 2 3 4 5 6 7 8 910
usingMicrosoft.Extensions.AI;usingAI21;IChatClientclient=newAi21Client(apiKey:Environment.GetEnvironmentVariable("AI21_API_KEY")!);varresponse=awaitclient.GetResponseAsync("What is the capital of France?",newChatOptions{ModelId="jamba-1.5-mini"});Console.WriteLine(response.Text);
Streaming
1 2 3 4 5 6 7 8 91011
usingMicrosoft.Extensions.AI;usingAI21;IChatClientclient=newAi21Client(apiKey:Environment.GetEnvironmentVariable("AI21_API_KEY")!);awaitforeach(varupdateinclient.GetStreamingResponseAsync("Explain quantum computing in simple terms.",newChatOptions{ModelId="jamba-1.5-mini"})){Console.Write(update.Text);}
usingMicrosoft.Extensions.AI;usingAI21;IChatClientclient=newAi21Client(apiKey:Environment.GetEnvironmentVariable("AI21_API_KEY")!);vartool=AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 22C.","GetWeather","Gets the current weather for a given location.");varoptions=newChatOptions{ModelId="jamba-1.5-mini",Tools=[tool],};varmessages=newList<ChatMessage>{new(ChatRole.User,"What is the weather in London?"),};// The loop handles automatic tool invocationwhile(true){varresponse=awaitclient.GetResponseAsync(messages,options);messages.AddRange(response.ToChatMessages());if(response.FinishReason==ChatFinishReason.ToolCalls){varresults=awaitresponse.CallToolsAsync(options);messages.AddRange(results);continue;}Console.WriteLine(response.Text);break;}
Reasoning
AI21 Jamba models support reasoning content. When reasoning is present, it is returned as TextReasoningContent in the response message.
1 2 3 4 5 6 7 8 9101112131415161718192021
usingMicrosoft.Extensions.AI;usingAI21;IChatClientclient=newAi21Client(apiKey:Environment.GetEnvironmentVariable("AI21_API_KEY")!);varresponse=awaitclient.GetResponseAsync("If a train leaves at 3pm going 60mph and another leaves at 4pm going 90mph, when do they meet?",newChatOptions{ModelId="jamba-1.5-mini"});foreach(varcontentinresponse.Messages.SelectMany(m=>m.Contents)){switch(content){caseTextReasoningContentreasoning:Console.WriteLine($"Reasoning: {reasoning.Text}");break;caseTextContenttext:Console.WriteLine($"Answer: {text.Text}");break;}}