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);}