-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel.Diagnostics; | ||
using Microsoft.SemanticKernel.Orchestration; | ||
using Microsoft.SemanticKernel.SkillDefinition; | ||
|
||
namespace Microsoft.SemanticKernel.Skills.Planning.BasicPlanner; | ||
|
||
public class BasicPlanner | ||
Check warning on line 14 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 14 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 14 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / check-format
Check warning on line 14 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
Check warning on line 14 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
|
||
{ | ||
private const string SKPrompt = "BasicPlanner/skprompt.txt"; | ||
private const string StopSequence = "#END-OF-PLAN"; | ||
private const string SkillName = "basicPlanner"; | ||
|
||
private readonly ISKFunction _plannerFunction; | ||
|
||
/// <summary> | ||
/// Initialize a new instance of the <see cref="BasicPlanner"/> class. | ||
/// </summary> | ||
/// <param name="kernel">The semantic kernel instance.</param> | ||
/// <param name="prompt">Optional prompt override</param> | ||
public BasicPlanner( | ||
IKernel kernel, | ||
string? prompt = null) | ||
{ | ||
Verify.NotNull(kernel, "The planner requires a non-null kernel instance"); | ||
|
||
string promptTemplate = prompt ?? File.ReadAllText(SKPrompt); | ||
|
||
this._plannerFunction = kernel.CreateSemanticFunction( | ||
promptTemplate: promptTemplate, | ||
maxTokens: 1024, | ||
stopSequences: new[] { StopSequence }); | ||
|
||
kernel.ImportSkill(this, skillName: SkillName); | ||
} | ||
|
||
public async Task<Plan> CreatePlanAsync(string goal, SKContext context) | ||
{ | ||
SKContext result = await this._plannerFunction.InvokeAsync(context); | ||
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / check-format
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / check-format
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
Check failure on line 45 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
|
||
|
||
// extract and parse JSON | ||
// build and return plan | ||
|
||
throw new NotImplementedException("work in progress"); | ||
} | ||
|
||
/// <summary> | ||
/// Native function returning a list of all the functions in the current context, | ||
/// excluding functions in the planner itself. | ||
/// </summary> | ||
/// <param name="goal">Currently unused. Will be used to handle long lists of functions.</param> | ||
/// <param name="context">Function execution context</param> | ||
/// <returns>List of functions, formatted accordingly to the prompt</returns> | ||
[SKFunction("List all functions available in the kernel")] | ||
[SKFunctionName("ListOfFunctions")] | ||
[SKFunctionInput(Description = "The current goal processed by the planner", DefaultValue = "")] | ||
public string ListOfFunctions(string goal, SKContext context) | ||
{ | ||
Verify.NotNull(context.Skills, "The planner requires a non-null skill collection"); | ||
var functionsAvailable = context.Skills.GetFunctionsView(); | ||
|
||
// Prepare list using the format used by skprompt.txt | ||
var list = new StringBuilder(); | ||
this.PopulateList(list, functionsAvailable.NativeFunctions); | ||
this.PopulateList(list, functionsAvailable.SemanticFunctions); | ||
|
||
return list.ToString(); | ||
} | ||
|
||
private void PopulateList(StringBuilder list, IDictionary<string, List<FunctionView>> functions) | ||
{ | ||
foreach (KeyValuePair<string, List<FunctionView>> skill in functions) | ||
{ | ||
// Skill the BasicPlanner skill (ie. this class) | ||
if (string.Compare(skill.Key, SkillName, StringComparison.OrdinalIgnoreCase) == 0) { continue; } | ||
Check warning on line 81 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 81 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 81 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
|
||
|
||
foreach (FunctionView func in skill.Value) | ||
{ | ||
// Function description | ||
list.AppendLine($"// {AddPeriod(func.Description)}"); | ||
|
||
// Function name | ||
list.AppendLine($"{func.SkillName}.{func.Name}"); | ||
|
||
// Function parameters | ||
foreach (var p in func.Parameters) | ||
{ | ||
list.AppendLine($"Parameter \"{p.Name}\": {AddPeriod(p.Description)}"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
private static string AddPeriod(string x) | ||
{ | ||
return x.EndsWith(".") ? x : $"{x}."; | ||
Check warning on line 102 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 102 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Release)
Check warning on line 102 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / check-format
Check warning on line 102 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
Check warning on line 102 in dotnet/src/Skills/Skills.Planning/BasicPlanner/BasicPlanner.cs GitHub Actions / build (ubuntu-latest, Debug)
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
A planner takes a list of functions, a goal, and chooses which function to use. | ||
For each function the list includes details about the input parameters. | ||
[EXAMPLE 1] | ||
- List of functions: | ||
// Read a file. | ||
FileIOSkill.ReadAsync | ||
Parameter "path": Source file. | ||
// Write a file. | ||
FileIOSkill.WriteAsync | ||
Parameter "path": Destination file. | ||
Parameter "content": File content. | ||
// Get the current time. | ||
TimeSkill.Time | ||
No parameters. | ||
// Makes a POST request to a uri. | ||
HttpSkill.PostAsync | ||
Parameter "body": The body of the request. | ||
- End list of functions. | ||
Goal: create a file called "something.txt". | ||
#PLAN: | ||
{ | ||
"rationale": "the list contains a function that allows to create files", | ||
"function": "FileIOSkill.WriteAsync", | ||
"parameters": { | ||
"path": "something.txt", | ||
"content": "" | ||
} | ||
} | ||
#END-OF-PLAN | ||
[EXAMPLE 2] | ||
- List of functions: | ||
// Get the current time. | ||
TimeSkill.Time | ||
No parameters. | ||
// Write a file. | ||
FileIOSkill.WriteAsync | ||
Parameter "path": Destination file. | ||
Parameter "content": File content. | ||
// Makes a POST request to a uri. | ||
HttpSkill.PostAsync | ||
Parameter "body": The body of the request. | ||
// Read a file. | ||
FileIOSkill.ReadAsync | ||
Parameter "path": Source file. | ||
- End list of functions. | ||
Goal: tell me a joke. | ||
#PLAN: | ||
{ | ||
"rationale": "the list does not contain functions to tell jokes or something funny", | ||
"function": "", | ||
"parameters": {} | ||
} | ||
#END-OF-PLAN | ||
[END OF EXAMPLES] | ||
[REAL SCENARIO STARTS HERE] | ||
- List of functions: | ||
{{basicPlanner.ListOfFunctions}} | ||
- End list of functions. | ||
Goal: {{ $input }} | ||
#PLAN: |