Skip to content

Commit

Permalink
CopilotChat: Replace planner:enabled with implicit disable when no pl…
Browse files Browse the repository at this point in the history
…ugins enabled. (#1151)

### Motivation and Context
We have optimized planner usage to automatically disable when no plugins
are enabled.

### Description
- Remove planner options
- Replace planner:enabled check with check if any plugins are enabled.
  • Loading branch information
adrianwyatt committed May 22, 2023
1 parent 92ed59c commit ee20cd1
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.Graph;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI;
Expand All @@ -23,7 +22,6 @@
using Microsoft.SemanticKernel.Skills.MsGraph.Connectors.Client;
using Microsoft.SemanticKernel.Skills.OpenAPI.Authentication;
using SemanticKernel.Service.CopilotChat.Models;
using SemanticKernel.Service.CopilotChat.Options;
using SemanticKernel.Service.CopilotChat.Skills.ChatSkills;
using SemanticKernel.Service.Models;

Expand Down Expand Up @@ -63,8 +61,7 @@ public ChatController(ILogger<ChatController> logger)
[ProducesResponseType(StatusCodes.Status404NotFound)]
public async Task<IActionResult> ChatAsync(
[FromServices] IKernel kernel,
[FromServices] CopilotChatPlanner? planner,
[FromServices] IOptions<PlannerOptions> plannerOptions,
[FromServices] CopilotChatPlanner planner,
[FromBody] Ask ask,
[FromHeader] OpenApiSkillsAuthHeaders openApiSkillsAuthHeaders)
{
Expand All @@ -78,10 +75,7 @@ public async Task<IActionResult> ChatAsync(
}

// Register plugins that have been enabled
if (planner != null && plannerOptions.Value.Enabled)
{
await this.RegisterPlannerSkillsAsync(planner, plannerOptions.Value, openApiSkillsAuthHeaders, contextVariables);
}
await this.RegisterPlannerSkillsAsync(planner, openApiSkillsAuthHeaders, contextVariables);

// Get the function to invoke
ISKFunction? function = null;
Expand Down Expand Up @@ -114,7 +108,7 @@ public async Task<IActionResult> ChatAsync(
/// <summary>
/// Register skills with the planner's kernel.
/// </summary>
private async Task RegisterPlannerSkillsAsync(CopilotChatPlanner planner, PlannerOptions options, OpenApiSkillsAuthHeaders openApiSkillsAuthHeaders, ContextVariables variables)
private async Task RegisterPlannerSkillsAsync(CopilotChatPlanner planner, OpenApiSkillsAuthHeaders openApiSkillsAuthHeaders, ContextVariables variables)
{
// Register authenticated skills with the planner's kernel only if the request includes an auth header for the skill.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,16 @@ public static class CopilotChatSemanticKernelExtensions
/// </summary>
public static IServiceCollection AddCopilotChatPlannerServices(this IServiceCollection services)
{
IOptions<PlannerOptions>? plannerOptions = services.BuildServiceProvider().GetService<IOptions<PlannerOptions>>();
if (plannerOptions != null && plannerOptions.Value.Enabled)
{
services.AddScoped<CopilotChatPlanner>(sp => new CopilotChatPlanner(Kernel.Builder
.WithLogger(sp.GetRequiredService<ILogger<IKernel>>())
.WithConfiguration(
new KernelConfig().AddPlannerBackend(
sp.GetRequiredService<IOptions<AIServiceOptions>>().Value)
) // TODO verify planner has AI service configured
.Build()));
services.AddScoped<CopilotChatPlanner>(sp => new CopilotChatPlanner(Kernel.Builder
.WithLogger(sp.GetRequiredService<ILogger<IKernel>>())
.WithConfiguration(
new KernelConfig().AddPlannerBackend(
sp.GetRequiredService<IOptions<AIServiceOptions>>().Value)
) // TODO verify planner has AI service configured
.Build()));

// Register Planner skills (AI plugins) here.
// TODO: Move planner skill registration from ChatController to here.
}
// Register Planner skills (AI plugins) here.
// TODO: Move planner skill registration from ChatController to here.

return services;
}
Expand All @@ -52,7 +48,6 @@ public static IKernel RegisterCopilotChatSkills(this IKernel kernel, IServicePro
chatSessionRepository: sp.GetRequiredService<ChatSessionRepository>(),
promptOptions: sp.GetRequiredService<IOptions<PromptsOptions>>(),
planner: sp.GetRequiredService<CopilotChatPlanner>(),
plannerOptions: sp.GetRequiredService<IOptions<PlannerOptions>>().Value,
logger: sp.GetRequiredService<ILogger<ChatSkill>>()),
nameof(ChatSkill));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,7 @@ public static IServiceCollection AddCopilotChatOptions(this IServiceCollection s
.ValidateOnStart()
.PostConfigure(TrimStringProperties);

// Planner options
services.AddOptions<PlannerOptions>()
.Bind(configuration.GetSection(PlannerOptions.PropertyName))
.ValidateOnStart()
.PostConfigure(TrimStringProperties);

// Chat prompt options
services.AddOptions<PromptsOptions>()
.Bind(configuration.GetSection(PromptsOptions.PropertyName))
.ValidateOnStart()
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ public class ChatSkill
/// </summary>
private readonly CopilotChatPlanner _planner;

/// <summary>
/// Options for the planner.
/// </summary>
private readonly PlannerOptions _plannerOptions;

/// <summary>
/// Proposed plan to return for approval.
/// </summary>
Expand All @@ -80,7 +75,6 @@ public ChatSkill(
ChatSessionRepository chatSessionRepository,
IOptions<PromptsOptions> promptOptions,
CopilotChatPlanner planner,
PlannerOptions plannerOptions,
ILogger logger)
{
this._logger = logger;
Expand All @@ -89,7 +83,6 @@ public ChatSkill(
this._chatSessionRepository = chatSessionRepository;
this._promptOptions = promptOptions.Value;
this._planner = planner;
this._plannerOptions = plannerOptions;
}

/// <summary>
Expand Down Expand Up @@ -210,7 +203,8 @@ public async Task<string> ExtractUserMemoriesAsync(SKContext context)
[SKFunctionContextParameter(Name = "tokenLimit", Description = "Maximum number of tokens")]
public async Task<string> AcquireExternalInformationAsync(SKContext context)
{
if (!this._plannerOptions.Enabled)
FunctionsView functions = this._planner.Kernel.Skills.GetFunctionsView(true, true);
if (functions.NativeFunctions.IsEmpty && functions.SemanticFunctions.IsEmpty)
{
return string.Empty;
}
Expand Down
9 changes: 0 additions & 9 deletions samples/apps/copilot-chat-app/webapi/appsettings.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,6 @@
}
},

//
// Planner can determine which skill functions, if any, need to be used to fulfill a user's request.
// https://learn.microsoft.com/en-us/semantic-kernel/concepts-sk/planner
// - Set Enabled to false to disable the planner which will save a round-trip to the AI service but disables plug-in support.
//
"Planner": {
"Enabled": true
},

//
// Optional Azure Speech service configuration for providing Azure Speech access tokens.
// - Set the Region to the region of your Azure Speech resource (e.g., "westus").
Expand Down

0 comments on commit ee20cd1

Please sign in to comment.