Skip to content

Commit

Permalink
initial set of changes for SK RC3
Browse files Browse the repository at this point in the history
  • Loading branch information
dehoward committed Dec 8, 2023
1 parent 90e4054 commit 4336d8f
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 206 deletions.
2 changes: 1 addition & 1 deletion memorypipeline/CopilotChatMemoryPipeline.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<ItemGroup>
<PackageReference Include="Microsoft.ApplicationInsights.AspNetCore" Version="2.21.0" />
<PackageReference Include="Microsoft.KernelMemory.Core" Version="0.10.231117.1-preview" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc3" />
</ItemGroup>

</Project>
24 changes: 11 additions & 13 deletions webapi/Controllers/ChatController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@
using Microsoft.Extensions.Options;
using Microsoft.Graph;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Functions.OpenAPI.Authentication;
using Microsoft.SemanticKernel.Functions.OpenAPI.Extensions;
using Microsoft.SemanticKernel.Functions.OpenAPI.OpenAI;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.Plugins.MsGraph;
using Microsoft.SemanticKernel.Plugins.MsGraph.Connectors;
using Microsoft.SemanticKernel.Plugins.MsGraph.Connectors.Client;
Expand Down Expand Up @@ -96,7 +94,7 @@ public class ChatController : ControllerBase, IDisposable
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status504GatewayTimeout)]
public async Task<IActionResult> ChatAsync(
[FromServices] IKernel kernel,
[FromServices] Kernel kernel,
[FromServices] IHubContext<MessageRelayHub> messageRelayHubContext,
[FromServices] CopilotChatPlanner planner,
[FromServices] AskConverter askConverter,
Expand Down Expand Up @@ -132,7 +130,7 @@ public class ChatController : ControllerBase, IDisposable
[ProducesResponseType(StatusCodes.Status404NotFound)]
[ProducesResponseType(StatusCodes.Status504GatewayTimeout)]
public async Task<IActionResult> ProcessPlanAsync(
[FromServices] IKernel kernel,
[FromServices] Kernel kernel,
[FromServices] IHubContext<MessageRelayHub> messageRelayHubContext,
[FromServices] CopilotChatPlanner planner,
[FromServices] AskConverter askConverter,
Expand Down Expand Up @@ -163,7 +161,7 @@ public class ChatController : ControllerBase, IDisposable
/// <returns>Results containing the response from the model.</returns>
private async Task<IActionResult> HandleRequest(
string functionName,
IKernel kernel,
Kernel kernel,
IHubContext<MessageRelayHub> messageRelayHubContext,
CopilotChatPlanner planner,
AskConverter askConverter,
Expand All @@ -174,7 +172,7 @@ public class ChatController : ControllerBase, IDisposable
string chatId)
{
// Put ask's variables in the context we will use.
var contextVariables = askConverter.GetContextVariables(ask);
var kernelArguments = askConverter.GetKernelArguments(ask);

// Verify that the chat exists and that the user has access to it.
ChatSession? chat = null;
Expand All @@ -190,33 +188,33 @@ public class ChatController : ControllerBase, IDisposable

// Register plugins that have been enabled
var openApiPluginAuthHeaders = this.GetPluginAuthHeaders(this.HttpContext.Request.Headers);
await this.RegisterPlannerFunctionsAsync(planner, openApiPluginAuthHeaders, contextVariables);
await this.RegisterPlannerFunctionsAsync(planner, openApiPluginAuthHeaders, kernelArguments);

// Register hosted plugins that have been enabled
await this.RegisterPlannerHostedFunctionsUsedAsync(planner, chat!.EnabledPlugins);

// Get the function to invoke
ISKFunction? function = null;
KernelFunction? function = null;
try
{
function = kernel.Functions.GetFunction(ChatPluginName, functionName);
}
catch (SKException ex)
catch (KernelException ex)
{
this._logger.LogError("Failed to find {PluginName}/{FunctionName} on server: {Exception}", ChatPluginName, functionName, ex);
return this.NotFound($"Failed to find {ChatPluginName}/{functionName} on server");
}

// Run the function.
KernelResult? result = null;
FunctionResult? result = null;
try
{
using CancellationTokenSource? cts = this._serviceOptions.TimeoutLimitInS is not null
// Create a cancellation token source with the timeout if specified
? new CancellationTokenSource(TimeSpan.FromSeconds((double)this._serviceOptions.TimeoutLimitInS))
: null;

result = await kernel.RunAsync(function!, contextVariables, cts?.Token ?? default);
result = await kernel.InvokeAsync(function!, kernelArguments, cts?.Token ?? default);
this._telemetryService.TrackPluginFunction(ChatPluginName, functionName, true);
}
catch (Exception ex)
Expand All @@ -235,7 +233,7 @@ public class ChatController : ControllerBase, IDisposable
AskResult chatAskResult = new()
{
Value = result.GetValue<string>() ?? string.Empty,
Variables = contextVariables.Select(v => new KeyValuePair<string, string>(v.Key, v.Value))
Variables = kernelArguments.Select(v => new KeyValuePair<string, string>(v.Key, v.Value))
};

// Broadcast AskResult to all users
Expand Down Expand Up @@ -272,7 +270,7 @@ public class ChatController : ControllerBase, IDisposable
/// <summary>
/// Register functions with the planner's kernel.
/// </summary>
private async Task RegisterPlannerFunctionsAsync(CopilotChatPlanner planner, Dictionary<string, string> authHeaders, ContextVariables variables)
private async Task RegisterPlannerFunctionsAsync(CopilotChatPlanner planner, Dictionary<string, string> authHeaders, KernelArguments variables)
{
// Register authenticated functions with the planner's kernel only if the request includes an auth header for the plugin.

Expand Down
16 changes: 8 additions & 8 deletions webapi/CopilotChatWebApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@
<PackageReference Include="Microsoft.Azure.Cosmos" Version="3.37.0" />
<PackageReference Include="Microsoft.Identity.Web" Version="2.16.0" />
<PackageReference Include="Microsoft.KernelMemory.Core" Version="0.10.231117.1-preview" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Abstractions" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.AI.OpenAI" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.AzureCognitiveSearch" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.SemanticKernel.Connectors.Memory.Qdrant" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Functions.OpenAPI" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Planners.Core" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.MsGraph" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.0.0-beta8" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Core" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.MsGraph" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.TemplateEngine.Basic" Version="1.0.0-beta8" />
<PackageReference Include="SharpToken" Version="1.2.12" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
Expand Down
19 changes: 9 additions & 10 deletions webapi/Extensions/SemanticKernelExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Microsoft.Extensions.Options;
using Microsoft.KernelMemory;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Diagnostics;
using Microsoft.SemanticKernel.Plugins.Core;

namespace CopilotChat.WebApi.Extensions;
Expand All @@ -33,19 +32,19 @@ internal static class SemanticKernelExtensions
/// <summary>
/// Delegate to register functions with a Semantic Kernel
/// </summary>
public delegate Task RegisterFunctionsWithKernel(IServiceProvider sp, IKernel kernel);
public delegate Task RegisterFunctionsWithKernel(IServiceProvider sp, Kernel kernel);

/// <summary>
/// Delegate for any complimentary setup of the kernel, i.e., registering custom plugins, etc.
/// See webapi/README.md#Add-Custom-Setup-to-Chat-Copilot's-Kernel for more details.
/// </summary>
public delegate Task KernelSetupHook(IServiceProvider sp, IKernel kernel);
public delegate Task KernelSetupHook(IServiceProvider sp, Kernel kernel);

/// <summary>
/// Delegate to register plugins with the planner's kernel (i.e., omits plugins not required to generate bot response).
/// See webapi/README.md#Add-Custom-Plugin-Registration-to-the-Planner's-Kernel for more details.
/// </summary>
public delegate Task RegisterFunctionsWithPlannerHook(IServiceProvider sp, IKernel kernel);
public delegate Task RegisterFunctionsWithPlannerHook(IServiceProvider sp, Kernel kernel);

/// <summary>
/// Add Semantic Kernel services
Expand All @@ -55,7 +54,7 @@ public static WebApplicationBuilder AddSemanticKernelServices(this WebApplicatio
builder.InitializeKernelProvider();

// Semantic Kernel
builder.Services.AddScoped<IKernel>(
builder.Services.AddScoped<Kernel>(
sp =>
{
var provider = sp.GetRequiredService<SemanticKernelProvider>();
Expand Down Expand Up @@ -148,7 +147,7 @@ public static IServiceCollection AddPlannerSetupHook(this IServiceCollection ser
/// <summary>
/// Register the chat plugin with the kernel.
/// </summary>
public static IKernel RegisterChatPlugin(this IKernel kernel, IServiceProvider sp)
public static Kernel RegisterChatPlugin(this Kernel kernel, IServiceProvider sp)
{
// Chat plugin
kernel.ImportFunctions(
Expand Down Expand Up @@ -176,7 +175,7 @@ private static void InitializeKernelProvider(this WebApplicationBuilder builder)
/// <summary>
/// Register functions with the main kernel responsible for handling Chat Copilot requests.
/// </summary>
private static Task RegisterChatCopilotFunctionsAsync(IServiceProvider sp, IKernel kernel)
private static Task RegisterChatCopilotFunctionsAsync(IServiceProvider sp, Kernel kernel)
{
// Chat Copilot functions
kernel.RegisterChatPlugin(sp);
Expand All @@ -190,7 +189,7 @@ private static Task RegisterChatCopilotFunctionsAsync(IServiceProvider sp, IKern
/// <summary>
/// Register plugins with a given kernel.
/// </summary>
private static Task RegisterPluginsAsync(IServiceProvider sp, IKernel kernel)
private static Task RegisterPluginsAsync(IServiceProvider sp, Kernel kernel)
{
var logger = kernel.LoggerFactory.CreateLogger(nameof(Kernel));

Expand All @@ -204,7 +203,7 @@ private static Task RegisterPluginsAsync(IServiceProvider sp, IKernel kernel)
{
kernel.ImportSemanticFunctionsFromDirectory(options.SemanticPluginsDirectory, Path.GetFileName(subDir)!);
}
catch (SKException ex)
catch (KernelException ex)
{
logger.LogError("Could not load plugin from {Directory}: {Message}", subDir, ex.Message);
}
Expand Down Expand Up @@ -233,7 +232,7 @@ private static Task RegisterPluginsAsync(IServiceProvider sp, IKernel kernel)
var plugin = Activator.CreateInstance(classType);
kernel.ImportFunctions(plugin!, classType.Name!);
}
catch (SKException ex)
catch (KernelException ex)
{
logger.LogError("Could not load plugin from file {File}: {Details}", file, ex.Message);
}
Expand Down
Loading

0 comments on commit 4336d8f

Please sign in to comment.