awaitforeach(varupdateinchatClient.GetStreamingResponseAsync([new ChatMessage(ChatRole.User, "Count from 1 to 10.")],newChatOptions{ModelId="llama3.2"})){Console.Write(string.Concat(update.Contents.OfType<TextContent>().Select(c=>c.Text)));}
System Messages
123456789
varmessages=newList<ChatMessage>{new(ChatRole.System,"You are a helpful assistant that responds concisely."),new(ChatRole.User,"What is the capital of France?"),};varresponse=awaitchatClient.GetResponseAsync(messages,newChatOptions{ModelId="llama3.2"});
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([new ChatMessage(ChatRole.User, "What's the weather in Paris?")],newChatOptions{ModelId="llama3.2",Tools=[weatherTool],});
Image Content
1234567
varmessage=newChatMessage(ChatRole.User,[]);message.Contents.Add(newTextContent("What is in this image?"));message.Contents.Add(newDataContent(imageBytes,"image/png"));varresponse=awaitchatClient.GetResponseAsync([message],newChatOptions{ModelId="llava"});
The MEAI interface works alongside the native Ollama tools integration:
1 2 3 4 5 6 7 8 9101112131415161718192021
usingOllama;usingCSharpToJsonSchema;usingMicrosoft.Extensions.AI;[GenerateJsonSchema]publicinterfaceIWeatherFunctions{[Description("Get the current weather in a given location")]publicTask<string>GetCurrentWeatherAsync([Description("The city name")]stringlocation,CancellationTokencancellationToken=default);}// Use the native Ollama Chat abstraction with toolsvarchat=ollama.Chat(model:"llama3.2",systemMessage:"You are a helpful weather assistant.",autoCallTools:true);varservice=newWeatherService();chat.AddToolService(service.AsTools().AsOllamaTools(),service.AsCalls());