-
Notifications
You must be signed in to change notification settings - Fork 27
/
Program.cs
83 lines (71 loc) · 2.28 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft. All rights reserved.
using Microsoft.SemanticKernel;
using Microsoft.TypeChat;
namespace Plugins;
public class PluginApp : ConsoleApp
{
OpenAIConfig _config;
Kernel _kernel;
PluginProgramTranslator _programTranslator;
ProgramInterpreter _interpreter;
public PluginApp()
{
InitPlugins();
_programTranslator = new PluginProgramTranslator(_kernel, _config.Model);
_programTranslator.Translator.MaxRepairAttempts = 2;
_interpreter = new ProgramInterpreter();
// Uncomment to see ALL raw messages to and from the AI
// base.SubscribeAllEvents(_programTranslator.Translator);
}
public Kernel Kernel => _kernel;
public string Schema => _programTranslator.Schema;
public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
{
using Program program = await _programTranslator.TranslateAsync(input, cancelToken);
program.Print(_programTranslator.Api.TypeName);
Console.WriteLine();
if (program.IsComplete)
{
await RunProgram(program);
}
}
async Task RunProgram(Program program)
{
if (!program.IsComplete)
{
return;
}
Console.WriteLine("Running program");
string result = await _interpreter.RunAsync(program, _programTranslator.Api.InvokeAsync);
if (!string.IsNullOrEmpty(result))
{
Console.WriteLine(result);
}
}
void InitPlugins()
{
_config = Config.LoadOpenAI();
_kernel = _config.CreateKernel();
_kernel.Plugins.AddFromObject(new ShellPlugin());
_kernel.Plugins.AddFromObject(new FoldersPlugin());
_kernel.Plugins.AddFromObject(new StringPlugin());
_kernel.Plugins.AddFromObject(new TimePlugin());
}
public static async Task<int> Main(string[] args)
{
try
{
PluginApp app = new PluginApp();
// Uncomment to view auto-generated schema
//Console.WriteLine(app.Schema);
await app.RunAsync("🤖> ", args.GetOrNull(0));
}
catch (Exception ex)
{
WriteError(ex);
Console.ReadLine();
return -1;
}
return 0;
}
}