Skip to content

Commit

Permalink
.Net: Merge BasicPromptTemplateFactory to ms.sk.core (microsoft#3518)
Browse files Browse the repository at this point in the history
### 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.
-->

### Description

<!-- Describe your changes, the overall approach, the underlying design.
These notes will help understanding how your code works. Thanks! -->

### Contribution Checklist

<!-- Before submitting this PR, please make sure: -->

- [ ] The code builds clean without any errors or warnings
- [ ] 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
- [ ] All unit tests pass, and I have added new tests where possible
- [ ] I didn't break anyone 😄
  • Loading branch information
markwallace-microsoft authored Nov 16, 2023
1 parent ace7a70 commit 38fe2f0
Show file tree
Hide file tree
Showing 53 changed files with 113 additions and 302 deletions.
9 changes: 0 additions & 9 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ApplicationInsightsExample"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Connectors.Memory.Kusto", "src\Connectors\Connectors.Memory.Kusto\Connectors.Memory.Kusto.csproj", "{E07608CC-D710-4655-BB9E-D22CF3CDD193}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TemplateEngine.Basic", "src\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj", "{10E4B697-D4E8-468D-872D-49670FD150FB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Reliability.Polly", "src\Extensions\Reliability.Polly\Reliability.Polly.csproj", "{D4540A0F-98E3-4E70-9093-1948AE5B2AAD}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Reliability.Basic", "src\Extensions\Reliability.Basic\Reliability.Basic.csproj", "{3DC4DBD8-20A5-4937-B4F5-BB5E24E7A567}"
Expand Down Expand Up @@ -355,12 +353,6 @@ Global
{E07608CC-D710-4655-BB9E-D22CF3CDD193}.Publish|Any CPU.Build.0 = Publish|Any CPU
{E07608CC-D710-4655-BB9E-D22CF3CDD193}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E07608CC-D710-4655-BB9E-D22CF3CDD193}.Release|Any CPU.Build.0 = Release|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Publish|Any CPU.Build.0 = Publish|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10E4B697-D4E8-468D-872D-49670FD150FB}.Release|Any CPU.Build.0 = Release|Any CPU
{D4540A0F-98E3-4E70-9093-1948AE5B2AAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4540A0F-98E3-4E70-9093-1948AE5B2AAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4540A0F-98E3-4E70-9093-1948AE5B2AAD}.Publish|Any CPU.ActiveCfg = Publish|Any CPU
Expand Down Expand Up @@ -497,7 +489,6 @@ Global
{E6EDAB8F-3406-4DBF-9AAB-DF40DC2CA0FA} = {FA3720F1-C99A-49B2-9577-A940257098BF}
{C754950A-E16C-4F96-9CC7-9328E361B5AF} = {FA3720F1-C99A-49B2-9577-A940257098BF}
{E07608CC-D710-4655-BB9E-D22CF3CDD193} = {0247C2C9-86C3-45BA-8873-28B0948EDC0C}
{10E4B697-D4E8-468D-872D-49670FD150FB} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{D4540A0F-98E3-4E70-9093-1948AE5B2AAD} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{3DC4DBD8-20A5-4937-B4F5-BB5E24E7A567} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{D6D598DF-C17C-46F4-B2B9-CDE82E2DE132} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
Expand Down
1 change: 0 additions & 1 deletion dotnet/notebooks/03-semantic-function-inline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@
"using Microsoft.SemanticKernel;\n",
"using Microsoft.SemanticKernel.Connectors.AI.OpenAI;\n",
"using Microsoft.SemanticKernel.TemplateEngine;\n",
"using Microsoft.SemanticKernel.TemplateEngine.Basic;\n",
"\n",
"var builder = new KernelBuilder();\n",
"\n",
Expand Down
1 change: 0 additions & 1 deletion dotnet/notebooks/04-context-variables-chat.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"\n",
"using Microsoft.SemanticKernel;\n",
"using Microsoft.SemanticKernel.TemplateEngine;\n",
"using Microsoft.SemanticKernel.TemplateEngine.Basic;\n",
"using Microsoft.SemanticKernel.Orchestration;\n",
"using Microsoft.SemanticKernel.Connectors.AI.OpenAI;\n",
"\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
<ItemGroup>
<ProjectReference Include="..\..\src\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" />
<ProjectReference Include="..\..\src\SemanticKernel.Core\SemanticKernel.Core.csproj" />
<ProjectReference Include="..\..\src\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\..\src\Planners\Planners.Core\Planners.Core.csproj" />
<ProjectReference Include="..\..\src\Plugins\Plugins.Core\Plugins.Core.csproj" />
<ProjectReference Include="..\..\src\Plugins\Plugins.Web\Plugins.Web.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Plugins.Core;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using RepoUtils;

// ReSharper disable once InconsistentNaming
Expand Down Expand Up @@ -52,7 +51,7 @@ Is it weekend time (weekend/not weekend)?

// This allows to see the prompt before it's sent to OpenAI
Console.WriteLine("--- Rendered Prompt");
var promptTemplateFactory = new BasicPromptTemplateFactory();
var promptTemplateFactory = new KernelPromptTemplateFactory();
var promptTemplate = promptTemplateFactory.Create(FunctionDefinition, new PromptTemplateConfig());
var renderedPrompt = await promptTemplate.RenderAsync(kernel.CreateNewContext());
Console.WriteLine(renderedPrompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using Microsoft.SemanticKernel.Plugins.Web.Google;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using RepoUtils;

/// <summary>
Expand Down Expand Up @@ -150,7 +149,7 @@ [END OF EXAMPLES]
// If the answer contains commands, execute them using the prompt renderer.
if (result.Contains("bing.search", StringComparison.OrdinalIgnoreCase))
{
var promptTemplateFactory = new BasicPromptTemplateFactory();
var promptTemplateFactory = new KernelPromptTemplateFactory();
var promptTemplate = promptTemplateFactory.Create(result, new PromptTemplateConfig());

Console.WriteLine("---- Fetching information from Bing...");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Plugins.Core;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using RepoUtils;
using Resources;

Expand Down Expand Up @@ -41,7 +40,7 @@
* var context = kernel.CreateNewContext();
* ... set variables ...
*
* var promptRenderer = new BasicPromptTemplateEngine();
* var promptRenderer = new KernelPromptTemplateEngine();
* string renderedPrompt = await promptRenderer.RenderAsync("...prompt template...", context);
*/
// ReSharper disable CommentTypo
Expand Down Expand Up @@ -87,7 +86,7 @@ public static async Task RunAsync()
// Instantiate the prompt template factory, which we will use to turn prompt templates
// into strings, that we will store into a Chat history object, which is then sent
// to the Chat Model.
var promptTemplateFactory = new BasicPromptTemplateFactory();
var promptTemplateFactory = new KernelPromptTemplateFactory();

// Render the system prompt. This string is used to configure the chat.
// This contains the context, ie a piece of a wikipedia page selected by the user.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
using Microsoft.SemanticKernel.Connectors.AI.OpenAI;
using Microsoft.SemanticKernel.Plugins.Core;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using RepoUtils;

// ReSharper disable once InconsistentNaming
Expand Down Expand Up @@ -56,7 +55,7 @@ public static async Task RunAsync()

// This allows to see the prompt before it's sent to OpenAI
Console.WriteLine("--- Rendered Prompt");
var promptTemplateFactory = new BasicPromptTemplateFactory();
var promptTemplateFactory = new KernelPromptTemplateFactory();
var promptTemplate = promptTemplateFactory.Create(FunctionDefinition, new PromptTemplateConfig());
var renderedPrompt = await promptTemplate.RenderAsync(context);
Console.WriteLine(renderedPrompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using Microsoft.SemanticKernel.TemplateEngine.Handlebars;
using RepoUtils;

Expand Down Expand Up @@ -42,7 +41,7 @@ public static async Task RunAsync()
.Build();

var promptTemplateFactory = new AggregatorPromptTemplateFactory(
new BasicPromptTemplateFactory(),
new KernelPromptTemplateFactory(),
new HandlebarsPromptTemplateFactory());

var skPrompt = "Hello AI, my name is {{$name}}. What is the origin of my name?";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
<ProjectReference Include="..\..\src\Experimental\Assistants\Experimental.Assistants.csproj" />
<ProjectReference Include="..\..\src\Experimental\Orchestration.Flow\Experimental.Orchestration.Flow.csproj" />
<ProjectReference Include="..\..\src\Extensions\Reliability.Basic\Reliability.Basic.csproj" />
<ProjectReference Include="..\..\src\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\..\src\Extensions\Reliability.Polly\Reliability.Polly.csproj" />
<ProjectReference Include="..\..\src\Extensions\TemplateEngine.Handlebars\TemplateEngine.Handlebars.csproj" />
<ProjectReference Include="..\..\src\Planners\Planners.OpenAI\Planners.OpenAI.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
<ItemGroup>
<ProjectReference Include="..\..\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" />
<ProjectReference Include="..\..\Extensions\Reliability.Basic\Reliability.Basic.csproj" />
<ProjectReference Include="..\..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\..\Plugins\Plugins.Memory\Plugins.Memory.csproj" />
<ProjectReference Include="..\..\Plugins\Plugins.Web\Plugins.Web.csproj" />
<ProjectReference Include="..\Orchestration.Flow\Experimental.Orchestration.Flow.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.SemanticKernel.Experimental.Orchestration.Abstractions;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;

namespace Microsoft.SemanticKernel.Experimental.Orchestration.Execution;

Expand Down Expand Up @@ -684,7 +683,7 @@ private async Task<ContextVariables> ExecuteStepAsync(FlowStep step, string sess

private ISKFunction ImportSemanticFunction(IKernel kernel, string functionName, string promptTemplate, PromptTemplateConfig config)
{
var factory = new BasicPromptTemplateFactory(kernel.LoggerFactory);
var factory = new KernelPromptTemplateFactory(kernel.LoggerFactory);
var template = factory.Create(promptTemplate, config);

return kernel.RegisterSemanticFunction(RestrictedPluginName, functionName, config, template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Microsoft.SemanticKernel.AI.ChatCompletion;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;

namespace Microsoft.SemanticKernel.Experimental.Orchestration.Execution;

Expand Down Expand Up @@ -235,7 +234,7 @@ private SKContext CreateActionContext(Dictionary<string, string> actionVariables

private ISKFunction ImportSemanticFunction(IKernel kernel, string functionName, string promptTemplate, PromptTemplateConfig config)
{
var factory = new BasicPromptTemplateFactory(kernel.LoggerFactory);
var factory = new KernelPromptTemplateFactory(kernel.LoggerFactory);
var template = factory.Create(promptTemplate, config);

return kernel.RegisterSemanticFunction(RestrictedPluginName, functionName, config, template);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
<PackageReference Include="YamlDotNet" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\..\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Reliability.Basic\Reliability.Basic.csproj" />
<ProjectReference Include="..\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\Reliability.Polly\Reliability.Polly.csproj" />
<ProjectReference Include="..\TemplateEngine.Handlebars\TemplateEngine.Handlebars.csproj" />
</ItemGroup>
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Reliability.Basic;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;
using Xunit.Abstractions;
Expand Down Expand Up @@ -340,7 +339,7 @@ public async Task MultipleServiceLoadPromptConfigTestAsync()
{
// Arrange
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);
var promptTemplateFactory = new BasicPromptTemplateFactory();
var promptTemplateFactory = new KernelPromptTemplateFactory();
this.ConfigureAzureOpenAI(builder);
this.ConfigureInvalidAzureOpenAI(builder);

Expand Down
1 change: 0 additions & 1 deletion dotnet/src/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
<ProjectReference Include="..\Connectors\Connectors.Memory.Postgres\Connectors.Memory.Postgres.csproj" />
<ProjectReference Include="..\Connectors\Connectors.Memory.Weaviate\Connectors.Memory.Weaviate.csproj" />
<ProjectReference Include="..\Extensions\Reliability.Basic\Reliability.Basic.csproj" />
<ProjectReference Include="..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\Extensions\Reliability.Polly\Reliability.Polly.csproj" />
<ProjectReference Include="..\Planners\Planners.OpenAI\Planners.OpenAI.csproj" />
<ProjectReference Include="..\Plugins\Plugins.Core\Plugins.Core.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using System.Threading.Tasks;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.TemplateEngine.Basic;
using Microsoft.SemanticKernel.TemplateEngine;
using Xunit;
using Xunit.Abstractions;

Expand All @@ -23,7 +23,7 @@ public sealed class PromptTemplateEngineTests : IDisposable
public PromptTemplateEngineTests(ITestOutputHelper output)
{
this._logger = new RedirectOutput(output);
this._target = new BasicPromptTemplateEngine();
this._target = new KernelPromptTemplateEngine();
}

[Fact]
Expand Down Expand Up @@ -208,7 +208,7 @@ public string MyFunction3(string name, DateTime birthdate, string exclamation)
#region internals

private readonly RedirectOutput _logger;
private readonly BasicPromptTemplateEngine _target;
private readonly KernelPromptTemplateEngine _target;

private static IEnumerable<string[]> GetTestData(string file)
{
Expand Down
1 change: 0 additions & 1 deletion dotnet/src/Planners/Planners.Core/Planners.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,6 @@
<ItemGroup>
<ProjectReference Include="..\..\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
<ProjectReference Include="..\..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
</ItemGroup>

<ItemGroup>
Expand Down
5 changes: 2 additions & 3 deletions dotnet/src/Planners/Planners.Core/Stepwise/StepwisePlanner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using Microsoft.SemanticKernel.Planning;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TemplateEngine;
using Microsoft.SemanticKernel.TemplateEngine.Basic;

#pragma warning disable IDE0130
// ReSharper disable once CheckNamespace - Using NS of Plan
Expand Down Expand Up @@ -61,7 +60,7 @@ public StepwisePlanner(
this._promptConfig.SetMaxTokens(this.Config.MaxCompletionTokens);

// Initialize prompt renderer
this._promptTemplateFactory = new BasicPromptTemplateFactory(this._kernel.LoggerFactory);
this._promptTemplateFactory = new KernelPromptTemplateFactory(this._kernel.LoggerFactory);

// Import native functions
this._nativeFunctions = this._kernel.ImportFunctions(this, RestrictedPluginName);
Expand Down Expand Up @@ -651,7 +650,7 @@ private static void AddExecutionStatsToContext(List<SystemStep> stepsTaken, SKCo
/// <summary>
/// The prompt renderer to use for the system step
/// </summary>
private readonly BasicPromptTemplateFactory _promptTemplateFactory;
private readonly KernelPromptTemplateFactory _promptTemplateFactory;

/// <summary>
/// The prompt config to use for the system step
Expand Down
1 change: 0 additions & 1 deletion dotnet/src/Planners/Planners.OpenAI/Planners.OpenAI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
<ProjectReference Include="..\..\Functions\Functions.OpenAPI\Functions.OpenAPI.csproj" />
<ProjectReference Include="..\..\SemanticKernel.Abstractions\SemanticKernel.Abstractions.csproj" />
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
<ProjectReference Include="..\..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
<ProjectReference Include="..\Planners.Core\Planners.Core.csproj" />
</ItemGroup>
</Project>
Loading

0 comments on commit 38fe2f0

Please sign in to comment.