Skip to content

Commit

Permalink
Update Semantic.Kernel to beta 1.0 release - refactor and update SKon…
Browse files Browse the repository at this point in the history
…sole project, commands, and plugins (#26)

* 🔄 Refactor code, update packages, and improve functions

This commit includes updating the Microsoft.SemanticKernel package to
version 1.0.0-beta1, refactoring the code to use the new package
structure, and improving various functions and classes. Key changes
include:

- Updating function imports to use new namespaces and classes
- Refactoring PromptChatCommand to use AIRequestSettings and ExtensionData
- Modifying RespondTo method to return SKContext instead of Task<SKContext>
- Updating StepwisePlannerCommand to import functions instead of skills
- Adding a new SuperFileIOPlugin class with various file operations
- Updating AI request settings in WriterSkill.cs
- Refactoring functions in CondenseSkill.cs and PullRequestSkill.cs
- Updating method calls and parameter types in PullRequestSkill
- Minor code improvements and formatting

* 📝 Refactor tasks.json and improve code readability

- Refactored tasks.json to include a new "format" task
- Simplified KernelProvider using switch expression
- Updated Program.cs to use object initializer for RootCommand
- Minor changes in CommitParser.cs for better readability

* 🔧 Update launch args and improve logging

- Update launch arguments in .vscode/launch.json
- Replace internalConsole with integratedTerminal
- Add SpectreConsole logging and extensions
- Improve ArgumentNullException handling in SpectreConsoleExtensions
- Add SpectreConsoleLoggerProvider for better logging output
  • Loading branch information
lemillermicrosoft authored Oct 11, 2023
1 parent 9fe1f96 commit 5be840d
Show file tree
Hide file tree
Showing 21 changed files with 388 additions and 168 deletions.
6 changes: 2 additions & 4 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceFolder}/apps/SKonsole/bin/Debug/net7.0/SKonsole.dll",
"args": [
/*"commit"*/
],
"args": ["stepwise", "optionset", "bing++"],
"cwd": "${workspaceFolder}",
"console": "internalConsole",
"console": "integratedTerminal",
"stopAtEntry": false
},
{
Expand Down
91 changes: 51 additions & 40 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,41 +1,52 @@
{
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
"version": "2.0.0",
"tasks": [
{
"label": "build",
"command": "dotnet",
"type": "process",
"args": [
"build",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "format",
"command": "dotnet",
"type": "process",
"args": ["format", "${workspaceFolder}/skonsole.sln"],
"problemMatcher": "$msCompile",
"group": {
"kind": "test",
"isDefault": true
}
},
{
"label": "publish",
"command": "dotnet",
"type": "process",
"args": [
"publish",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj",
"/property:GenerateFullPaths=true",
"/consoleloggerparameters:NoSummary"
],
"problemMatcher": "$msCompile"
},
{
"label": "watch",
"command": "dotnet",
"type": "process",
"args": [
"watch",
"run",
"--project",
"${workspaceFolder}/apps/SKonsole/SKonsole.csproj"
],
"problemMatcher": "$msCompile"
}
]
}
8 changes: 4 additions & 4 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
<PackageVersion Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="7.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageVersion Include="Microsoft.ML.Tokenizers" Version="0.21.0-preview.23266.6"/>
<PackageVersion Include="Microsoft.SemanticKernel" Version="0.24.230918.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Skills.Web" Version="0.24.230918.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Planning.StepwisePlanner" Version="0.24.230918.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel.Reliability.Basic" Version="0.24.230918.1-preview" />
<PackageVersion Include="Microsoft.SemanticKernel" Version="1.0.0-beta1" />
<PackageVersion Include="Microsoft.SemanticKernel.Planners.Core" Version="1.0.0-beta1" />
<PackageVersion Include="Microsoft.SemanticKernel.Plugins.Web" Version="1.0.0-beta1" />
<PackageVersion Include="Microsoft.SemanticKernel.Reliability.Basic" Version="1.0.0-beta1" />
<PackageVersion Include="Spectre.Console" Version="0.47.0" />
<PackageVersion Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
<PackageVersion Include="TextCopy" Version="6.2.1" />
Expand Down
6 changes: 3 additions & 3 deletions apps/SKonsole/Commands/CommitCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ private static async Task RunCommitMessage(CancellationToken token, ILogger logg
}
}

var pullRequestSkill = kernel.ImportSkill(new PRSkill.PullRequestSkill(kernel));
var pullRequestSkill = kernel.ImportFunctions(new PRSkill.PullRequestSkill(kernel));

void HorizontalRule(string title, string style = "white bold")
static void HorizontalRule(string title, string style = "white bold")
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule($"[{style}]{title}[/]").RuleStyle("grey").LeftJustified());
Expand All @@ -121,7 +121,7 @@ void HorizontalRule(string title, string style = "white bold")
var kernelResponse = await kernel.RunAsync(output, token, pullRequestSkill["GenerateCommitMessage"]);
task.StopTask();
var result = kernelResponse.Result;
var result = kernelResponse.GetValue<string>() ?? string.Empty;
await ClipboardService.SetTextAsync(result);
return result;
});
Expand Down
11 changes: 6 additions & 5 deletions apps/SKonsole/Commands/PRCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public PRCommand(ConfigurationProvider config, ILogger? logger = null) : base("p
{
return context.ParseResult.GetValueForOption(option);
}

private Command GeneratePRFeedbackCommand(Option<string> targetBranchOption)
{
var prFeedbackCommand = new Command("feedback", "Pull Request feedback subcommand");
Expand Down Expand Up @@ -106,11 +107,11 @@ private static async Task RunPullRequestFeedback(CancellationToken token, ILogge

string output = await process.StandardOutput.ReadToEndAsync();

var pullRequestSkill = kernel.ImportSkill(new PRSkill.PullRequestSkill(kernel));
var pullRequestSkill = kernel.ImportFunctions(new PRSkill.PullRequestSkill(kernel));

var kernelResponse = await kernel.RunAsync(output, token, pullRequestSkill["GeneratePullRequestFeedback"]);

logger.LogInformation("Pull Request Feedback:\n{result}", kernelResponse.Result);
logger.LogInformation("Pull Request Feedback:\n{result}", kernelResponse.GetValue<string>());
}

private static async Task RunPullRequestDescription(CancellationToken token, ILogger logger, string targetBranch = "origin/main", string outputFormat = "", string outputFile = "", string diffInputFile = "")
Expand All @@ -119,13 +120,13 @@ private static async Task RunPullRequestDescription(CancellationToken token, ILo

var output = await FetchDiff(targetBranch, diffInputFile);

var pullRequestSkill = kernel.ImportSkill(new PRSkill.PullRequestSkill(kernel));
var pullRequestSkill = kernel.ImportFunctions(new PRSkill.PullRequestSkill(kernel));

var contextVariables = new ContextVariables(output);
contextVariables.Set("outputFormatInstructions", PRSkill.Utils.FormatInstructionsProvider.GetOutputFormatInstructions(outputFormat));

var kernelResponse = await kernel.RunAsync(contextVariables, token, pullRequestSkill["GeneratePR"]);
logger.LogInformation("Pull Request Description:\n{result}", kernelResponse.Result);
logger.LogInformation("Pull Request Description:\n{result}", kernelResponse.GetValue<string>());

if (!string.IsNullOrEmpty(outputFile))
{
Expand All @@ -134,7 +135,7 @@ private static async Task RunPullRequestDescription(CancellationToken token, ILo
{
Directory.CreateDirectory(directory);
}
System.IO.File.WriteAllText(outputFile, kernelResponse.Result);
System.IO.File.WriteAllText(outputFile, kernelResponse.GetValue<string>());
}
}

Expand Down
31 changes: 16 additions & 15 deletions apps/SKonsole/Commands/PlannerCommand.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
using System.CommandLine;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Planning;
using Microsoft.SemanticKernel.Skills.Web;
using Microsoft.SemanticKernel.Skills.Web.Bing;
using Microsoft.SemanticKernel.Planners;
using Microsoft.SemanticKernel.Plugins.Web;
using Microsoft.SemanticKernel.Plugins.Web.Bing;
using SKonsole.Skills;
using SKonsole.Utils;

Expand Down Expand Up @@ -31,8 +30,10 @@ private Command GenerateCreatePlanCommand()
{
var messageArgument = new Argument<string>
("message", "An argument that is parsed as a string.");
var createPlanCommand = new Command("create", "Create Plan subcommand");
createPlanCommand.Add(messageArgument);
var createPlanCommand = new Command("create", "Create Plan subcommand")
{
messageArgument
};
createPlanCommand.SetHandler(async (messageArgumentValue) => await RunCreatePlan(CancellationToken.None, this._logger, messageArgumentValue), messageArgument);
return createPlanCommand;
}
Expand All @@ -42,22 +43,22 @@ private static async Task RunCreatePlan(CancellationToken token, ILogger logger,
var kernel = KernelProvider.Instance.Get();

// Eventually, Kernel will be smarter about what skills it uses for an ask.
// kernel.ImportSkill(new EmailSkill(), "email");
// kernel.ImportSkill(new GitSkill(), "git");
// kernel.ImportSkill(new SearchUrlSkill(), "url");
// kernel.ImportSkill(new HttpSkill(), "http");
// kernel.ImportSkill(new PRSkill.PullRequestSkill(kernel), "PullRequest");
// kernel.ImportFunctions(new EmailSkill(), "email");
// kernel.ImportFunctions(new GitSkill(), "git");
// kernel.ImportFunctions(new SearchUrlSkill(), "url");
// kernel.ImportFunctions(new HttpSkill(), "http");
// kernel.ImportFunctions(new PRSkill.PullRequestSkill(kernel), "PullRequest");

kernel.ImportSkill(new WriterSkill(kernel), "writer");
kernel.ImportFunctions(new WriterSkill(kernel), "writer");
var bingConnector = new BingConnector(Configuration.ConfigVar("BING_API_KEY"));
var bing = new WebSearchEngineSkill(bingConnector);
var search = kernel.ImportSkill(bing, "bing");
var bing = new WebSearchEnginePlugin(bingConnector);
var search = kernel.ImportFunctions(bing, "bing");

// var planner = new ActionPlanner();
var planner = new SequentialPlanner(kernel);
var plan = await planner.CreatePlanAsync(message);

await plan.InvokeAsync();
await kernel.RunAsync(plan);
}

private readonly ILogger _logger;
Expand Down
25 changes: 14 additions & 11 deletions apps/SKonsole/Commands/PromptChatCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
using System.Text;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI;
using Microsoft.SemanticKernel.Orchestration;
using Microsoft.SemanticKernel.SemanticFunctions;
using Microsoft.SemanticKernel.SkillDefinition;
using SKonsole.Utils;
using Spectre.Console;

Expand Down Expand Up @@ -46,16 +46,19 @@ private static async Task RunPromptChat(CancellationToken token, ILogger logger)
AI:
";

var promptConfig = new PromptTemplateConfig
{
Completion =
var promptConfig = new PromptTemplateConfig();
promptConfig.ModelSettings.Add(
new AIRequestSettings()
{
MaxTokens = 2000,
Temperature = 0.7,
TopP = 0.5,
StopSequences = new List<string> { "Human:", "AI:" },
ExtensionData = new Dictionary<string, object>()
{
{ "Temperature", 0.7 },
{ "TopP", 0.5 },
{ "MaxTokens", 2000 },
{ "StopSequences", new List<string> { "Human:", "AI:" } }
}
}
};
);
var promptTemplate = new PromptTemplate(SkPrompt, promptConfig, kernel);
var functionConfig = new SemanticFunctionConfig(promptConfig, promptTemplate);
var chatFunction = kernel.RegisterSemanticFunction("PromptBot", "Chat", functionConfig);
Expand All @@ -74,7 +77,7 @@ private static async Task RunChat(IKernel kernel, ILogger? logger, ISKFunction c
var botMessage = await kernel.RunAsync(contextVariables, chatFunction);
var userMessage = string.Empty;

void HorizontalRule(string title, string style = "white bold")
static void HorizontalRule(string title, string style = "white bold")
{
AnsiConsole.WriteLine();
AnsiConsole.Write(new Rule($"[{style}]{title}[/]").RuleStyle("grey").LeftJustified());
Expand All @@ -85,7 +88,7 @@ void HorizontalRule(string title, string style = "white bold")
{
HorizontalRule("AI", "green bold");
AnsiConsole.Foreground = ConsoleColor.Green;
AnsiConsole.WriteLine(botMessage.ToString());
AnsiConsole.WriteLine(botMessage?.ToString() ?? "NO MESSAGE FROM BOT");
AnsiConsole.ResetColors();

HorizontalRule("User");
Expand Down
Loading

0 comments on commit 5be840d

Please sign in to comment.