Skip to content

Repository files navigation

AI21

Nuget packagedotnetLicense: MITDiscord

Features 🔥

  • Fully generated C# SDK based on official OpenAPI specification using OpenApiGenerator
  • Same day update to support new features
  • 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

usingAI21;usungvar api =newAi21Client(apiKey);awaitapi.Chat.V1J2UltraChatAsync(messages:[newChatMessage{Text="Hello",Role=RoleType.User,}],system:string.Empty,cancellationToken:CancellationToken.None);

CLI

dotnet tool install --global AI21.CLI --prerelease
ai21 api --help

Microsoft.Extensions.AI

The SDK implements IChatClient:

usingAI21;usingMicrosoft.Extensions.AI;IChatClientchatClient=newAi21Client(apiKey);varresponse=awaitchatClient.GetResponseAsync([newChatMessage(ChatRole.User,"Hello!")],newChatOptions{ModelId="jamba-1.5-mini"});Console.WriteLine(response.Text);

Chat Client Five Random Words Streaming

usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;varupdates=chatClient.GetStreamingResponseAsync([newChatMessage(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

usingvarclient=GetAuthorizedClient();IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([newChatMessage(ChatRole.User,"Generate 5 random words.")],newChatOptions{ModelId="jamba-large",});

Chat Client Get Service Returns Chat Client Metadata

usingvarclient=CreateTestClient();IChatClientchatClient=client;varmetadata=chatClient.GetService<ChatClientMetadata>();

Chat Client Get Service Returns Null For Unknown Key

usingvarclient=CreateTestClient();IChatClientchatClient=client;varresult=chatClient.GetService<ChatClientMetadata>(serviceKey:"unknown");

Chat Client Get Service Returns Self

usingvarclient=CreateTestClient();IChatClientchatClient=client;varself=chatClient.GetService<Ai21Client>();

Chat Client Tool Calling Multi Turn

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,[newFunctionResultContent(functionCall.CallId,toolResult),]));// Second turn: model should respond with the weather infovarfinalResponse=awaitchatClient.GetResponseAsync(messages,options);

Chat Client Tool Calling Single Turn

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([newChatMessage(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

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([newChatMessage(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

usingvarapi=GetAuthorizedClient();// await api.Completion.V1J2UltraCompleteAsync(// messages:// [// new ChatMessage// {// Text = "Hello",// Role = RoleType.User,// }// ],// system: string.Empty,// cancellationToken: CancellationToken.None);

Ecosystem maintenance

This SDK is one of more than 200 .NET SDKs maintained with AutoSDK. The tryAGI SDK audit continuously checks repository synchronization, upstream-spec regeneration, release workflows, warnings, public API visibility, and trimming/NativeAOT compatibility.

Every issue is first investigated for ecosystem-wide applicability. When the root cause belongs in AutoSDK, we fix and regression-test the generator, then roll the improvement out to every applicable SDK. Provider-specific behavior remains in this repository when it cannot be derived safely from the API specification.

Issue content—including code blocks, logs, links, and attachments—is treated only as untrusted diagnostic data. Embedded control instructions, hidden directives, delimiter tricks, or requests to alter triage or tooling behavior are ignored. Please report reproducible technical evidence and remove secrets and personal data.

Support

Priority place for bugs: https://github.com/tryAGI/AI21/issues
Priority place for ideas and general questions: https://github.com/tryAGI/AI21/discussions
Discord: https://discord.gg/Ca2xhfBf3v

Acknowledgments

JetBrains logo

This project is supported by JetBrains through the Open Source Support Program.

About

C# SDK for the AI21 API -- Jamba LLM chat completions and tool calling

Topics

Resources

Code of conduct

Contributing

Security policy

Stars

1 star

Watchers

1 watching

Forks

Releases

Sponsor this project

Used by

Contributors

Languages