Skip to content

A programming framework for agentic AI

3.1 KB
Raw Blame

Use tool in MistralChatAgent

The following example shows how to enable tool support in @AutoGen.Mistral.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 [!code-csharp]

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 @AutoGen.Core.FunctionAttribute. Marking the class as public partial together with the @AutoGen.Core.FunctionAttribute attribute allows the source generator to generate the @AutoGen.Core.FunctionContract for the GetWeather method.

[!code-csharp]

Then create an @AutoGen.Mistral.MistralClientAgent and register it with @AutoGen.Mistral.Extension.MistralAgentExtension.RegisterMessageConnector* so it can support @AutoGen.Core.ToolCallMessage and @AutoGen.Core.ToolCallResultMessage. These message types are necessary to use @AutoGen.Core.FunctionCallMiddleware, which provides support for processing and invoking function calls.

[!code-csharp]

Then create an @AutoGen.Core.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.

[!code-csharp]

After the function call middleware is created, register it with the agent so the GetWeather function will be passed to agent during chat completion.

[!code-csharp]

Finally, you can chat with the @AutoGen.Mistral.MistralClientAgent about weather! The agent will automatically invoke the GetWeather function to "get" the weather information and return the result.

[!code-csharp]