Skip to content

Commit

Permalink
Add BasicPlanner
Browse files Browse the repository at this point in the history
  • Loading branch information
dluc committed Apr 25, 2023
1 parent 09a96c2 commit 6215f6b
Show file tree
Hide file tree
Showing 41 changed files with 758 additions and 103 deletions.
7 changes: 7 additions & 0 deletions dotnet/SK-dotnet.sln
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ 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}") = "Skills.Planning", "src\Skills\Skills.Planning\Skills.Planning.csproj", "{994BEF0B-E277-4D10-BB13-FE670D26620D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -177,6 +179,10 @@ 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
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -209,6 +215,7 @@ 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}
{994BEF0B-E277-4D10-BB13-FE670D26620D} = {9ECD1AA0-75B3-4E25-B0B5-9F0945B64974}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {FBDC56A3-86AD-4323-AA0F-201E59123B83}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ public abstract class ClientBase
}
}

Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(() =>
this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<Completions>? response = await RunRequestAsync<Response<Completions>?>(
() => this.Client.GetCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Choices.Count < 1)
{
Expand All @@ -99,9 +98,8 @@ public abstract class ClientBase
{
var options = new EmbeddingsOptions(text);

Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(() =>
this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<Embeddings>? response = await RunRequestAsync<Response<Embeddings>?>(
() => this.Client.GetEmbeddingsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Data.Count < 1)
{
Expand Down Expand Up @@ -169,9 +167,8 @@ public abstract class ClientBase
options.Messages.Add(new ChatMessage(role, message.Content));
}

Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(() =>
this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)
).ConfigureAwait(false);
Response<ChatCompletions>? response = await RunRequestAsync<Response<ChatCompletions>?>(
() => this.Client.GetChatCompletionsAsync(this.ModelId, options, cancellationToken)).ConfigureAwait(false);

if (response == null || response.Value.Choices.Count < 1)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using System.Buffers;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
Expand Down Expand Up @@ -100,9 +98,7 @@ public static List<int> Encode(string text)
// for every 1 UTF8 byte. If we can reasonably stack-allocate the space, we do, otherwise
// we temporarily rent a pooled array.
char[]? arrayPoolArray = null;
Span<char> chars = maxUtf8Length <= 256 ?
stackalloc char[maxUtf8Length] :
(arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));
Span<char> chars = maxUtf8Length <= 256 ? stackalloc char[maxUtf8Length] : (arrayPoolArray = ArrayPool<char>.Shared.Rent(maxUtf8Length));

// Rather than using separate space for the UTF8 bytes, we just reinterpret the Span<char>
// as a Span<byte>. Since our mapping is 1:1, the space required for the bytes will always
Expand Down Expand Up @@ -156,24 +152,23 @@ static unsafe int EncodingUtf8GetByteCount(ReadOnlySpan<char> chars)
static unsafe int EncodingUtf8GetBytes(ReadOnlySpan<char> chars, Span<byte> bytes)
{
fixed (char* charPtr = chars)
fixed (byte* bytesPtr = bytes)
{
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
fixed (byte* bytesPtr = bytes)
{
return Encoding.UTF8.GetBytes(charPtr, chars.Length, bytesPtr, bytes.Length);
}
}
}
}

public static List<int> Encode(StringBuilder? stringBuilder) =>
stringBuilder is not null ? Encode(stringBuilder.ToString()) :
new List<int>();
stringBuilder is not null ? Encode(stringBuilder.ToString()) : new List<int>();

public static List<int> Encode(char[]? chars) =>
chars is not null ? Encode(new string(chars)) :
new List<int>();
chars is not null ? Encode(new string(chars)) : new List<int>();

public static List<int> Encode(IEnumerable<char>? chars) =>
chars is not null ? Encode(string.Concat(chars)) :
new List<int>();
chars is not null ? Encode(string.Concat(chars)) : new List<int>();

private static List<string> BytePairEncoding(string token)
{
Expand Down Expand Up @@ -237,6 +232,7 @@ private static List<string> BytePairEncoding(string token)
{
break;
}

i = j;

if (i < (word.Count - 1) &&
Expand Down
1 change: 1 addition & 0 deletions dotnet/src/IntegrationTests/IntegrationTests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

<ItemGroup>
<ProjectReference Include="..\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" />
<ProjectReference Include="..\Skills\Skills.Planning\Skills.Planning.csproj" />
<ProjectReference Include="..\Skills\Skills.Web\Skills.Web.csproj" />
<ProjectReference Include="..\SemanticKernel\SemanticKernel.csproj" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Planning;
using Microsoft.SemanticKernel.Skills.Planning.SequentialPlannerInternals;
using SemanticKernel.IntegrationTests.Fakes;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;
using Xunit.Abstractions;

namespace SemanticKernel.IntegrationTests.Planning;
namespace SemanticKernel.IntegrationTests.Planning.SequentialPlanner;

public class SequentialPlanParserTests
{
Expand Down Expand Up @@ -57,7 +57,10 @@ public void CanCallToPlanFromXml()
</plan>";

// Act
// TODO - a test should not rely on the code under test, e.g. if there's a bug in ToPlanFromXml
// it'll result in false positives/negatives
var plan = planString.ToPlanFromXml(kernel.CreateNewContext());
// Microsoft.SemanticKernel.Skills.Planning.SequentialPlanner.SequentialPlanParser.

// Assert
Assert.NotNull(plan);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Memory;
using Microsoft.SemanticKernel.Planning.Planners;
using Microsoft.SemanticKernel.Skills.Planning.SequentialPlannerInternals;
using SemanticKernel.IntegrationTests.Fakes;
using SemanticKernel.IntegrationTests.TestSettings;
using Xunit;
using Xunit.Abstractions;

namespace SemanticKernel.IntegrationTests.Planning;
namespace SemanticKernel.IntegrationTests.Planning.SequentialPlanner;

public sealed class SequentialPlannerTests : IDisposable
{
Expand All @@ -39,7 +39,7 @@ public async Task CreatePlanFunctionFlowAsync(string prompt, string expectedFunc
IKernel kernel = this.InitializeKernel();
TestHelpers.GetSkill("FunSkill", kernel);

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

// Act
var plan = await planner.CreatePlanAsync(prompt);
Expand All @@ -61,7 +61,7 @@ public async Task CreatePlanGoalRelevantAsync(string prompt, string expectedFunc
// Import all sample skills available for demonstration purposes.
TestHelpers.ImportSampleSkills(kernel);

var planner = new SequentialPlanner(kernel, new PlannerConfig() { RelevancyThreshold = 0.70, MaxRelevantFunctions = 20 });
var planner = new Microsoft.SemanticKernel.Skills.Planning.SequentialPlanner(kernel, new PlannerConfig { RelevancyThreshold = 0.70, MaxRelevantFunctions = 20 });

// Act
var plan = await planner.CreatePlanAsync(prompt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
using System;
using Microsoft.SemanticKernel.Diagnostics;

namespace Microsoft.SemanticKernel.Planning;
namespace Microsoft.SemanticKernel.Orchestration;

/// <summary>
/// Planning exception.
Expand All @@ -20,15 +20,20 @@ public enum ErrorCodes
/// </summary>
UnknownError = -1,

/// <summary>
/// Invalid goal.
/// </summary>
InvalidGoal = 0,

/// <summary>
/// Invalid plan.
/// </summary>
InvalidPlan = 0,
InvalidPlan = 1,

/// <summary>
/// Invalid configuration.
/// </summary>
InvalidConfiguration = 1,
InvalidConfiguration = 2,
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Connectors.AI.OpenAI</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Skills.Planning</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Skills.OpenAPI</_Parameter1>
</AssemblyAttribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Empowers app owners to integrate cutting-edge LLM technology quickly and easily
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\SemanticKernel\SemanticKernel.csproj" />
<ProjectReference Include="..\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" PrivateAssets="none"/>
<ProjectReference Include="..\SemanticKernel\SemanticKernel.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Connectors\Connectors.AI.OpenAI\Connectors.AI.OpenAI.csproj" PrivateAssets="none" />
<ProjectReference Include="..\Skills\Skills.Planning\Skills.Planning.csproj" PrivateAssets="none" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed class PromptTemplateEngineTests
public PromptTemplateEngineTests(ITestOutputHelper testOutputHelper)
{
this._logger = testOutputHelper;
this._target = new PromptTemplateEngine(ConsoleLogger.Log);
this._target = new PromptTemplateEngine(TestConsoleLogger.Log);
this._variables = new ContextVariables(Guid.NewGuid().ToString("X"));
this._skills = new Mock<IReadOnlySkillCollection>();
}
Expand Down Expand Up @@ -219,7 +219,7 @@ private SKContext MockContext()
this._variables,
NullMemory.Instance,
this._skills.Object,
ConsoleLogger.Log);
TestConsoleLogger.Log);
}
}
#pragma warning restore VSTHRD103
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace SemanticKernel.UnitTests.XunitHelpers;
/// <summary>
/// Basic logger printing to console
/// </summary>
internal static class ConsoleLogger
internal static class TestConsoleLogger
{
internal static ILogger Log => LogFactory.CreateLogger<object>();

Expand Down
3 changes: 3 additions & 0 deletions dotnet/src/SemanticKernel/SemanticKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ Install this package manually only if you are selecting individual Semantic Kern
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Connectors.OpenAI</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Skills.Planning</_Parameter1>
</AssemblyAttribute>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>Microsoft.SemanticKernel.Skills.OpenAPI</_Parameter1>
</AssemblyAttribute>
Expand Down

0 comments on commit 6215f6b

Please sign in to comment.