mirror of
https://github.com/microsoft/semantic-kernel.git
synced 2026-10-12 02:19:06 +00:00
.Net: Merge BasicPromptTemplateFactory to ms.sk.core (#3518)
### 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 😄
This commit is contained in:
@@ -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}"
|
||||
@@ -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
|
||||
@@ -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}
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -6,7 +6,6 @@ using Microsoft.SemanticKernel;
|
||||
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
|
||||
@@ -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);
|
||||
|
||||
@@ -8,7 +8,6 @@ using Microsoft.SemanticKernel.Plugins.Web;
|
||||
using Microsoft.SemanticKernel.Plugins.Web.Bing;
|
||||
using Microsoft.SemanticKernel.Plugins.Web.Google;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using RepoUtils;
|
||||
|
||||
/// <summary>
|
||||
@@ -150,7 +149,7 @@ Answer: ";
|
||||
// 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...");
|
||||
|
||||
@@ -7,7 +7,6 @@ using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Plugins.Core;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using RepoUtils;
|
||||
using Resources;
|
||||
|
||||
@@ -41,7 +40,7 @@ using Resources;
|
||||
* 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
|
||||
@@ -87,7 +86,7 @@ public static class Example30_ChatWithPrompts
|
||||
// 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.
|
||||
|
||||
+1
-2
@@ -6,7 +6,6 @@ using Microsoft.SemanticKernel;
|
||||
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
|
||||
@@ -56,7 +55,7 @@ public static class Example56_TemplateNativeFunctionsWithMultipleArguments
|
||||
|
||||
// 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);
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.Threading.Tasks;
|
||||
using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Handlebars;
|
||||
using RepoUtils;
|
||||
|
||||
@@ -42,7 +41,7 @@ public static class Example64_MultiplePromptTemplates
|
||||
.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?";
|
||||
|
||||
@@ -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" />
|
||||
|
||||
-1
@@ -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" />
|
||||
|
||||
@@ -12,7 +12,6 @@ using Microsoft.SemanticKernel.Diagnostics;
|
||||
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;
|
||||
|
||||
@@ -684,7 +683,7 @@ internal class FlowExecutor : IFlowExecutor
|
||||
|
||||
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);
|
||||
|
||||
@@ -10,7 +10,6 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.AI.ChatCompletion;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
|
||||
namespace Microsoft.SemanticKernel.Experimental.Orchestration.Execution;
|
||||
|
||||
@@ -235,7 +234,7 @@ internal sealed class ReActEngine
|
||||
|
||||
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);
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -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>
|
||||
|
||||
-55
@@ -1,55 +0,0 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
|
||||
#pragma warning disable IDE0130
|
||||
// ReSharper disable once CheckNamespace - Using the namespace of IKernel
|
||||
namespace Microsoft.SemanticKernel;
|
||||
#pragma warning restore IDE0130
|
||||
|
||||
/// <summary>
|
||||
/// Class for extensions methods to define semantic functions using the basic prompt template factory.
|
||||
/// </summary>
|
||||
public static class KernelBasicPromptTemplateExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Build and register a function in the internal function collection, in a global generic plugin.
|
||||
/// </summary>
|
||||
/// <param name="kernel">Semantic Kernel instance</param>
|
||||
/// <param name="functionName">Name of the semantic function. The name can contain only alphanumeric chars + underscore.</param>
|
||||
/// <param name="templateString">Prompt template string.</param>
|
||||
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
|
||||
/// <returns>A C# function wrapping AI logic, usually defined with natural language</returns>
|
||||
public static ISKFunction RegisterSemanticFunction(
|
||||
this IKernel kernel,
|
||||
string functionName,
|
||||
string templateString,
|
||||
PromptTemplateConfig promptTemplateConfig)
|
||||
{
|
||||
var promptTemplateFactory = new BasicPromptTemplateFactory(loggerFactory: kernel.LoggerFactory);
|
||||
var promptTemplate = promptTemplateFactory.Create(templateString, promptTemplateConfig);
|
||||
return kernel.RegisterSemanticFunction(functionName, promptTemplateConfig, promptTemplate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build and register a function in the internal function collection, in a global generic plugin.
|
||||
/// </summary>
|
||||
/// <param name="kernel">Semantic Kernel instance</param>
|
||||
/// <param name="pluginName"></param>
|
||||
/// <param name="functionName">Name of the semantic function. The name can contain only alphanumeric chars + underscore.</param>
|
||||
/// <param name="templateString">Prompt template string.</param>
|
||||
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
|
||||
/// <returns>A C# function wrapping AI logic, usually defined with natural language</returns>
|
||||
public static ISKFunction RegisterSemanticFunction(
|
||||
this IKernel kernel,
|
||||
string pluginName,
|
||||
string functionName,
|
||||
string templateString,
|
||||
PromptTemplateConfig promptTemplateConfig)
|
||||
{
|
||||
var promptTemplateFactory = new BasicPromptTemplateFactory(loggerFactory: kernel.LoggerFactory);
|
||||
var promptTemplate = promptTemplateFactory.Create(templateString, promptTemplateConfig);
|
||||
return kernel.RegisterSemanticFunction(pluginName, functionName, promptTemplateConfig, promptTemplate);
|
||||
}
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<!-- THIS PROPERTY GROUP MUST COME FIRST -->
|
||||
<AssemblyName>Microsoft.SemanticKernel.TemplateEngine.Basic</AssemblyName>
|
||||
<RootNamespace>Microsoft.SemanticKernel.TemplateEngine.Basic</RootNamespace>
|
||||
<TargetFramework>netstandard2.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(RepoRoot)/dotnet/nuget/nuget-package.props" />
|
||||
|
||||
<PropertyGroup>
|
||||
<!-- NuGet Package Settings -->
|
||||
<Title>Semantic Kernel - Basic Prompt Template Engine</Title>
|
||||
<Description>Semantic Kernel Basic Prompt Template Engine</Description>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<InternalsVisibleTo Include="SemanticKernel.Extensions.UnitTests" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\SemanticKernel.Core\SemanticKernel.Core.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
@@ -12,7 +12,6 @@ using Microsoft.SemanticKernel.Diagnostics;
|
||||
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;
|
||||
@@ -340,7 +339,7 @@ public sealed class OpenAICompletionTests : IDisposable
|
||||
{
|
||||
// Arrange
|
||||
var builder = this._kernelBuilder.WithLoggerFactory(this._logger);
|
||||
var promptTemplateFactory = new BasicPromptTemplateFactory();
|
||||
var promptTemplateFactory = new KernelPromptTemplateFactory();
|
||||
this.ConfigureAzureOpenAI(builder);
|
||||
this.ConfigureInvalidAzureOpenAI(builder);
|
||||
|
||||
|
||||
@@ -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" />
|
||||
|
||||
@@ -7,7 +7,7 @@ using System.IO;
|
||||
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;
|
||||
|
||||
@@ -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]
|
||||
@@ -208,7 +208,7 @@ public sealed class PromptTemplateEngineTests : IDisposable
|
||||
#region internals
|
||||
|
||||
private readonly RedirectOutput _logger;
|
||||
private readonly BasicPromptTemplateEngine _target;
|
||||
private readonly KernelPromptTemplateEngine _target;
|
||||
|
||||
private static IEnumerable<string[]> GetTestData(string file)
|
||||
{
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -18,7 +18,6 @@ using Microsoft.SemanticKernel.Orchestration;
|
||||
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
|
||||
@@ -61,7 +60,7 @@ public class StepwisePlanner : IStepwisePlanner
|
||||
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);
|
||||
@@ -651,7 +650,7 @@ public class StepwisePlanner : IStepwisePlanner
|
||||
/// <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
|
||||
|
||||
@@ -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>
|
||||
|
||||
@@ -15,7 +15,6 @@ using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Functions.OpenAPI.Model;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
|
||||
#pragma warning disable IDE0130
|
||||
// ReSharper disable once CheckNamespace - Using NS of Plan
|
||||
@@ -41,7 +40,7 @@ public sealed class FunctionCallingStepwisePlanner
|
||||
this._chatCompletion = kernel.GetService<IChatCompletion>();
|
||||
|
||||
// Initialize prompt renderer
|
||||
this._promptTemplateFactory = new BasicPromptTemplateFactory(this._kernel.LoggerFactory);
|
||||
this._promptTemplateFactory = new KernelPromptTemplateFactory(this._kernel.LoggerFactory);
|
||||
|
||||
// Set up Config with default values and excluded plugins
|
||||
this.Config = config ?? new();
|
||||
@@ -297,7 +296,7 @@ public sealed class FunctionCallingStepwisePlanner
|
||||
/// <summary>
|
||||
/// The prompt renderer to use for the system step
|
||||
/// </summary>
|
||||
private readonly BasicPromptTemplateFactory _promptTemplateFactory;
|
||||
private readonly KernelPromptTemplateFactory _promptTemplateFactory;
|
||||
|
||||
/// <summary>
|
||||
/// The name to use when creating semantic functions that are restricted from plan creation
|
||||
|
||||
@@ -4,9 +4,6 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.AI;
|
||||
@@ -25,6 +22,25 @@ namespace Microsoft.SemanticKernel;
|
||||
/// </summary>
|
||||
public static class KernelSemanticFunctionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Build and register a function in the internal function collection, in a global generic plugin.
|
||||
/// </summary>
|
||||
/// <param name="kernel">Semantic Kernel instance</param>
|
||||
/// <param name="functionName">Name of the semantic function. The name can contain only alphanumeric chars + underscore.</param>
|
||||
/// <param name="templateString">Prompt template string.</param>
|
||||
/// <param name="promptTemplateConfig">Prompt template configuration.</param>
|
||||
/// <returns>A C# function wrapping AI logic, usually defined with natural language</returns>
|
||||
public static ISKFunction RegisterSemanticFunction(
|
||||
this IKernel kernel,
|
||||
string functionName,
|
||||
string templateString,
|
||||
PromptTemplateConfig promptTemplateConfig)
|
||||
{
|
||||
var promptTemplateFactory = new KernelPromptTemplateFactory(loggerFactory: kernel.LoggerFactory);
|
||||
var promptTemplate = promptTemplateFactory.Create(templateString, promptTemplateConfig);
|
||||
return kernel.RegisterSemanticFunction(functionName, promptTemplateConfig, promptTemplate);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Build and register a function in the internal function collection, in a global generic plugin.
|
||||
/// </summary>
|
||||
@@ -127,7 +143,7 @@ public static class KernelSemanticFunctionExtensions
|
||||
Verify.ValidFunctionName(functionName);
|
||||
if (!string.IsNullOrEmpty(pluginName)) { Verify.ValidPluginName(pluginName); }
|
||||
|
||||
var factory = promptTemplateFactory ?? CreateDefaultPromptTemplateFactory(kernel);
|
||||
var factory = promptTemplateFactory ?? new KernelPromptTemplateFactory(kernel.LoggerFactory);
|
||||
IPromptTemplate promptTemplateInstance = factory.Create(promptTemplate, promptTemplateConfig);
|
||||
|
||||
// TODO: manage overwrites, potentially error out
|
||||
@@ -247,7 +263,7 @@ public static class KernelSemanticFunctionExtensions
|
||||
const string ConfigFile = "config.json";
|
||||
const string PromptFile = "skprompt.txt";
|
||||
|
||||
var factory = promptTemplateFactory ?? CreateDefaultPromptTemplateFactory(kernel);
|
||||
var factory = promptTemplateFactory ?? new KernelPromptTemplateFactory(kernel.LoggerFactory);
|
||||
var functions = new Dictionary<string, ISKFunction>();
|
||||
|
||||
ILogger? logger = null;
|
||||
@@ -312,99 +328,6 @@ public static class KernelSemanticFunctionExtensions
|
||||
kernel.LoggerFactory
|
||||
);
|
||||
}
|
||||
|
||||
private const string BasicTemplateFactoryAssemblyName = "Microsoft.SemanticKernel.TemplateEngine.Basic";
|
||||
private const string BasicTemplateFactoryTypeName = "BasicPromptTemplateFactory";
|
||||
private static bool s_promptTemplateFactoryInitialized = false;
|
||||
private static Type? s_promptTemplateFactoryType = null;
|
||||
|
||||
/// <summary>
|
||||
/// Create a default prompt template factory.
|
||||
///
|
||||
/// This is a temporary solution to avoid breaking existing clients.
|
||||
/// There will be a separate task to add support for registering instances of IPromptTemplateEngine and obsoleting the current approach.
|
||||
///
|
||||
/// </summary>
|
||||
/// <param name="kernel">Kernel instance</param>
|
||||
/// <returns>Instance of <see cref="IPromptTemplateEngine"/>.</returns>
|
||||
private static IPromptTemplateFactory CreateDefaultPromptTemplateFactory(IKernel kernel)
|
||||
{
|
||||
#pragma warning disable CS0618 // Type or member is obsolete
|
||||
if (kernel.PromptTemplateEngine is not null)
|
||||
{
|
||||
return new PromptTemplateFactory(kernel.PromptTemplateEngine);
|
||||
}
|
||||
#pragma warning restore CS0618 // Type or member is obsolete
|
||||
|
||||
if (!s_promptTemplateFactoryInitialized)
|
||||
{
|
||||
s_promptTemplateFactoryType = GetPromptTemplateFactoryType();
|
||||
s_promptTemplateFactoryInitialized = true;
|
||||
}
|
||||
|
||||
if (s_promptTemplateFactoryType is not null)
|
||||
{
|
||||
var constructor = s_promptTemplateFactoryType.GetConstructor(new Type[] { typeof(ILoggerFactory) });
|
||||
if (constructor is not null)
|
||||
{
|
||||
#pragma warning disable CS8601 // Null logger factory is OK
|
||||
var factory = (IPromptTemplateFactory)constructor.Invoke(new object[] { kernel.LoggerFactory });
|
||||
if (factory is not null)
|
||||
{
|
||||
return factory;
|
||||
}
|
||||
#pragma warning restore CS8601
|
||||
}
|
||||
}
|
||||
|
||||
return new NullPromptTemplateFactory();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the prompt template engine type if available
|
||||
/// </summary>
|
||||
/// <returns>The type for the prompt template engine if available</returns>
|
||||
private static Type? GetPromptTemplateFactoryType()
|
||||
{
|
||||
try
|
||||
{
|
||||
var assembly = Assembly.Load(BasicTemplateFactoryAssemblyName);
|
||||
|
||||
return assembly.ExportedTypes.Single(type =>
|
||||
type.Name.Equals(BasicTemplateFactoryTypeName, StringComparison.Ordinal) &&
|
||||
type.GetInterface(nameof(IPromptTemplateFactory)) is not null);
|
||||
}
|
||||
catch (Exception ex) when (!ex.IsCriticalException())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NullPromptTemplateFactory : IPromptTemplateFactory
|
||||
{
|
||||
public IPromptTemplate Create(string templateString, PromptTemplateConfig promptTemplateConfig)
|
||||
{
|
||||
return new NullPromptTemplate(templateString);
|
||||
}
|
||||
}
|
||||
|
||||
private sealed class NullPromptTemplate : IPromptTemplate
|
||||
{
|
||||
private readonly string _templateText;
|
||||
|
||||
public NullPromptTemplate(string templateText)
|
||||
{
|
||||
this._templateText = templateText;
|
||||
}
|
||||
|
||||
public IReadOnlyList<ParameterView> Parameters => Array.Empty<ParameterView>();
|
||||
|
||||
public Task<string> RenderAsync(SKContext executionContext, CancellationToken cancellationToken = default)
|
||||
{
|
||||
return Task.FromResult(this._templateText);
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Obsolete
|
||||
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
/// <summary>
|
||||
/// Base class for blocks parsed from a prompt template
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal enum BlockTypes
|
||||
{
|
||||
+1
-1
@@ -8,7 +8,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
#pragma warning disable CA2254 // error strings are used also internally, not just for logging
|
||||
#pragma warning disable CA1031 // IsCriticalException is an internal utility and should not be used by extensions
|
||||
+1
-1
@@ -6,7 +6,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal sealed class FunctionIdBlock : Block, ITextRendering
|
||||
{
|
||||
+1
-1
@@ -4,7 +4,7 @@ using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
/// <summary>
|
||||
/// Interface of dynamic blocks that need async IO to be rendered.
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
/// <summary>
|
||||
/// Interface of static blocks that don't need async IO to be rendered.
|
||||
+1
-1
@@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
/// <summary>
|
||||
/// A <see cref="Block"/> that represents a named argument for a function call.
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal static class Symbols
|
||||
{
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal sealed class TextBlock : Block, ITextRendering
|
||||
{
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal sealed class ValBlock : Block, ITextRendering
|
||||
{
|
||||
+1
-1
@@ -5,7 +5,7 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
internal sealed class VarBlock : Block, ITextRendering
|
||||
{
|
||||
+2
-2
@@ -6,9 +6,9 @@ using System.Text;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Simple tokenizer used for default SK template code language.
|
||||
+5
-5
@@ -10,9 +10,9 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Given a prompt, that might contain references to variables and functions:
|
||||
@@ -24,7 +24,7 @@ namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
/// - Functions do not receive the context variables, unless specified using a special variable
|
||||
/// - Functions can be invoked in order and in parallel so the context variables must be immutable when invoked within the template
|
||||
/// </summary>
|
||||
public sealed class BasicPromptTemplate : IPromptTemplate
|
||||
public sealed class KernelPromptTemplate : IPromptTemplate
|
||||
{
|
||||
/// <summary>
|
||||
/// Constructor for PromptTemplate.
|
||||
@@ -32,10 +32,10 @@ public sealed class BasicPromptTemplate : IPromptTemplate
|
||||
/// <param name="templateString">Prompt template string.</param>
|
||||
/// <param name="promptTemplateConfig">Prompt template configuration</param>
|
||||
/// <param name="loggerFactory">Logger factory</param>
|
||||
public BasicPromptTemplate(string templateString, PromptTemplateConfig promptTemplateConfig, ILoggerFactory? loggerFactory = null)
|
||||
public KernelPromptTemplate(string templateString, PromptTemplateConfig promptTemplateConfig, ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
this._loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
|
||||
this._logger = this._loggerFactory.CreateLogger(typeof(BasicPromptTemplate));
|
||||
this._logger = this._loggerFactory.CreateLogger(typeof(KernelPromptTemplate));
|
||||
this._templateString = templateString;
|
||||
this._promptTemplateConfig = promptTemplateConfig;
|
||||
this._parameters = new(() => this.InitParameters());
|
||||
+7
-7
@@ -11,9 +11,9 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Given a prompt, that might contain references to variables and functions:
|
||||
@@ -25,22 +25,22 @@ namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
/// - Functions do not receive the context variables, unless specified using a special variable
|
||||
/// - Functions can be invoked in order and in parallel so the context variables must be immutable when invoked within the template
|
||||
/// </summary>
|
||||
[Obsolete("Use BasicPromptTemplateFactory instead. This will be removed in a future release.")]
|
||||
[Obsolete("Use KernelPromptTemplateFactory instead. This will be removed in a future release.")]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public class BasicPromptTemplateEngine : IPromptTemplateEngine
|
||||
public class KernelPromptTemplateEngine : IPromptTemplateEngine
|
||||
{
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
private readonly ILogger _logger;
|
||||
private readonly TemplateTokenizer _tokenizer;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BasicPromptTemplateEngine"/> class.
|
||||
/// Initializes a new instance of the <see cref="KernelPromptTemplateEngine"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param>
|
||||
public BasicPromptTemplateEngine(ILoggerFactory? loggerFactory = null)
|
||||
public KernelPromptTemplateEngine(ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
this._loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
|
||||
this._logger = this._loggerFactory.CreateLogger(typeof(BasicPromptTemplateEngine));
|
||||
this._logger = this._loggerFactory.CreateLogger(typeof(KernelPromptTemplateEngine));
|
||||
this._tokenizer = new TemplateTokenizer(this._loggerFactory);
|
||||
}
|
||||
|
||||
+5
-5
@@ -4,20 +4,20 @@ using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Default implementation of <see cref="IPromptTemplateFactory"/> for the semantic-kernel prompt template format.
|
||||
/// </summary>
|
||||
public sealed class BasicPromptTemplateFactory : IPromptTemplateFactory
|
||||
public sealed class KernelPromptTemplateFactory : IPromptTemplateFactory
|
||||
{
|
||||
private readonly ILoggerFactory _loggerFactory;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BasicPromptTemplateFactory"/> class.
|
||||
/// Initializes a new instance of the <see cref="KernelPromptTemplateFactory"/> class.
|
||||
/// </summary>
|
||||
/// <param name="loggerFactory">The <see cref="ILoggerFactory"/> to use for logging. If null, no logging will be performed.</param>
|
||||
public BasicPromptTemplateFactory(ILoggerFactory? loggerFactory = null)
|
||||
public KernelPromptTemplateFactory(ILoggerFactory? loggerFactory = null)
|
||||
{
|
||||
this._loggerFactory = loggerFactory ?? NullLoggerFactory.Instance;
|
||||
}
|
||||
@@ -27,7 +27,7 @@ public sealed class BasicPromptTemplateFactory : IPromptTemplateFactory
|
||||
{
|
||||
if (promptTemplateConfig.TemplateFormat.Equals(PromptTemplateConfig.SemanticKernelTemplateFormat, System.StringComparison.Ordinal))
|
||||
{
|
||||
return new BasicPromptTemplate(templateString, promptTemplateConfig, this._loggerFactory);
|
||||
return new KernelPromptTemplate(templateString, promptTemplateConfig, this._loggerFactory);
|
||||
}
|
||||
|
||||
throw new SKException($"Prompt template format {promptTemplateConfig.TemplateFormat} is not supported.");
|
||||
+2
-2
@@ -4,9 +4,9 @@ using System.Collections.Generic;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
namespace Microsoft.SemanticKernel.TemplateEngine;
|
||||
|
||||
/// <summary>
|
||||
/// Simple tokenizer used for default SK template language.
|
||||
@@ -13,7 +13,6 @@ Empowers app owners to integrate cutting-edge LLM technology quickly and easily
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Extensions\Reliability.Basic\Reliability.Basic.csproj" />
|
||||
<ProjectReference Include="..\Extensions\TemplateEngine.Basic\TemplateEngine.Basic.csproj" />
|
||||
<ProjectReference Include="..\SemanticKernel.Core\SemanticKernel.Core.csproj" PrivateAssets="none" />
|
||||
<ProjectReference Include="..\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" PrivateAssets="none" />
|
||||
<ProjectReference Include="..\Plugins\Plugins.Core\Plugins.Core.csproj" PrivateAssets="none" />
|
||||
|
||||
+1
-1
@@ -11,7 +11,7 @@ using Microsoft.SemanticKernel.AI;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.Services;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Moq;
|
||||
using Xunit;
|
||||
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt.Blocks;
|
||||
+1
-1
@@ -3,7 +3,7 @@
|
||||
using Microsoft.Extensions.Logging.Abstractions;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt.Blocks;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt.Blocks;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt.Blocks;
|
||||
+1
-1
@@ -2,7 +2,7 @@
|
||||
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt.Blocks;
|
||||
+2
-2
@@ -2,8 +2,8 @@
|
||||
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt;
|
||||
+7
-8
@@ -2,26 +2,25 @@
|
||||
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt;
|
||||
namespace SemanticKernel.UnitTests.TemplateEngine;
|
||||
|
||||
public sealed class BasicPromptTemplateFactoryTests
|
||||
public sealed class KernelPromptTemplateFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void ItCreatesBasicPromptTemplateByDefault()
|
||||
{
|
||||
// Arrange
|
||||
var templateString = "{{$input}}";
|
||||
var target = new BasicPromptTemplateFactory();
|
||||
var target = new KernelPromptTemplateFactory();
|
||||
|
||||
// Act
|
||||
var result = target.Create(templateString, new PromptTemplateConfig());
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result is BasicPromptTemplate);
|
||||
Assert.True(result is KernelPromptTemplate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -29,14 +28,14 @@ public sealed class BasicPromptTemplateFactoryTests
|
||||
{
|
||||
// Arrange
|
||||
var templateString = "{{$input}}";
|
||||
var target = new BasicPromptTemplateFactory();
|
||||
var target = new KernelPromptTemplateFactory();
|
||||
|
||||
// Act
|
||||
var result = target.Create(templateString, new PromptTemplateConfig() { TemplateFormat = "semantic-kernel" });
|
||||
|
||||
// Assert
|
||||
Assert.NotNull(result);
|
||||
Assert.True(result is BasicPromptTemplate);
|
||||
Assert.True(result is KernelPromptTemplate);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -44,7 +43,7 @@ public sealed class BasicPromptTemplateFactoryTests
|
||||
{
|
||||
// Arrange
|
||||
var templateString = "{{$input}}";
|
||||
var target = new BasicPromptTemplateFactory();
|
||||
var target = new KernelPromptTemplateFactory();
|
||||
|
||||
// Act
|
||||
// Assert
|
||||
+18
-19
@@ -13,19 +13,18 @@ using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.Services;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Moq;
|
||||
using SemanticKernel.Extensions.UnitTests.XunitHelpers;
|
||||
using SemanticKernel.UnitTests.XunitHelpers;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt;
|
||||
namespace SemanticKernel.UnitTests.TemplateEngine;
|
||||
|
||||
public sealed class BasicPromptTemplateTests
|
||||
public sealed class KernelPromptTemplateTests
|
||||
{
|
||||
private const string DateFormat = "M/d/yyyy";
|
||||
private readonly BasicPromptTemplateFactory _factory;
|
||||
private readonly KernelPromptTemplateFactory _factory;
|
||||
private readonly ContextVariables _variables;
|
||||
private readonly Mock<IReadOnlyFunctionCollection> _functions;
|
||||
private readonly ITestOutputHelper _logger;
|
||||
@@ -34,10 +33,10 @@ public sealed class BasicPromptTemplateTests
|
||||
private readonly Mock<IAIServiceProvider> _serviceProvider;
|
||||
private readonly Mock<IAIServiceSelector> _serviceSelector;
|
||||
|
||||
public BasicPromptTemplateTests(ITestOutputHelper testOutputHelper)
|
||||
public KernelPromptTemplateTests(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
this._logger = testOutputHelper;
|
||||
this._factory = new BasicPromptTemplateFactory(TestConsoleLogger.LoggerFactory);
|
||||
this._factory = new KernelPromptTemplateFactory(TestConsoleLogger.LoggerFactory);
|
||||
this._variables = new ContextVariables(Guid.NewGuid().ToString("X"));
|
||||
this._functions = new Mock<IReadOnlyFunctionCollection>();
|
||||
this._kernel = new Mock<IKernel>();
|
||||
@@ -52,7 +51,7 @@ public sealed class BasicPromptTemplateTests
|
||||
// Arrange
|
||||
var template = "{$x11} This {$a} is {$_a} a {{$x11}} test {{$x11}} " +
|
||||
"template {{foo}}{{bar $a}}{{baz $_a}}{{yay $x11}}{{food a='b' c = $d}}";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
|
||||
// Act
|
||||
var blocks = target.ExtractBlocks(template);
|
||||
@@ -167,7 +166,7 @@ public sealed class BasicPromptTemplateTests
|
||||
|
||||
this._variables.Update("INPUT-BAR");
|
||||
var template = "foo-{{function}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
|
||||
this.MockFunctionRunner(functions[0]);
|
||||
|
||||
@@ -196,7 +195,7 @@ public sealed class BasicPromptTemplateTests
|
||||
|
||||
this._variables.Set("myVar", "BAR");
|
||||
var template = "foo-{{function $myVar}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
|
||||
this.MockFunctionRunner(func);
|
||||
var context = this.MockContext();
|
||||
@@ -218,7 +217,7 @@ public sealed class BasicPromptTemplateTests
|
||||
[Description("Slogan"), SKName("slogan")] string slogan,
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
var dateStr = date.ToString(BasicPromptTemplateTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
@@ -230,7 +229,7 @@ public sealed class BasicPromptTemplateTests
|
||||
this._variables.Set("input", "Mario");
|
||||
this._variables.Set("someDate", "2023-08-25T00:00:00");
|
||||
var template = "foo-{{function input=$input age='42' slogan='Let\\'s-a go!' date=$someDate}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
|
||||
this.MockFunctionRunner(func);
|
||||
var context = this.MockContext();
|
||||
@@ -252,7 +251,7 @@ public sealed class BasicPromptTemplateTests
|
||||
[Description("Slogan"), SKName("slogan")] string slogan,
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
var dateStr = date.ToString(BasicPromptTemplateTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
@@ -263,7 +262,7 @@ public sealed class BasicPromptTemplateTests
|
||||
this._variables.Set("input", "Mario");
|
||||
this._variables.Set("someDate", "2023-08-25T00:00:00");
|
||||
var template = "foo-{{function input=$input age=42 slogan='Let\\'s-a go!' date=$someDate}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var context = this.MockContext();
|
||||
|
||||
// Act
|
||||
@@ -282,7 +281,7 @@ public sealed class BasicPromptTemplateTests
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
var dateStr = date.ToString(BasicPromptTemplateTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
|
||||
@@ -294,7 +293,7 @@ public sealed class BasicPromptTemplateTests
|
||||
this._variables.Set("someDate", "2023-08-25T00:00:00");
|
||||
|
||||
var template = "foo-{{function $input age='42' slogan='Let\\'s-a go!' date=$someDate}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
|
||||
this.MockFunctionRunner(func);
|
||||
|
||||
@@ -312,7 +311,7 @@ public sealed class BasicPromptTemplateTests
|
||||
{
|
||||
// Arrange
|
||||
var template = "{{func1}} {{func2}} {{func3 $myVar}}";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
this._variables.Update("BAR");
|
||||
this._variables.Set("myVar", "BAZ");
|
||||
|
||||
@@ -369,7 +368,7 @@ public sealed class BasicPromptTemplateTests
|
||||
this._variables.Set("myVar", "BAR");
|
||||
|
||||
var template = "foo-{{function $myVar}}-baz";
|
||||
var target = (BasicPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var target = (KernelPromptTemplate)this._factory.Create(template, new PromptTemplateConfig());
|
||||
var context = this.MockContext();
|
||||
|
||||
// Act
|
||||
+9
-9
@@ -12,21 +12,21 @@ using Microsoft.SemanticKernel;
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.Orchestration;
|
||||
using Microsoft.SemanticKernel.Services;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Moq;
|
||||
using SemanticKernel.Extensions.UnitTests.XunitHelpers;
|
||||
using SemanticKernel.UnitTests.XunitHelpers;
|
||||
using Xunit;
|
||||
using Xunit.Abstractions;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt;
|
||||
namespace SemanticKernel.UnitTests.TemplateEngine;
|
||||
|
||||
[Obsolete("Use BasicPromptTemplateFactory instead. This will be removed in a future release.")]
|
||||
[EditorBrowsable(EditorBrowsableState.Never)]
|
||||
public sealed class PromptTemplateEngineTests
|
||||
{
|
||||
private const string DateFormat = "M/d/yyyy";
|
||||
private readonly BasicPromptTemplateEngine _target;
|
||||
private readonly KernelPromptTemplateEngine _target;
|
||||
private readonly ContextVariables _variables;
|
||||
private readonly Mock<IReadOnlyFunctionCollection> _functions;
|
||||
private readonly ITestOutputHelper _logger;
|
||||
@@ -38,7 +38,7 @@ public sealed class PromptTemplateEngineTests
|
||||
public PromptTemplateEngineTests(ITestOutputHelper testOutputHelper)
|
||||
{
|
||||
this._logger = testOutputHelper;
|
||||
this._target = new BasicPromptTemplateEngine(TestConsoleLogger.LoggerFactory);
|
||||
this._target = new KernelPromptTemplateEngine(TestConsoleLogger.LoggerFactory);
|
||||
this._variables = new ContextVariables(Guid.NewGuid().ToString("X"));
|
||||
this._functions = new Mock<IReadOnlyFunctionCollection>();
|
||||
this._kernel = new Mock<IKernel>();
|
||||
@@ -216,7 +216,7 @@ public sealed class PromptTemplateEngineTests
|
||||
[Description("Slogan"), SKName("slogan")] string slogan,
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
var dateStr = date.ToString(PromptTemplateEngineTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
@@ -249,7 +249,7 @@ public sealed class PromptTemplateEngineTests
|
||||
[Description("Slogan"), SKName("slogan")] string slogan,
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
var dateStr = date.ToString(PromptTemplateEngineTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
@@ -278,7 +278,7 @@ public sealed class PromptTemplateEngineTests
|
||||
[Description("Date"), SKName("date")] DateTime date)
|
||||
{
|
||||
this._logger.WriteLine("MyFunction call received, name: {0}, age: {1}, slogan: {2}, date: {3}", name, age, slogan, date);
|
||||
var dateStr = date.ToString(PromptTemplateEngineTests.DateFormat, CultureInfo.InvariantCulture);
|
||||
var dateStr = date.ToString(DateFormat, CultureInfo.InvariantCulture);
|
||||
return $"[{dateStr}] {name} ({age}): \"{slogan}\"";
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,8 +1,8 @@
|
||||
// Copyright (c) Microsoft. All rights reserved.
|
||||
|
||||
using Microsoft.SemanticKernel.Diagnostics;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Basic.Blocks;
|
||||
using Microsoft.SemanticKernel.TemplateEngine;
|
||||
using Microsoft.SemanticKernel.TemplateEngine.Blocks;
|
||||
using Xunit;
|
||||
|
||||
namespace SemanticKernel.Extensions.UnitTests.TemplateEngine.Prompt;
|
||||
Reference in New Issue
Block a user