-
Notifications
You must be signed in to change notification settings - Fork 3.3k
/
MistralAI_FunctionCalling.cs
169 lines (144 loc) · 6.75 KB
/
MistralAI_FunctionCalling.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Copyright (c) Microsoft. All rights reserved.
using System.ComponentModel;
using System.Text.Json.Serialization;
using Microsoft.OpenApi.Extensions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.MistralAI;
namespace ChatCompletion;
/// <summary>
/// Demonstrates the use of function calling with MistralAI.
/// </summary>
public sealed class MistralAI_FunctionCalling(ITestOutputHelper output) : BaseTest(output)
{
[Fact]
public async Task AutoInvokeKernelFunctionsAsync()
{
// Create a kernel with MistralAI chat completion and WeatherPlugin
Kernel kernel = this.CreateKernelWithWeatherPlugin();
// Invoke chat prompt with auto invocation of functions enabled
const string ChatPrompt = """
<message role="user">What is the weather like in Paris?</message>
""";
var executionSettings = new MistralAIPromptExecutionSettings { ToolCallBehavior = MistralAIToolCallBehavior.AutoInvokeKernelFunctions };
var chatSemanticFunction = kernel.CreateFunctionFromPrompt(
ChatPrompt, executionSettings);
var chatPromptResult = await kernel.InvokeAsync(chatSemanticFunction);
Console.WriteLine(chatPromptResult);
}
[Fact]
public async Task AutoInvokeKernelFunctionsMultipleCallsAsync()
{
// Create a kernel with MistralAI chat completion and WeatherPlugin
Kernel kernel = this.CreateKernelWithWeatherPlugin();
var service = kernel.GetRequiredService<IChatCompletionService>();
// Invoke chat prompt with auto invocation of functions enabled
var chatHistory = new ChatHistory
{
new ChatMessageContent(AuthorRole.User, "What is the weather like in Paris?")
};
var executionSettings = new MistralAIPromptExecutionSettings { ToolCallBehavior = MistralAIToolCallBehavior.AutoInvokeKernelFunctions };
var chatPromptResult1 = await service.GetChatMessageContentsAsync(chatHistory, executionSettings, kernel);
chatHistory.AddRange(chatPromptResult1);
chatHistory.Add(new ChatMessageContent(AuthorRole.User, "What is the weather like in Marseille?"));
var chatPromptResult2 = await service.GetChatMessageContentsAsync(chatHistory, executionSettings, kernel);
Console.WriteLine(chatPromptResult1[0].Content);
Console.WriteLine(chatPromptResult2[0].Content);
}
[Fact]
public async Task RequiredKernelFunctionsAsync()
{
// Create a kernel with MistralAI chat completion and WeatherPlugin
Kernel kernel = this.CreateKernelWithWeatherPlugin();
var plugin = kernel.Plugins.First();
// Invoke chat prompt with auto invocation of functions enabled
const string ChatPrompt = """
<message role="user">What is the weather like in Paris?</message>
""";
var executionSettings = new MistralAIPromptExecutionSettings
{
ToolCallBehavior = MistralAIToolCallBehavior.RequiredFunctions(plugin, true)
};
var chatSemanticFunction = kernel.CreateFunctionFromPrompt(
ChatPrompt, executionSettings);
var chatPromptResult = await kernel.InvokeAsync(chatSemanticFunction);
Console.WriteLine(chatPromptResult);
}
[Fact]
public async Task NoKernelFunctionsAsync()
{
// Create a kernel with MistralAI chat completion and WeatherPlugin
Kernel kernel = this.CreateKernelWithWeatherPlugin();
// Invoke chat prompt with auto invocation of functions enabled
const string ChatPrompt = """
<message role="user">What is the weather like in Paris?</message>
""";
var executionSettings = new MistralAIPromptExecutionSettings
{
ToolCallBehavior = MistralAIToolCallBehavior.NoKernelFunctions
};
var chatSemanticFunction = kernel.CreateFunctionFromPrompt(
ChatPrompt, executionSettings);
var chatPromptResult = await kernel.InvokeAsync(chatSemanticFunction);
Console.WriteLine(chatPromptResult);
}
[Fact]
public async Task AutoInvokeKernelFunctionsMultiplePluginsAsync()
{
// Create a kernel with MistralAI chat completion and WeatherPlugin and WidgetPlugin
Kernel kernel = this.CreateKernelWithWeatherPlugin();
kernel.Plugins.AddFromType<WidgetPlugin>();
// Invoke chat prompt with auto invocation of functions enabled
const string ChatPrompt = """
<message role="user">Create a lime and scarlet colored widget for me.</message>
""";
var executionSettings = new MistralAIPromptExecutionSettings { ToolCallBehavior = MistralAIToolCallBehavior.AutoInvokeKernelFunctions };
var chatSemanticFunction = kernel.CreateFunctionFromPrompt(
ChatPrompt, executionSettings);
var chatPromptResult = await kernel.InvokeAsync(chatSemanticFunction);
Console.WriteLine(chatPromptResult);
}
public sealed class WeatherPlugin
{
[KernelFunction]
[Description("Get the current weather in a given location.")]
public string GetWeather(
[Description("The city and department, e.g. Marseille, 13")] string location
) => "12°C\nWind: 11 KMPH\nHumidity: 48%\nMostly cloudy";
}
public sealed class WidgetPlugin
{
[KernelFunction]
[Description("Creates a new widget of the specified type and colors")]
public string CreateWidget([Description("The colors of the widget to be created")] WidgetColor[] widgetColors)
{
var colors = string.Join('-', widgetColors.Select(c => c.GetDisplayName()).ToArray());
return $"Widget created with colors: {colors}";
}
}
[JsonConverter(typeof(JsonStringEnumConverter))]
public enum WidgetColor
{
[Description("Use when creating a red item.")]
Red,
[Description("Use when creating a green item.")]
Green,
[Description("Use when creating a blue item.")]
Blue
}
private Kernel CreateKernelWithWeatherPlugin()
{
// Create a logging handler to output HTTP requests and responses
var handler = new LoggingHandler(new HttpClientHandler(), this.Output);
HttpClient httpClient = new(handler);
// Create a kernel with MistralAI chat completion and WeatherPlugin
IKernelBuilder kernelBuilder = Kernel.CreateBuilder();
kernelBuilder.AddMistralChatCompletion(
modelId: TestConfiguration.MistralAI.ChatModelId!,
apiKey: TestConfiguration.MistralAI.ApiKey!,
httpClient: httpClient);
kernelBuilder.Plugins.AddFromType<WeatherPlugin>();
Kernel kernel = kernelBuilder.Build();
return kernel;
}
}