Use tool in MistralChatAgent
The following example shows how to enable tool support in MistralClientAgent by creating a GetWeatherAsync function and passing it to the agent.
Firstly, you need to install the following packages:
dotnet add package AutoGen.Mistral
dotnet add package AutoGen.SourceGenerator
Note
Tool support is only available in some mistral models. Please refer to the link for tool call support in mistral models.
Note
The AutoGen.SourceGenerator package carries a source generator that adds support for type-safe function definition generation. For more information, please check out Create type-safe function.
Note
If you are using VSCode as your editor, you may need to restart the editor to see the generated code.
Import the required namespace
using AutoGen.Core;
using AutoGen.Mistral;
using AutoGen.Mistral.Extension;
using FluentAssertions;
Then define a public partial MistralAgentFunction class and GetWeather method. The GetWeather method is a simple function that returns the weather of a given location that marked with FunctionAttribute. Marking the class as public partial together with the FunctionAttribute attribute allows the source generator to generate the FunctionContract for the GetWeather method.
public partial class MistralAgentFunction
{
[Function]
public async Task<string> GetWeather(string location)
{
return "The weather in " + location + " is sunny.";
}
}
Then create an MistralClientAgent and register it with RegisterMessageConnector so it can support ToolCallMessage and ToolCallResultMessage. These message types are necessary to use FunctionCallMiddleware, which provides support for processing and invoking function calls.
var apiKey = Environment.GetEnvironmentVariable("MISTRAL_API_KEY") ?? throw new Exception("Missing MISTRAL_API_KEY environment variable");
var client = new MistralClient(apiKey: apiKey);
var agent = new MistralClientAgent(
client: client,
name: "MistralAI",
model: MistralAIModelID.MISTRAL_SMALL_LATEST)
.RegisterMessageConnector(); // support more AutoGen built-in message types like ToolCallMessage and ToolCallResultMessage
Then create an FunctionCallMiddleware with GetWeather function When creating the middleware, we also pass a functionMap object which means the function will be automatically invoked when the agent replies a GetWeather function call.
var mistralFunctions = new MistralAgentFunction();
var functionCallMiddleware = new FunctionCallMiddleware(
functions: [mistralFunctions.GetWeatherFunctionContract],
functionMap: new Dictionary<string, Func<string, Task<string>>> // with functionMap, the function will be automatically triggered if the tool name matches one of the keys.
{
{ mistralFunctions.GetWeatherFunctionContract.Name, mistralFunctions.GetWeather }
});
After the function call middleware is created, register it with the agent so the GetWeather function will be passed to agent during chat completion.
agent = agent.RegisterStreamingMiddleware(functionCallMiddleware);
Finally, you can chat with the MistralClientAgent about weather! The agent will automatically invoke the GetWeather function to "get" the weather information and return the result.
var reply = await agent.SendAsync("What is the weather in Seattle?");
reply.GetContent().Should().Be("The weather in Seattle is sunny.");