Skip to content

Commit fa9548a

Browse files
markwallace-microsoftdmytrostruk
authored andcommitted
.Net: Net: PromptTemplateConfig and IPromptTemplate namesapce refactor (microsoft#3174)
### Motivation and Context Resolves: microsoft#3173 ### Description - Move `IPromptTemplate` to namespace `Microsoft.SemanticKernel.TemplateEngine` - Move `PromptTemplate` and `PromptTemplateConfig` to namespace `Microsoft.SemanticKernel.TemplateEngine` - Eliminate `Microsoft.SemanticKernel.SemanticFunctions` namespace - Remove `SemanticFunctionConfig` ### Contribution Checklist - [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: Dmytro Struk <[email protected]>
1 parent ebdd60d commit fa9548a

14 files changed

Lines changed: 57 additions & 87 deletions

File tree

‎dotnet/samples/KernelSyntaxExamples/Example58_ConfigureRequestSettings.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
using System.Threading.Tasks;
66
using Microsoft.SemanticKernel;
77
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
8-
using Microsoft.SemanticKernel.SemanticFunctions;
8+
using Microsoft.SemanticKernel.TemplateEngine;
99
using RepoUtils;
1010

1111
// ReSharper disable once InconsistentNaming
@@ -68,7 +68,7 @@ public static async Task RunAsync()
6868
}
6969
}";
7070
var templateConfig = JsonSerializer.Deserialize<PromptTemplateConfig>(configPayload);
71-
var func = kernel.CreateSemanticFunction(prompt, config: templateConfig!, "HelloAI");
71+
var func = kernel.CreateSemanticFunction(prompt, templateConfig!, "HelloAI");
7272

7373
result = await kernel.RunAsync(func);
7474
Console.WriteLine(result.GetValue<string>());

‎dotnet/src/Functions/Functions.Semantic/Extensions/KernelSemanticFunctionExtensions.cs‎

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
using Microsoft.SemanticKernel.AI.TextCompletion;
1111
using Microsoft.SemanticKernel.Diagnostics;
1212
using Microsoft.SemanticKernel.Orchestration;
13-
using Microsoft.SemanticKernel.SemanticFunctions;
13+
using Microsoft.SemanticKernel.TemplateEngine;
1414
using Microsoft.SemanticKernel.Text;
1515

1616
#pragma warning disable IDE0130
@@ -28,14 +28,16 @@ public static class KernelSemanticFunctionExtensions
2828
/// </summary>
2929
/// <param name="kernel">Semantic Kernel instance</param>
3030
/// <param name="functionName">Name of the semantic function. The name can contain only alphanumeric chars + underscore.</param>
31-
/// <param name="functionConfig">Function configuration, e.g. I/O params, AI settings, localization details, etc.</param>
31+
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
32+
/// <param name="promptTemplate">Prompt template.</param>
3233
/// <returns>A C# function wrapping AI logic, usually defined with natural language</returns>
3334
public static ISKFunction RegisterSemanticFunction(
3435
this IKernel kernel,
3536
string functionName,
36-
SemanticFunctionConfig functionConfig)
37+
PromptTemplateConfig promptTemplateConfig,
38+
IPromptTemplate promptTemplate)
3739
{
38-
return kernel.RegisterSemanticFunction(FunctionCollection.GlobalFunctionsPluginName, functionName, functionConfig);
40+
return kernel.RegisterSemanticFunction(FunctionCollection.GlobalFunctionsPluginName, functionName, promptTemplateConfig, promptTemplate);
3941
}
4042

4143
/// <summary>
@@ -44,18 +46,20 @@ public static ISKFunction RegisterSemanticFunction(
4446
/// <param name="kernel">Semantic Kernel instance</param>
4547
/// <param name="pluginName">Name of the plugin containing the function. The name can contain only alphanumeric chars + underscore.</param>
4648
/// <param name="functionName">Name of the semantic function. The name can contain only alphanumeric chars + underscore.</param>
47-
/// <param name="functionConfig">Function configuration, e.g. I/O params, AI settings, localization details, etc.</param>
49+
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
50+
/// <param name="promptTemplate">Prompt template.</param>
4851
/// <returns>A C# function wrapping AI logic, usually defined with natural language</returns>
4952
public static ISKFunction RegisterSemanticFunction(
5053
this IKernel kernel,
5154
string pluginName,
5255
string functionName,
53-
SemanticFunctionConfig functionConfig)
56+
PromptTemplateConfig promptTemplateConfig,
57+
IPromptTemplate promptTemplate)
5458
{
5559
// Future-proofing the name not to contain special chars
5660
Verify.ValidFunctionName(functionName);
5761

58-
ISKFunction function = kernel.CreateSemanticFunction(pluginName, functionName, functionConfig);
62+
ISKFunction function = kernel.CreateSemanticFunction(pluginName, functionName, promptTemplateConfig, promptTemplate);
5963
return kernel.RegisterCustomFunction(function);
6064
}
6165

@@ -81,19 +85,19 @@ public static ISKFunction CreateSemanticFunction(
8185
{
8286
functionName ??= RandomFunctionName();
8387

84-
var config = new PromptTemplateConfig
88+
var promptTemplateConfig = new PromptTemplateConfig
8589
{
8690
Description = description ?? "Generic function, unknown purpose",
8791
};
8892

8993
if (requestSettings is not null)
9094
{
91-
config.ModelSettings.Add(requestSettings);
95+
promptTemplateConfig.ModelSettings.Add(requestSettings);
9296
}
9397

9498
return kernel.CreateSemanticFunction(
9599
promptTemplate: promptTemplate,
96-
config: config,
100+
promptTemplateConfig: promptTemplateConfig,
97101
functionName: functionName,
98102
pluginName: pluginName);
99103
}
@@ -103,53 +107,50 @@ public static ISKFunction CreateSemanticFunction(
103107
/// </summary>
104108
/// <param name="kernel">Semantic Kernel instance</param>
105109
/// <param name="promptTemplate">Plain language definition of the semantic function, using SK template language</param>
106-
/// <param name="config">Optional function settings</param>
110+
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
107111
/// <param name="functionName">A name for the given function. The name can be referenced in templates and used by the pipeline planner.</param>
108112
/// <param name="pluginName">An optional plugin name, e.g. to namespace functions with the same name. When empty,
109113
/// the function is added to the global namespace, overwriting functions with the same name</param>
110114
/// <returns>A function ready to use</returns>
111115
public static ISKFunction CreateSemanticFunction(
112116
this IKernel kernel,
113117
string promptTemplate,
114-
PromptTemplateConfig config,
118+
PromptTemplateConfig promptTemplateConfig,
115119
string? functionName = null,
116120
string? pluginName = null)
117121
{
118122
functionName ??= RandomFunctionName();
119123
Verify.ValidFunctionName(functionName);
120124
if (!string.IsNullOrEmpty(pluginName)) { Verify.ValidPluginName(pluginName); }
121125

122-
var template = new PromptTemplate(promptTemplate, config, kernel.PromptTemplateEngine);
123-
124-
// Prepare lambda wrapping AI logic
125-
var functionConfig = new SemanticFunctionConfig(config, template);
126+
var template = new PromptTemplate(promptTemplate, promptTemplateConfig, kernel.PromptTemplateEngine);
126127

127128
// TODO: manage overwrites, potentially error out
128129
return string.IsNullOrEmpty(pluginName)
129-
? kernel.RegisterSemanticFunction(functionName, functionConfig)
130-
: kernel.RegisterSemanticFunction(pluginName!, functionName, functionConfig);
130+
? kernel.RegisterSemanticFunction(functionName, promptTemplateConfig, template)
131+
: kernel.RegisterSemanticFunction(pluginName!, functionName, promptTemplateConfig, template);
131132
}
132133

133134
/// <summary>
134135
/// Invoke a semantic function using the provided prompt template.
135136
/// </summary>
136137
/// <param name="kernel">Semantic Kernel instance</param>
137-
/// <param name="promptTemplate">Plain language definition of the semantic function, using SK template language</param>
138+
/// <param name="template">Plain language definition of the semantic function, using SK template language</param>
138139
/// <param name="functionName">A name for the given function. The name can be referenced in templates and used by the pipeline planner.</param>
139140
/// <param name="pluginName">Optional plugin name, for namespacing and avoid collisions</param>
140141
/// <param name="description">Optional description, useful for the planner</param>
141142
/// <param name="requestSettings">Optional LLM request settings</param>
142143
/// <returns>Kernel execution result</returns>
143144
public static Task<KernelResult> InvokeSemanticFunctionAsync(
144145
this IKernel kernel,
145-
string promptTemplate,
146+
string template,
146147
string? functionName = null,
147148
string? pluginName = null,
148149
string? description = null,
149150
AIRequestSettings? requestSettings = null)
150151
{
151152
var skFunction = kernel.CreateSemanticFunction(
152-
promptTemplate,
153+
template,
153154
functionName,
154155
pluginName,
155156
description,
@@ -256,14 +257,12 @@ public static IDictionary<string, ISKFunction> ImportSemanticFunctionsFromDirect
256257
// Load prompt template
257258
var template = new PromptTemplate(File.ReadAllText(promptPath), config, kernel.PromptTemplateEngine);
258259

259-
var functionConfig = new SemanticFunctionConfig(config, template);
260-
261260
if (logger.IsEnabled(LogLevel.Trace))
262261
{
263262
logger.LogTrace("Registering function {0}.{1} loaded from {2}", pluginDirectoryName, functionName, dir);
264263
}
265264

266-
functions[functionName] = kernel.RegisterSemanticFunction(pluginDirectoryName, functionName, functionConfig);
265+
functions[functionName] = kernel.RegisterSemanticFunction(pluginDirectoryName, functionName, config, template);
267266
}
268267
}
269268

@@ -276,23 +275,25 @@ private static ISKFunction CreateSemanticFunction(
276275
this IKernel kernel,
277276
string pluginName,
278277
string functionName,
279-
SemanticFunctionConfig functionConfig)
278+
PromptTemplateConfig promptTemplateConfig,
279+
IPromptTemplate promptTemplate)
280280
{
281281
ISKFunction func = SemanticFunction.FromSemanticConfig(
282282
pluginName,
283283
functionName,
284-
functionConfig,
284+
promptTemplateConfig,
285+
promptTemplate,
285286
kernel.LoggerFactory
286287
);
287288

288289
// Connect the function to the current kernel function collection, in case the function
289290
// is invoked manually without a context and without a way to find other functions.
290291
func.SetDefaultFunctionCollection(kernel.Functions);
291292

292-
func.SetAIConfiguration(functionConfig.PromptTemplateConfig.GetDefaultRequestSettings());
293+
func.SetAIConfiguration(promptTemplateConfig.GetDefaultRequestSettings());
293294

294295
// Note: the service is instantiated using the kernel configuration state when the function is invoked
295-
func.SetAIService(() => kernel.GetService<ITextCompletion>(functionConfig.PromptTemplateConfig.GetDefaultRequestSettings()?.ServiceId ?? null));
296+
func.SetAIService(() => kernel.GetService<ITextCompletion>(promptTemplateConfig.GetDefaultRequestSettings()?.ServiceId ?? null));
296297

297298
return func;
298299
}

‎dotnet/src/Functions/Functions.Semantic/SemanticFunction.cs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
using Microsoft.SemanticKernel.AI.TextCompletion;
1515
using Microsoft.SemanticKernel.Diagnostics;
1616
using Microsoft.SemanticKernel.Orchestration;
17-
using Microsoft.SemanticKernel.SemanticFunctions;
17+
using Microsoft.SemanticKernel.TemplateEngine;
1818

1919
#pragma warning disable IDE0130
2020
// ReSharper disable once CheckNamespace - Using the main namespace
@@ -51,27 +51,30 @@ internal sealed class SemanticFunction : ISKFunction, IDisposable
5151
/// </summary>
5252
/// <param name="pluginName">Name of the plugin to which the function being created belongs.</param>
5353
/// <param name="functionName">Name of the function to create.</param>
54-
/// <param name="functionConfig">Semantic function configuration.</param>
54+
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
55+
/// <param name="promptTemplate">Prompt template.</param>
5556
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param>
5657
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param>
5758
/// <returns>SK function instance.</returns>
5859
public static ISKFunction FromSemanticConfig(
5960
string pluginName,
6061
string functionName,
61-
SemanticFunctionConfig functionConfig,
62+
PromptTemplateConfig promptTemplateConfig,
63+
IPromptTemplate promptTemplate,
6264
ILoggerFactory? loggerFactory = null,
6365
CancellationToken cancellationToken = default)
6466
{
65-
Verify.NotNull(functionConfig);
67+
Verify.NotNull(promptTemplateConfig);
68+
Verify.NotNull(promptTemplate);
6669

6770
var func = new SemanticFunction(
68-
template: functionConfig.PromptTemplate,
69-
description: functionConfig.PromptTemplateConfig.Description,
71+
template: promptTemplate,
72+
description: promptTemplateConfig.Description,
7073
pluginName: pluginName,
7174
functionName: functionName,
7275
loggerFactory: loggerFactory
7376
);
74-
func.SetAIConfiguration(functionConfig.PromptTemplateConfig.GetDefaultRequestSettings());
77+
func.SetAIConfiguration(promptTemplateConfig.GetDefaultRequestSettings());
7578

7679
return func;
7780
}

‎dotnet/src/Functions/Functions.Semantic/SemanticFunctions/SemanticFunctionConfig.cs‎

Lines changed: 0 additions & 32 deletions
This file was deleted.

dotnet/src/Functions/Functions.Semantic/SemanticFunctions/PromptTemplate.cs renamed to dotnet/src/Functions/Functions.Semantic/TemplateEngine/PromptTemplate.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
using System.Threading;
77
using System.Threading.Tasks;
88
using Microsoft.SemanticKernel.Orchestration;
9-
using Microsoft.SemanticKernel.TemplateEngine;
109

11-
namespace Microsoft.SemanticKernel.SemanticFunctions;
10+
namespace Microsoft.SemanticKernel.TemplateEngine;
1211

1312
/// <summary>
1413
/// Prompt template.

dotnet/src/Functions/Functions.Semantic/SemanticFunctions/PromptTemplateConfig.cs renamed to dotnet/src/Functions/Functions.Semantic/TemplateEngine/PromptTemplateConfig.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using Microsoft.SemanticKernel.AI;
88
using Microsoft.SemanticKernel.Text;
99

10-
namespace Microsoft.SemanticKernel.SemanticFunctions;
10+
namespace Microsoft.SemanticKernel.TemplateEngine;
1111

1212
/// <summary>
1313
/// Prompt template configuration.

‎dotnet/src/Functions/Functions.UnitTests/SemanticFunctions/PromptTemplateConfigTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
using System.Text.Json;
44
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
5-
using Microsoft.SemanticKernel.SemanticFunctions;
5+
using Microsoft.SemanticKernel.TemplateEngine;
66
using Xunit;
77

88
namespace SemanticKernel.Functions.UnitTests.SemanticFunctions;

‎dotnet/src/Functions/Functions.UnitTests/SemanticFunctions/SemanticFunctionTests.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
1414
using Microsoft.SemanticKernel.Diagnostics;
1515
using Microsoft.SemanticKernel.Events;
16-
using Microsoft.SemanticKernel.SemanticFunctions;
16+
using Microsoft.SemanticKernel.TemplateEngine;
1717
using Moq;
1818
using Xunit;
1919

‎dotnet/src/IntegrationTests/Connectors/OpenAI/OpenAICompletionTests.cs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
using Microsoft.SemanticKernel.Diagnostics;
1212
using Microsoft.SemanticKernel.Orchestration;
1313
using Microsoft.SemanticKernel.Reliability.Basic;
14-
using Microsoft.SemanticKernel.SemanticFunctions;
14+
using Microsoft.SemanticKernel.TemplateEngine;
1515
using SemanticKernel.IntegrationTests.TestSettings;
1616
using Xunit;
1717
using Xunit.Abstractions;
@@ -356,14 +356,12 @@ public async Task MultipleServiceLoadPromptConfigTestAsync()
356356

357357
var defaultFunc = target.RegisterSemanticFunction(
358358
"WherePlugin", "FishMarket1",
359-
new SemanticFunctionConfig(
360-
defaultConfig,
361-
new PromptTemplate(prompt, defaultConfig, target.PromptTemplateEngine)));
359+
defaultConfig,
360+
new PromptTemplate(prompt, defaultConfig, target.PromptTemplateEngine));
362361
var azureFunc = target.RegisterSemanticFunction(
363362
"WherePlugin", "FishMarket2",
364-
new SemanticFunctionConfig(
365-
azureConfig,
366-
new PromptTemplate(prompt, azureConfig, target.PromptTemplateEngine)));
363+
azureConfig,
364+
new PromptTemplate(prompt, azureConfig, target.PromptTemplateEngine));
367365

368366
// Act
369367
await Assert.ThrowsAsync<HttpOperationException>(() => target.RunAsync(defaultFunc));

‎dotnet/src/Planners/Planners.Core.UnitTests/Sequential/SequentialPlanParserTests.cs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
using Microsoft.Extensions.Logging;
55
using Microsoft.SemanticKernel.Diagnostics;
66
using Microsoft.SemanticKernel.Orchestration;
7-
using Microsoft.SemanticKernel.SemanticFunctions;
7+
using Microsoft.SemanticKernel.TemplateEngine;
88
using Moq;
99
using Xunit;
1010
using Xunit.Abstractions;
@@ -87,7 +87,8 @@ private void CreateKernelAndFunctionCreateMocks(List<(string name, string plugin
8787
kernelMock.Setup(x => x.RegisterSemanticFunction(
8888
It.IsAny<string>(),
8989
It.IsAny<string>(),
90-
It.IsAny<SemanticFunctionConfig>()
90+
It.IsAny<PromptTemplateConfig>(),
91+
It.IsAny<IPromptTemplate>()
9192
)).Returns(mockFunction.Object);
9293
}
9394
else

0 commit comments

Comments
 (0)