usingOpenAI;usingvarclient=CustomProviders.DeepInfra(apiKey);varenumerable=api.Chat.CreateChatCompletionAsStreamAsync(model:"meta-llama/Meta-Llama-3-8B-Instruct",messages:["What is the capital of the United States?"]);awaitforeach(varresponseinenumerable){Console.Write(response.Choices[0].Delta.Content);}
Microsoft.Extensions.AI (MEAI) Support
DeepInfra provides an OpenAI-compatible API. For IChatClient and IEmbeddingGenerator support via Microsoft.Extensions.AI, use the tryAGI.OpenAI package:
usingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([new Meai.ChatMessage(Meai.ChatRole.User, "Say hello in exactly 3 words.")],newMeai.ChatOptions{ModelId=DeepInfraModel});vartext=response.Messages[0].Text;Console.WriteLine(text);
Chat Client Get Streaming Response Async
1 2 3 4 5 6 7 8 910111213141516
usingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;varupdates=newList<Meai.ChatResponseUpdate>();awaitforeach(varupdateinchatClient.GetStreamingResponseAsync([new Meai.ChatMessage(Meai.ChatRole.User, "Count from 1 to 5.")],newMeai.ChatOptions{ModelId=DeepInfraModel})){updates.Add(update);vartext=string.Concat(update.Contents.OfType<Meai.TextContent>().Select(c=>c.Text));if(!string.IsNullOrEmpty(text)){Console.Write(text);}}Console.WriteLine();
usingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;vartool=Meai.AIFunctionFactory.Create((stringcity)=>cityswitch{"Paris"=>"22°C, sunny","London"=>"15°C, cloudy",_=>"Unknown",},name:"GetWeather",description:"Gets the current weather for a city");varchatOptions=newMeai.ChatOptions{ModelId=DeepInfraModel,Tools=[tool],};varmessages=newList<Meai.ChatMessage>{new(Meai.ChatRole.User,"What's the weather in Paris? Respond with the temperature only."),};// First turn — get tool callvarresponse=awaitchatClient.GetResponseAsync((IEnumerable<Meai.ChatMessage>)messages,chatOptions);varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<Meai.FunctionCallContent>().First();// Execute tool and add resultvartoolResult=awaittool.InvokeAsync(functionCall.Argumentsis{}args?newMeai.AIFunctionArguments(args):null);messages.AddRange(response.Messages);messages.Add(newMeai.ChatMessage(Meai.ChatRole.Tool,newMeai.AIContent[]{newMeai.FunctionResultContent(functionCall.CallId,toolResult),}));// Second turn — get final responsevarfinalResponse=awaitchatClient.GetResponseAsync((IEnumerable<Meai.ChatMessage>)messages,chatOptions);vartext=finalResponse.Messages[0].Text;Console.WriteLine($"Final response: {text}");
usingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;vartool=Meai.AIFunctionFactory.Create((stringcity)=>cityswitch{"Paris"=>"22°C, sunny","London"=>"15°C, cloudy",_=>"Unknown",},name:"GetWeather",description:"Gets the current weather for a city");varresponse=awaitchatClient.GetResponseAsync([new Meai.ChatMessage(Meai.ChatRole.User, "What's the weather in Paris?")],newMeai.ChatOptions{ModelId=DeepInfraModel,Tools=[tool],});varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<Meai.FunctionCallContent>().FirstOrDefault();Console.WriteLine($"Tool call: {functionCall.Name}({string.Join(",", functionCall.Arguments?.Select(kv => $"{kv.Key}={kv.Value}") ?? [])})");
Chat Client With System Message
1 2 3 4 5 6 7 8 9101112
usingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([ new Meai.ChatMessage(Meai.ChatRole.System, "You always respond with exactly one word."), new Meai.ChatMessage(Meai.ChatRole.User, "What color is the sky?"), ],newMeai.ChatOptions{ModelId=DeepInfraModel});vartext=response.Messages[0].Text;Console.WriteLine(text);
Create Chat Completion
1 2 3 4 5 6 7 8 91011
// Use the OpenAI SDK via CustomProviders.DeepInfra() with MEAI interfaceusingvarclient=GetAuthenticatedOpenAiClient();Meai.IChatClientchatClient=client;awaitforeach(varupdateinchatClient.GetStreamingResponseAsync([new Meai.ChatMessage(Meai.ChatRole.User, "What is the capital of the United States?")],newMeai.ChatOptions{ModelId=DeepInfraModel})){vartext=string.Concat(update.Contents.OfType<Meai.TextContent>().Select(c=>c.Text));Console.Write(text);}
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.