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

✨ Add ExecuteCommand to SKonsole #31

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions apps/SKonsole/Commands/ExecuteCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System.CommandLine;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using SKonsole.Utils;

namespace SKonsole.Commands;
internal sealed class ExecuteCommand : Command
{
private ILogger _logger;

public ExecuteCommand(ConfigurationProvider config, ILogger? logger = null) : base("exec", "Execute semantic function.")
{
if (logger is null)
{
using var loggerFactory = Logging.GetFactory();
this._logger = loggerFactory.CreateLogger(this.GetType());
}
else
{
this._logger = logger;
}

var promptArgument = new Argument<string>
("prompt", "The semantic function prompt or input for direct execution");

this.AddArgument(promptArgument);

var templateOption = new Option<string>(
new string[] { "--template", "-t" },
() => { return "{{$input}}"; },
"The template (file) to use for the semantic function.");

this.AddOption(templateOption);

var outputOption = new Option<string>(
new string[] { "--outputFile", "-o" },
description: "Output the result to the specified file.");
this.AddOption(outputOption);

this.SetHandler(async context => await RunExecuteAsync(context.GetCancellationToken(),
context.BindingContext.ParseResult.GetValueForArgument<string>(promptArgument),
context.BindingContext.ParseResult.GetValueForOption<string>(templateOption),
context.BindingContext.ParseResult.GetValueForOption<string>(outputOption),
this._logger));
}

private static async Task RunExecuteAsync(CancellationToken cancellationToken,
string prompt,
string? template = null,
string? outputFile = null,
ILogger? logger = null)
{
var kernel = KernelProvider.Instance.Get();

if (string.IsNullOrWhiteSpace(template))
{
template = "{{$input}}";
}
else if (Path.Exists(template))
{
template = await File.ReadAllTextAsync(template, cancellationToken);
}
else if (Path.Exists(Path.Combine(template, "skprompt.txt")))
{
template = await File.ReadAllTextAsync(Path.Combine(template, "skprompt.txt"), cancellationToken);
}

var func = kernel.CreateSemanticFunction(template);

var output = await kernel.RunAsync(prompt, func);

var result = output.GetValue<string>();

if (!string.IsNullOrWhiteSpace(outputFile))
{
var directory = Path.GetDirectoryName(outputFile);
if (!string.IsNullOrEmpty(directory))
{
Directory.CreateDirectory(directory);
}
System.IO.File.WriteAllText(outputFile, result);
}
else
{
Console.WriteLine(result);
}
}
}
3 changes: 2 additions & 1 deletion apps/SKonsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
new PRCommand(ConfigurationProvider.Instance),
new PlannerCommand(ConfigurationProvider.Instance),
new StepwisePlannerCommand(ConfigurationProvider.Instance),
new PromptChatCommand(ConfigurationProvider.Instance)
new PromptChatCommand(ConfigurationProvider.Instance),
new ExecuteCommand(ConfigurationProvider.Instance),
};

return await rootCommand.InvokeAsync(args);
Loading