awaitforeach(varupdateinchatClient.GetStreamingResponseAsync("Count from 1 to 10.",newChatOptions{ModelId="gpt-4o-mini"})){Console.Write(string.Concat(update.Contents.OfType<TextContent>().Select(c=>c.Text)));}
System Messages
123456789
varmessages=newList<ChatMessage>{new(ChatRole.System,"You are a pirate. Always respond with 'Arrr!'."),new(ChatRole.User,"Hello!"),};varresponse=awaitchatClient.GetResponseAsync(messages,newChatOptions{ModelId="gpt-4o-mini"});
Tool Calling
1 2 3 4 5 6 7 8 91011121314151617
varweatherTool=AIFunctionFactory.Create((stringcity)=>cityswitch{"Paris"=>"22C, sunny","London"=>"15C, cloudy",_=>"Unknown",},name:"GetWeather",description:"Gets the current weather for a city");varresponse=awaitchatClient.GetResponseAsync("What's the weather in Paris?",newChatOptions{ModelId="gpt-4o-mini",Tools=[weatherTool],});
Structured Output (JSON Schema)
1 2 3 4 5 6 7 8 910111213141516171819
varschema=System.Text.Json.JsonDocument.Parse("""{"type":"object","properties":{"name":{"type":"string"},"age":{"type":"integer"}},"required":["name","age"],"additionalProperties":false}""").RootElement;varresponse=awaitchatClient.GetResponseAsync("Return info about Alice who is 30.",newChatOptions{ModelId="gpt-4o-mini",ResponseFormat=newChatResponseFormatJson(schema,"person","A person object"),});
Image Content
123456789
varmessage=newChatMessage(ChatRole.User,[]);message.Contents.Add(newTextContent("What is in this image?"));message.Contents.Add(newUriContent(newUri("https://example.com/photo.png"),mediaType:"image/png"));varresponse=awaitchatClient.GetResponseAsync([message],newChatOptions{ModelId="gpt-4o-mini"});
Temperature, TopP, and Stop Sequences
123456789
varresponse=awaitchatClient.GetResponseAsync("What is 2+2?",newChatOptions{ModelId="gpt-4o-mini",Temperature=0f,TopP=1f,StopSequences=["5"],});
Additional Properties
Pass provider-specific parameters via AdditionalProperties:
usingtryAGI.OpenAI;usingMicrosoft.Extensions.AI;usingvarclient=newOpenAiClient("API_KEY");ITextToSpeechClientttsClient=client;varresponse=awaitttsClient.GetAudioAsync("Today is a wonderful day to build something people love.",newTextToSpeechOptions{ModelId="gpt-4o-mini-tts",VoiceId="coral",AudioFormat="mp3",Speed=1.05f,AdditionalProperties=new(){[OpenAiTextToSpeechPropertyNames.Instructions]="Speak in a cheerful and positive tone.",},});varaudio=response.Contents.OfType<DataContent>().Single().Data;
For low-latency playback, use streaming:
1 2 3 4 5 6 7 8 9101112131415
awaitforeach(varupdateinttsClient.GetStreamingAudioAsync("Stream this as audio chunks.",newTextToSpeechOptions{ModelId="gpt-4o-mini-tts",VoiceId="coral",AudioFormat="pcm",})){foreach(varchunkinupdate.Contents.OfType<DataContent>()){varbytes=chunk.Data.ToArray();// Send bytes to your audio output pipeline.}}
Custom Providers
All 15+ custom providers support the same MEAI interfaces:
1 2 3 4 5 6 7 8 9101112131415
usingOpenAI;usingMicrosoft.Extensions.AI;// Any CustomProvider works as IChatClient and IEmbeddingGeneratorusingvarclient=CustomProviders.Groq("API_KEY");// or: CustomProviders.DeepInfra("API_KEY")// or: CustomProviders.Together("API_KEY")// or: CustomProviders.Azure("API_KEY", "ENDPOINT")// or: CustomProviders.DeepSeek("API_KEY")// or: any other CustomProviderIChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync("Hello!",newChatOptions{ModelId="llama-3.3-70b-versatile"});