Skip to content

Commit 3a7d3bd

Browse files
gitri-mscrickman
andauthored
.Net: Planner options improvements (#4793)
### Motivation and Context <!-- Thank you for your contribution to the semantic-kernel repo! Please help reviewers and future users, providing the following information: 1. Why is this change required? 2. What problem does it solve? 3. What scenario does it contribute to? 4. If it fixes an open issue, please link to the issue here. --> Changes to improve consistency across the .NET planners. Addresses the following: - #4727 - #4728 - #4729 - #4731 - #4775 ### Description <!-- Describe your changes, the overall approach, the underlying design. These notes will help understanding how your code works. Thanks! --> - Clean up planner options. Shared options live in parent class (`PlannerOptions`). Other options reside in the planner-specific options class (`HandlebarsPlannerOptions` or `FunctionCallingStepwisePlannerOptions`). - Change default name delimiter to dash (was underscore for function calling stepwise planner). Update all tests accordingly. (Will explore making this configurable in a separate issue/PR) - Change how we get the list of available functions for Handlebars planner, to reduce code duplication and get overridable options for free. - Make `MaxTokens` setting optional for function calling stepwise planner (not yet supported for Handlebars planner) ### Contribution Checklist <!-- Before submitting this PR, please make sure: --> - [x] The code builds clean without any errors or warnings - [x] The PR follows the [SK Contribution Guidelines](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md) and the [pre-submission formatting script](https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md#development-scripts) raises no violations - [x] All unit tests pass, and I have added new tests where possible - [x] I didn't break anyone 😄 --------- Co-authored-by: Chris <[email protected]>
1 parent de197eb commit 3a7d3bd

26 files changed

Lines changed: 151 additions & 164 deletions

‎dotnet/SK-dotnet.sln‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Planners.Handlebars", "src\
152152
EndProject
153153
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "planning", "planning", "{A7DAB812-81CF-4931-B38C-468A3261C4C8}"
154154
ProjectSection(SolutionItems) = preProject
155-
src\InternalUtilities\planning\PlannerConfigBase.cs = src\InternalUtilities\planning\PlannerConfigBase.cs
156155
src\InternalUtilities\planning\PlannerInstrumentation.cs = src\InternalUtilities\planning\PlannerInstrumentation.cs
157156
src\InternalUtilities\planning\PlanningUtilities.props = src\InternalUtilities\planning\PlanningUtilities.props
157+
src\InternalUtilities\planning\PlannerOptions.cs = src\InternalUtilities\planning\PlannerOptions.cs
158158
src\InternalUtilities\planning\SemanticMemoryConfig.cs = src\InternalUtilities\planning\SemanticMemoryConfig.cs
159159
EndProjectSection
160160
EndProject

‎dotnet/samples/KernelSyntaxExamples/Example48_GroundednessChecks.cs‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.IO;
44
using System.Threading.Tasks;
55
using Microsoft.SemanticKernel;
6+
using Microsoft.SemanticKernel.Connectors.OpenAI;
67
using Microsoft.SemanticKernel.Planning.Handlebars;
78
using Microsoft.SemanticKernel.Plugins.Core;
89
using RepoUtils;
@@ -101,7 +102,16 @@ which are not grounded in the original.
101102

102103
kernel.ImportPluginFromType<TextPlugin>();
103104

104-
var planner = new HandlebarsPlanner();
105+
var planner = new HandlebarsPlanner(
106+
new HandlebarsPlannerOptions()
107+
{
108+
// When using OpenAI models, we recommend using low values for temperature and top_p to minimize planner hallucinations.
109+
ExecutionSettings = new OpenAIPromptExecutionSettings()
110+
{
111+
Temperature = 0.0,
112+
TopP = 0.1,
113+
}
114+
});
105115
var plan = await planner.CreatePlanAsync(kernel, ask);
106116

107117
WriteLine($"======== Goal: ========\n{ask}");

‎dotnet/samples/KernelSyntaxExamples/Example65_HandlebarsPlanner.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.IO;
55
using System.Threading.Tasks;
66
using Microsoft.SemanticKernel;
7+
using Microsoft.SemanticKernel.Connectors.OpenAI;
78
using Microsoft.SemanticKernel.Planning.Handlebars;
89
using Microsoft.SemanticKernel.Plugins.OpenApi;
910
using Plugins.DictionaryPlugin;
@@ -81,6 +82,13 @@ await kernel.ImportPluginFromOpenApiAsync(
8182
var planner = new HandlebarsPlanner(
8283
new HandlebarsPlannerOptions()
8384
{
85+
// When using OpenAI models, we recommend using low values for temperature and top_p to minimize planner hallucinations.
86+
ExecutionSettings = new OpenAIPromptExecutionSettings()
87+
{
88+
Temperature = 0.0,
89+
TopP = 0.1,
90+
},
91+
8492
// Change this if you want to test with loops regardless of model selection.
8593
AllowLoops = allowLoopsInPlan
8694
});

‎dotnet/samples/KernelSyntaxExamples/Example66_FunctionCallingStepwisePlanner.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ public async Task RunAsync()
2222

2323
var kernel = InitializeKernel();
2424

25-
var config = new FunctionCallingStepwisePlannerConfig
25+
var options = new FunctionCallingStepwisePlannerOptions
2626
{
2727
MaxIterations = 15,
2828
MaxTokens = 4000,
2929
};
30-
var planner = new FunctionCallingStepwisePlanner(config);
30+
var planner = new FunctionCallingStepwisePlanner(options);
3131

3232
foreach (var question in questions)
3333
{

‎dotnet/samples/TelemetryExample/Program.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ private static Kernel GetKernel(ILoggerFactory loggerFactory)
105105
return builder.Build();
106106
}
107107

108-
private static HandlebarsPlanner CreatePlanner(int maxTokens = 1024)
108+
private static HandlebarsPlanner CreatePlanner()
109109
{
110-
var plannerConfig = new HandlebarsPlannerOptions { MaxTokens = maxTokens };
111-
return new HandlebarsPlanner(plannerConfig);
110+
var plannerOptions = new HandlebarsPlannerOptions();
111+
return new HandlebarsPlanner(plannerOptions);
112112
}
113113

114114
private static void LoadUserSecrets()

‎dotnet/src/Connectors/Connectors.OpenAI/AzureSdk/OpenAIFunction.cs‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,9 @@ internal OpenAIFunction(
101101
}
102102

103103
/// <summary>Gets the separator used between the plugin name and the function name, if a plugin name is present.</summary>
104-
public static string NameSeparator => "_";
104+
/// <remarks>This separator was previously <c>_</c>, but has been changed to <c>-</c> to better align to the behavior elsewhere in SK and in response
105+
/// to developers who want to use underscores in their function or plugin names. We plan to make this setting configurable in the future.</remarks>
106+
public static string NameSeparator { get; set; } = "-";
105107

106108
/// <summary>Gets the name of the plugin with which the function is associated, if any.</summary>
107109
public string? PluginName { get; }

‎dotnet/src/Connectors/Connectors.UnitTests/OpenAI/AzureSdk/OpenAIPluginCollectionExtensionsTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void TryGetFunctionAndArgumentsWithoutArgumentsReturnsTrue()
3838
var plugin = KernelPluginFactory.CreateFromFunctions("MyPlugin", [function]);
3939

4040
var plugins = new KernelPluginCollection([plugin]);
41-
var toolCall = new ChatCompletionsFunctionToolCall("id", "MyPlugin_MyFunction", string.Empty);
41+
var toolCall = new ChatCompletionsFunctionToolCall("id", "MyPlugin-MyFunction", string.Empty);
4242

4343
// Act
4444
var result = plugins.TryGetFunctionAndArguments(toolCall, out var actualFunction, out var actualArguments);
@@ -57,7 +57,7 @@ public void TryGetFunctionAndArgumentsWithArgumentsReturnsTrue()
5757
var plugin = KernelPluginFactory.CreateFromFunctions("MyPlugin", [function]);
5858

5959
var plugins = new KernelPluginCollection([plugin]);
60-
var toolCall = new ChatCompletionsFunctionToolCall("id", "MyPlugin_MyFunction", "{\n \"location\": \"San Diego\",\n \"max_price\": 300\n,\n \"null_argument\": null\n}");
60+
var toolCall = new ChatCompletionsFunctionToolCall("id", "MyPlugin-MyFunction", "{\n \"location\": \"San Diego\",\n \"max_price\": 300\n,\n \"null_argument\": null\n}");
6161

6262
// Act
6363
var result = plugins.TryGetFunctionAndArguments(toolCall, out var actualFunction, out var actualArguments);

‎dotnet/src/Connectors/Connectors.UnitTests/OpenAI/ChatCompletion/AzureOpenAIChatCompletionServiceTests.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ public async Task GetChatMessageContentsWithRequiredFunctionCallAsync()
388388
var secondContentJson = JsonSerializer.Deserialize<JsonElement>(secondContent);
389389

390390
Assert.Equal(1, firstContentJson.GetProperty("tools").GetArrayLength());
391-
Assert.Equal("MyPlugin_GetCurrentWeather", firstContentJson.GetProperty("tool_choice").GetProperty("function").GetProperty("name").GetString());
391+
Assert.Equal("MyPlugin-GetCurrentWeather", firstContentJson.GetProperty("tool_choice").GetProperty("function").GetProperty("name").GetString());
392392

393393
Assert.Equal("none", secondContentJson.GetProperty("tool_choice").GetString());
394394
}
@@ -555,7 +555,7 @@ public async Task GetStreamingChatMessageContentsWithRequiredFunctionCallAsync()
555555
var secondContentJson = JsonSerializer.Deserialize<JsonElement>(secondContent);
556556

557557
Assert.Equal(1, firstContentJson.GetProperty("tools").GetArrayLength());
558-
Assert.Equal("MyPlugin_GetCurrentWeather", firstContentJson.GetProperty("tool_choice").GetProperty("function").GetProperty("name").GetString());
558+
Assert.Equal("MyPlugin-GetCurrentWeather", firstContentJson.GetProperty("tool_choice").GetProperty("function").GetProperty("name").GetString());
559559

560560
Assert.Equal("none", secondContentJson.GetProperty("tool_choice").GetString());
561561
}

‎dotnet/src/Connectors/Connectors.UnitTests/OpenAI/ChatCompletion/OpenAIChatCompletionServiceTests.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public async Task ItCreatesCorrectFunctionToolCallsWhenUsingAutoAsync()
9999
Assert.NotNull(actualRequestContent);
100100
var optionsJson = JsonSerializer.Deserialize<JsonElement>(actualRequestContent);
101101
Assert.Equal(2, optionsJson.GetProperty("tools").GetArrayLength());
102-
Assert.Equal("TimePlugin_Date", optionsJson.GetProperty("tools")[0].GetProperty("function").GetProperty("name").GetString());
103-
Assert.Equal("TimePlugin_Now", optionsJson.GetProperty("tools")[1].GetProperty("function").GetProperty("name").GetString());
102+
Assert.Equal("TimePlugin-Date", optionsJson.GetProperty("tools")[0].GetProperty("function").GetProperty("name").GetString());
103+
Assert.Equal("TimePlugin-Now", optionsJson.GetProperty("tools")[1].GetProperty("function").GetProperty("name").GetString());
104104
}
105105

106106
[Fact]
@@ -120,7 +120,7 @@ public async Task ItCreatesCorrectFunctionToolCallsWhenUsingNowAsync()
120120
Assert.NotNull(actualRequestContent);
121121
var optionsJson = JsonSerializer.Deserialize<JsonElement>(actualRequestContent);
122122
Assert.Equal(1, optionsJson.GetProperty("tools").GetArrayLength());
123-
Assert.Equal("TimePlugin_Now", optionsJson.GetProperty("tools")[0].GetProperty("function").GetProperty("name").GetString());
123+
Assert.Equal("TimePlugin-Now", optionsJson.GetProperty("tools")[0].GetProperty("function").GetProperty("name").GetString());
124124
}
125125

126126
[Fact]

‎dotnet/src/Connectors/Connectors.UnitTests/OpenAI/FunctionCalling/KernelFunctionMetadataExtensionsTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void ItCanConvertToOpenAIFunctionNoParameters()
3535
Assert.Equal(sut.Name, result.FunctionName);
3636
Assert.Equal(sut.PluginName, result.PluginName);
3737
Assert.Equal(sut.Description, result.Description);
38-
Assert.Equal($"{sut.PluginName}_{sut.Name}", result.FullyQualifiedName);
38+
Assert.Equal($"{sut.PluginName}-{sut.Name}", result.FullyQualifiedName);
3939

4040
Assert.NotNull(result.ReturnParameter);
4141
Assert.Equal("retDesc", result.ReturnParameter.Description);

0 commit comments

Comments
 (0)