Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copilot Chat: Add StepwisePlanner to Copilot Chat Sample App #1879

Closed
samleegithub opened this issue Jul 6, 2023 · 6 comments
Closed

Copilot Chat: Add StepwisePlanner to Copilot Chat Sample App #1879

samleegithub opened this issue Jul 6, 2023 · 6 comments
Assignees

Comments

@samleegithub
Copy link

Add StepwisePlanner to Copilot Chat Sample App.

I tried to do it myself but I'm unable to figure out why StepwisePlanner could not be found.

I am getting this error:

The type or namespace name 'StepwisePlanner' could not be found (are you missing a using directive or an assembly reference?) [CopilotChatWebApi]

First, I made the same changes in this pull request (#1811) to get Copilot Chat working with the "0.17.230629.1-preview" version:

Next I added this code to https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/copilot-chat-app/webapi/CopilotChat/Skills/ChatSkills/CopilotChatPlanner.cs:

   if (this._plannerOptions?.Type == PlanType.Sequential)
    {
        return new SequentialPlanner(this.Kernel).CreatePlanAsync(goal);
    }
    else if (this._plannerOptions?.Type == PlanType.Stepwise)
    {
        return new StepwisePlanner(this.Kernel).CreatePlanAsync(goal);
    }

I added a new enum for Stepwise here: https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/copilot-chat-app/webapi/CopilotChat/Models/ProposedPlan.cs

// Type of Plan
public enum PlanType
{
Action, // single-step
Sequential, // multi-step
Stepwise, // step-by-step
}

Finally, I set Planner Type to "Stepwise" here: https://github.com/microsoft/semantic-kernel/blob/main/samples/apps/copilot-chat-app/webapi/appsettings.json

//
// 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 Planner:Type to "Action" to use the single-step ActionPlanner (default)
// - Set Planner:Type to "Sequential" to enable the multi-step SequentialPlanner
// - Set Planner:Type to "Stepwise" to enable the stepwise StepwisePlanner
// See the "Enabling Sequential Planner" section in webapi/README.md for configuration instructions.
//
"Planner": {
"Type": "Stepwise"
},

@github-actions github-actions bot added the triage label Jul 6, 2023
@hollygrimm
Copy link

Did you try adding this to your to your .csproj file?

    <PackageReference Include="Microsoft.SemanticKernel.Planning.StepwisePlanner" Version="0.17.230629.1-preview" />

@samleegithub
Copy link
Author

samleegithub commented Jul 7, 2023

@hollygrimm Thank you that helped!

Just curious, why do we need to add a reference in .csproj file for StepwisePlanner because I didn't see any for SequentialPlanner or ActionPlanner?

I had to implement a workaround to fix another issue:

In StepwisePlanner.cs there is no CreatePlanAsync method. I could only find the CreatePlan method.

Is there a reason that CreatePlanAsync is not implemented for StepwisePlanner?

FYI, here is how I modified the code to make it work:

/// <summary>
/// Create a plan for a goal.
/// </summary>
/// <param name="goal">The goal to create a plan for.</param>
/// <returns>The plan.</returns>
public Task<Plan> CreatePlanAsync(string goal)
{
    FunctionsView plannerFunctionsView = this.Kernel.Skills.GetFunctionsView(true, true);
    if (plannerFunctionsView.NativeFunctions.IsEmpty && plannerFunctionsView.SemanticFunctions.IsEmpty)
    {
        // No functions are available - return an empty plan.
        return Task.FromResult(new Plan(goal));
    }

    switch (this._plannerOptions?.Type)
    {
        case PlanType.Sequential:
            return new SequentialPlanner(this.Kernel).CreatePlanAsync(goal);
        case PlanType.Stepwise:
            return Task.FromResult(new StepwisePlanner(this.Kernel).CreatePlan(goal));
        default:
            return new ActionPlanner(this.Kernel).CreatePlanAsync(goal);
    }
}

BTW, I used the preview version of VS Code Copilot Chat to help fix the problem. It suggested using Task.FromResult(). It also suggested using a switch statement instead of "if else" to improve readability and maintainability.

@hollygrimm
Copy link

@samleegithub If you run dotnet list package --include-transitive you can see that SequentialPlanner and ActionPlanner are included as transitive dependencies:

   > Microsoft.SemanticKernel.Abstractions                                              0.17.230629.1-preview
   > Microsoft.SemanticKernel.Core                                                      0.17.230629.1-preview
   > Microsoft.SemanticKernel.Planning.ActionPlanner                                    0.17.230629.1-preview
   > Microsoft.SemanticKernel.Planning.SequentialPlanner                                0.17.230629.1-preview
  1. I'm not sure why they didn't support CreatePlanAsync for Stepwise. I'm working on it this week, let you know if I figure it out

@shawncal shawncal changed the title Add StepwisePlanner to Copilot Chat Sample App Copilot Chat: Add StepwisePlanner to Copilot Chat Sample App Jul 11, 2023
@TaoChenOSU
Copy link
Contributor

Hello @samleegithub, glad to hear you are interested in the stepwise planner and excited to see that you are playing with the code!

Our team is also actively working on bring the Stepwise planner to CC: #1758. Stay tuned!

@teresaqhoang
Copy link
Contributor

Hey @samleegithub, to answer your question:

Is there a reason that CreatePlanAsync is not implemented for StepwisePlanner?

You can think of Stepwise planner as a planning system that loops through an intent detection pattern. Its plan creation is not asynchronous because it creates a static single-step plan that always has the same function: ExecutePlan (populated with the kernel's functions and other context).

So when you get this plan back from Stepwise Planner and invoke it, it will invoke the ExecutePlan function, which loops through a chain of thought, action, and observation until it finds the answer. It'll give propose an action (function + parameters to call), invoke, then make an observation based on the output of the function, correct as necessary and repeat, or return if it thinks it meets the goal.

This example shows the flow pretty well:

public static class Example51_StepwisePlanner

And Lee (creator of Stepwise Planner) explains it more in detail with examples here: This video is super helpful in explaining it: What is the Stepwise Planner? Motivation and Walkthrough | Intro to Semantic Kernel - YouTube

Hope this helps!

@TaoChenOSU
Copy link
Contributor

Closing for now. Reopen if necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

5 participants