Skip to content

Commit

Permalink
Add ActionPlanner
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Apr 26, 2023
1 parent 41f35fb commit c5c64ec
Show file tree
Hide file tree
Showing 82 changed files with 1,182 additions and 133 deletions.
24 changes: 24 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SemanticKernel.MetaPackage"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ImportDocument", "..\samples\apps\copilot-chat-app\importdocument\ImportDocument.csproj", "{94E14913-D600-4448-A5C5-A268C23B2C3B}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Planning.ActionPlanner", "src\Extensions\Planning.ActionPlanner\Planning.ActionPlanner.csproj", "{994BEF0B-E277-4D10-BB13-FE670D26620D}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "extensions", "extensions", "{078F96B4-09E1-4E0E-B214-F71A4F4BF633}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extensions.UnitTests", "src\Extensions\Extensions.UnitTests\Extensions.UnitTests.csproj", "{F51017A9-15C8-472D-893C-080046D710A6}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Planning.SequentialPlanner", "src\Extensions\Planning.SequentialPlanner\Planning.SequentialPlanner.csproj", "{A350933D-F9D5-4AD3-8C4F-B856B5020297}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -177,6 +185,18 @@ Global
{94E14913-D600-4448-A5C5-A268C23B2C3B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{94E14913-D600-4448-A5C5-A268C23B2C3B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{94E14913-D600-4448-A5C5-A268C23B2C3B}.Release|Any CPU.Build.0 = Release|Any CPU
{994BEF0B-E277-4D10-BB13-FE670D26620D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{994BEF0B-E277-4D10-BB13-FE670D26620D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{994BEF0B-E277-4D10-BB13-FE670D26620D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{994BEF0B-E277-4D10-BB13-FE670D26620D}.Release|Any CPU.Build.0 = Release|Any CPU
{F51017A9-15C8-472D-893C-080046D710A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F51017A9-15C8-472D-893C-080046D710A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F51017A9-15C8-472D-893C-080046D710A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F51017A9-15C8-472D-893C-080046D710A6}.Release|Any CPU.Build.0 = Release|Any CPU
{A350933D-F9D5-4AD3-8C4F-B856B5020297}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A350933D-F9D5-4AD3-8C4F-B856B5020297}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A350933D-F9D5-4AD3-8C4F-B856B5020297}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A350933D-F9D5-4AD3-8C4F-B856B5020297}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -209,6 +229,10 @@ Global
{627742DB-1E52-468A-99BD-6FF1A542D25B} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
{E3299033-EB81-4C4C-BCD9-E8DC40937969} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
{94E14913-D600-4448-A5C5-A268C23B2C3B} = {FA3720F1-C99A-49B2-9577-A940257098BF}
{078F96B4-09E1-4E0E-B214-F71A4F4BF633} = {831DDCA2-7D2C-4C31-80DB-6BDB3E1F7AE0}
{994BEF0B-E277-4D10-BB13-FE670D26620D} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{F51017A9-15C8-472D-893C-080046D710A6} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
{A350933D-F9D5-4AD3-8C4F-B856B5020297} = {078F96B4-09E1-4E0E-B214-F71A4F4BF633}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<AssemblyName>SemanticKernel.Extensions.UnitTests</AssemblyName>
<RootNamespace>SemanticKernel.Extensions.UnitTests</RootNamespace>
<TargetFramework>net6.0</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>disable</ImplicitUsings>
<IsPackable>false</IsPackable>
<NoWarn>CA2007,VSTHRD111</NoWarn>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Moq" />
<PackageReference Include="xunit" />
<PackageReference Include="xunit.runner.visualstudio">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Logging.Console" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Planning.ActionPlanner\Planning.ActionPlanner.csproj" />
<ProjectReference Include="..\Planning.SequentialPlanner\Planning.SequentialPlanner.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@
using Microsoft.SemanticKernel.SemanticFunctions;
using Microsoft.SemanticKernel.SkillDefinition;
using Moq;
using SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner.TestHelpers;
using Xunit;
using Xunit.Abstractions;

namespace SemanticKernel.UnitTests.Planning;
namespace SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner;

public class SequentialPlanParserTests
public class PlanParserTests
{
private readonly ITestOutputHelper _testOutputHelper;

public SequentialPlanParserTests(ITestOutputHelper testOutputHelper)
public PlanParserTests(ITestOutputHelper testOutputHelper)
{
this._testOutputHelper = testOutputHelper;
}
Expand Down Expand Up @@ -132,6 +133,7 @@ public void CanCallToPlanFromXml()
</plan>";

// Act
// TODO: unit tests should not depend on the code under test
var plan = planString.ToPlanFromXml(kernel.CreateNewContext());

// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,14 @@
using System.Threading.Tasks;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning.Planners;
using Microsoft.SemanticKernel.Planning.Sequential;
using Microsoft.SemanticKernel.SkillDefinition;
using Moq;
using SemanticKernel.UnitTests.XunitHelpers;
using SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner.TestHelpers;
using SemanticKernel.Extensions.UnitTests.XunitHelpers;
using Xunit;

namespace SemanticKernel.UnitTests.Planning;
namespace SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner;

public class SKContextExtensionsTests
{
Expand All @@ -22,7 +23,7 @@ public async Task CanCallGetAvailableFunctionsWithNoFunctionsAsync()
// Arrange
var variables = new ContextVariables();
var skills = new SkillCollection();
var logger = ConsoleLogger.Log;
var logger = TestConsoleLogger.Log;
var cancellationToken = default(CancellationToken);

// Arrange Mock Memory and Result
Expand All @@ -38,7 +39,7 @@ public async Task CanCallGetAvailableFunctionsWithNoFunctionsAsync()

// Arrange GetAvailableFunctionsAsync parameters
var context = new SKContext(variables, memory.Object, skills.ReadOnlySkillCollection, logger, cancellationToken);
var config = new PlannerConfig();
var config = new SequentialPlannerConfig();
var semanticQuery = "test";

// Act
Expand All @@ -56,7 +57,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsAsync()
{
// Arrange
var variables = new ContextVariables();
var logger = ConsoleLogger.Log;
var logger = TestConsoleLogger.Log;
var cancellationToken = default(CancellationToken);

// Arrange FunctionView
Expand Down Expand Up @@ -91,7 +92,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsAsync()

// Arrange GetAvailableFunctionsAsync parameters
var context = new SKContext(variables, memory.Object, skills.Object, logger, cancellationToken);
var config = new PlannerConfig();
var config = new SequentialPlannerConfig();
var semanticQuery = "test";

// Act
Expand Down Expand Up @@ -120,7 +121,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsWithRelevancyAsync()
{
// Arrange
var variables = new ContextVariables();
var logger = ConsoleLogger.Log;
var logger = TestConsoleLogger.Log;
var cancellationToken = default(CancellationToken);

// Arrange FunctionView
Expand Down Expand Up @@ -155,7 +156,7 @@ public async Task CanCallGetAvailableFunctionsWithFunctionsWithRelevancyAsync()

// Arrange GetAvailableFunctionsAsync parameters
var context = new SKContext(variables, memory.Object, skills.Object, logger, cancellationToken);
var config = new PlannerConfig() { RelevancyThreshold = 0.78 };
var config = new SequentialPlannerConfig { RelevancyThreshold = 0.78 };
var semanticQuery = "test";

// Act
Expand Down Expand Up @@ -185,7 +186,7 @@ public async Task CanCallGetAvailableFunctionsAsyncWithDefaultRelevancyAsync()
// Arrange
var variables = new ContextVariables();
var skills = new SkillCollection();
var logger = ConsoleLogger.Log;
var logger = TestConsoleLogger.Log;
var cancellationToken = default(CancellationToken);

// Arrange Mock Memory and Result
Expand All @@ -202,7 +203,7 @@ public async Task CanCallGetAvailableFunctionsAsyncWithDefaultRelevancyAsync()

// Arrange GetAvailableFunctionsAsync parameters
var context = new SKContext(variables, memory.Object, skills.ReadOnlySkillCollection, logger, cancellationToken);
var config = new PlannerConfig() { RelevancyThreshold = 0.78 };
var config = new SequentialPlannerConfig { RelevancyThreshold = 0.78 };
var semanticQuery = "test";

// Act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@
using Microsoft.SemanticKernel.AI.TextCompletion;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning.Planners;
using Microsoft.SemanticKernel.SemanticFunctions;
using Microsoft.SemanticKernel.SkillDefinition;
using Moq;
using Xunit;

namespace SemanticKernel.UnitTests.Planning;
namespace SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner;

public sealed class PlanningTests
public sealed class SequentialPlannerTests
{
[Theory]
[InlineData("Write a poem or joke and send it in an e-mail to Kai.")]
Expand Down Expand Up @@ -116,7 +115,7 @@ public async Task ItCanCreatePlanAsync(string goal)
It.IsAny<SemanticFunctionConfig>()
)).Returns(mockFunctionFlowFunction.Object);

var planner = new SequentialPlanner(kernel.Object);
var planner = new Microsoft.SemanticKernel.Planning.SequentialPlanner(kernel.Object);

// Act
var plan = await planner.CreatePlanAsync(goal);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Threading;

// ReSharper disable once CheckNamespace // Extension methods
namespace System;

/// <summary>
/// Exception extension methods.
/// </summary>
internal static class ExceptionExtensions
{
/// <summary>
/// Check if an exception is of a type that should not be caught by the kernel.
/// </summary>
/// <param name="ex">Exception.</param>
/// <returns>True if <paramref name="ex"/> is a critical exception and should not be caught.</returns>
internal static bool IsCriticalException(this Exception ex)
=> ex is OutOfMemoryException
or ThreadAbortException
or AccessViolationException
or AppDomainUnloadedException
or BadImageFormatException
or CannotUnloadAppDomainException
or InvalidProgramException
or StackOverflowException;
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
// Copyright (c) Microsoft. All rights reserved.

using System.Linq;
using Microsoft.SemanticKernel.SkillDefinition;

#pragma warning disable IDE0130
// ReSharper disable once CheckNamespace // Extension methods
namespace Microsoft.SemanticKernel.SkillDefinition;
#pragma warning restore IDE0130
namespace SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner.TestHelpers;

internal static class FunctionViewExtensions
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel.SkillDefinition;

// ReSharper disable once CheckNamespace - Using NS of SKContext
namespace Microsoft.SemanticKernel.Orchestration;

internal static class SKContextExtensions
{
/// <summary>
/// Simple extension method to check if a function is registered in the SKContext.
/// </summary>
/// <param name="context">The SKContext to check</param>
/// <param name="skillName">The skill name</param>
/// <param name="functionName">The function name</param>
/// <param name="registeredFunction">The registered function, if found</param>
internal static bool IsFunctionRegistered(this SKContext context, string skillName, string functionName, out ISKFunction? registeredFunction)
{
context.ThrowIfSkillCollectionNotSet();

if (context.Skills!.HasNativeFunction(skillName, functionName))
{
registeredFunction = context.Skills.GetNativeFunction(skillName, functionName);
return true;
}

if (context.Skills.HasNativeFunction(functionName))
{
registeredFunction = context.Skills.GetNativeFunction(functionName);
return true;
}

if (context.Skills.HasSemanticFunction(skillName, functionName))
{
registeredFunction = context.Skills.GetSemanticFunction(skillName, functionName);
return true;
}

registeredFunction = null;
return false;
}

/// <summary>
/// Ensures the context has a skill collection available
/// </summary>
/// <param name="context">SK execution context</param>
internal static void ThrowIfSkillCollectionNotSet(this SKContext context)
{
if (context.Skills == null)
{
throw new KernelException(
KernelException.ErrorCodes.SkillCollectionNotSet,
"Skill collection not found in the context");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
using System.Text;
using System.Xml;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Planning;

namespace Microsoft.SemanticKernel.Planning;
namespace SemanticKernel.Extensions.UnitTests.Planning.SequentialPlanner.TestHelpers;

/// <summary>
/// Parse sequential plan text into a plan.
Expand Down

0 comments on commit c5c64ec

Please sign in to comment.