Note: Use the Meai alias because the Mistral SDK has its own generated IChatClient interface.
Chat Client Five Random Words Streaming
1 2 3 4 5 6 7 8 91011121314151617181920
usingvarclient=newMistralClient(apiKey);Meai.IChatClientchatClient=client;varupdates=chatClient.GetStreamingResponseAsync([ new Meai.ChatMessage(Meai.ChatRole.User, "Generate 5 random words.") ],newMeai.ChatOptions{ModelId="mistral-small-latest",});vardeltas=newList<string>();awaitforeach(varupdateinupdates){if(!string.IsNullOrWhiteSpace(update.Text)){deltas.Add(update.Text);}}
Chat Client Five Random Words
1 2 3 4 5 6 7 8 91011
usingvarclient=newMistralClient(apiKey);Meai.IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([ new Meai.ChatMessage(Meai.ChatRole.User, "Generate 5 random words.") ],newMeai.ChatOptions{ModelId="mistral-small-latest",});
Chat Client Get Service Returns Chat Client Metadata
usingvarclient=newMistralClient(apiKey);vargetWeatherTool=Meai.AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is 72°F and sunny.",name:"get_weather",description:"Gets the current weather for a given location.");Meai.IChatClientchatClient=client;varmessages=newList<Meai.ChatMessage>{new(Meai.ChatRole.User,"What is the weather in Paris?"),};// First turn: model requests tool callvarresponse=awaitchatClient.GetResponseAsync(messages,newMeai.ChatOptions{ModelId="mistral-small-latest",Tools=[getWeatherTool],});varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<Meai.FunctionCallContent>().FirstOrDefault();// Add assistant message with function call and tool resultmessages.AddRange(response.Messages);vartoolResult=awaitgetWeatherTool.InvokeAsync(functionCall!.Argumentsis{}args?newMeai.AIFunctionArguments(args):null);messages.Add(newMeai.ChatMessage(Meai.ChatRole.Tool,[ new Meai.FunctionResultContent(functionCall.CallId, toolResult),]));// Second turn: model should produce a final text responsevarfinalResponse=awaitchatClient.GetResponseAsync(messages,newMeai.ChatOptions{ModelId="mistral-small-latest",Tools=[getWeatherTool],});
Chat Client Tool Calling Single Turn
1 2 3 4 5 6 7 8 910111213141516171819202122
usingvarclient=newMistralClient(apiKey);vargetWeatherTool=Meai.AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is 72°F and sunny.",name:"get_weather",description:"Gets the current weather for a given location.");Meai.IChatClientchatClient=client;varresponse=awaitchatClient.GetResponseAsync([ new Meai.ChatMessage(Meai.ChatRole.User, "What is the weather in Paris?") ],newMeai.ChatOptions{ModelId="mistral-small-latest",Tools=[getWeatherTool],});varfunctionCall=response.Messages.SelectMany(m=>m.Contents).OfType<Meai.FunctionCallContent>().FirstOrDefault();
Chat Client Tool Calling Streaming
1 2 3 4 5 6 7 8 91011121314151617181920212223
usingvarclient=newMistralClient(apiKey);vargetWeatherTool=Meai.AIFunctionFactory.Create((stringlocation)=>$"The weather in {location} is 72°F and sunny.",name:"get_weather",description:"Gets the current weather for a given location.");Meai.IChatClientchatClient=client;varupdates=chatClient.GetStreamingResponseAsync([ new Meai.ChatMessage(Meai.ChatRole.User, "What is the weather in Paris?") ],newMeai.ChatOptions{ModelId="mistral-small-latest",Tools=[getWeatherTool],});varfunctionCalls=newList<Meai.FunctionCallContent>();awaitforeach(varupdateinupdates){functionCalls.AddRange(update.Contents.OfType<Meai.FunctionCallContent>());}