Updated and supported automatically if there are no breaking changes
All modern .NET features - nullability, trimming, NativeAOT, etc.
Support .Net Framework/.Net Standard 2.0
Microsoft.Extensions.AI IChatClient support
Usage
1 2 3 4 5 6 7 8 9101112131415
usingAI21;usungvarapi=newAi21Client(apiKey);awaitapi.Chat.V1J2UltraChatAsync(messages:[ new ChatMessage { Text = "Hello", Role = RoleType.User, } ],system:string.Empty,cancellationToken:CancellationToken.None);
usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;varupdates=chatClient.GetStreamingResponseAsync([ new ChatMessage(ChatRole.User, "Generate 5 random words.") ],newChatOptions{ModelId="jamba-large",});vardeltas=newList<string>();awaitforeach(varupdateinupdates){if(!string.IsNullOrWhiteSpace(update.Text)){deltas.Add(update.Text);}}
Chat Client Five Random Words
1 2 3 4 5 6 7 8 91011
usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([ new ChatMessage(ChatRole.User, "Generate 5 random words.") ],newChatOptions{ModelId="jamba-large",});
Chat Client Get Service Returns Chat Client Metadata
usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;vargetWeatherTool=AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 72°F","GetWeather","Gets the current weather for a location");varmessages=newList<ChatMessage>{new(ChatRole.User,"What's the weather in Seattle?"),};varoptions=newChatOptions{ModelId="jamba-large",Tools=[getWeatherTool],};// First turn: model should call the toolvarresponse=awaitchatClient.GetResponseAsync(messages,options);varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<FunctionCallContent>().FirstOrDefault();// Add assistant response with tool call and tool resultmessages.AddRange(response.Messages);vartoolResult=awaitgetWeatherTool.InvokeAsync(functionCall!.Argumentsis{}args?newAIFunctionArguments(args):null);messages.Add(newChatMessage(ChatRole.Tool,[ new FunctionResultContent(functionCall.CallId, toolResult),]));// Second turn: model should respond with the weather infovarfinalResponse=awaitchatClient.GetResponseAsync(messages,options);
Chat Client Tool Calling Single Turn
1 2 3 4 5 6 7 8 91011121314151617181920
usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;vargetWeatherTool=AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 72°F","GetWeather","Gets the current weather for a location");varresponse=awaitchatClient.GetResponseAsync([new ChatMessage(ChatRole.User, "What's the weather in Seattle?")],newChatOptions{ModelId="jamba-large",Tools=[getWeatherTool],});varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<FunctionCallContent>().FirstOrDefault();
Chat Client Tool Calling Streaming
1 2 3 4 5 6 7 8 9101112131415161718192021
usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;vargetWeatherTool=AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is sunny, 72°F","GetWeather","Gets the current weather for a location");varupdates=chatClient.GetStreamingResponseAsync([new ChatMessage(ChatRole.User, "What's the weather in Seattle?")],newChatOptions{ModelId="jamba-large",Tools=[getWeatherTool],});varfunctionCalls=newList<FunctionCallContent>();awaitforeach(varupdateinupdates){functionCalls.AddRange(update.Contents.OfType<FunctionCallContent>());}
Test
1 2 3 4 5 6 7 8 910111213
usingvarapi=GetAuthorizedClient();// await api.Completion.V1J2UltraCompleteAsync(// messages:// [// new ChatMessage// {// Text = "Hello",// Role = RoleType.User,// }// ],// system: string.Empty,// cancellationToken: CancellationToken.None);