forked from microsoft/typechat.net
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
83 lines (71 loc) · 2.25 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.TypeChat;
using Microsoft.TypeChat.Examples;
using Microsoft.TypeChat.LanguageModels;
using Microsoft.TypeChat.Schema;
namespace Math;
public class MathApp : ConsoleApp
{
ProgramTranslator<IMathAPI> _translator;
Api<IMathAPI> _api;
public MathApp()
{
_api = new MathAPI();
_translator = new ProgramTranslator<IMathAPI>(
new OpenAILanguageModel(ExampleConfig.LoadOpenAI()),
_api
);
_translator.MaxRepairAttempts = 3;
_api.CallCompleted += this.DisplayCall;
// Uncomment to see ALL raw messages to and from the AI
//base.SubscribeAllEvents(_translator);
}
public TypeSchema Schema => _translator.Validator.Schema;
public override async Task ProcessInputAsync(string input, CancellationToken cancelToken)
{
using Program program = await _translator.TranslateAsync(input, cancelToken);
// Print whatever program was returned
program.Print(_api.Type.Name);
Console.WriteLine();
if (program.IsComplete)
{
// IsComplete: If program has steps and program.HasNotTranslated is false
RunProgram(program);
}
}
void RunProgram(Program program)
{
Console.WriteLine("Running program");
dynamic retval = program.Run(_api);
if (retval is not null && retval is double)
{
Console.WriteLine($"Result: {retval}");
}
else
{
Console.WriteLine("No result");
}
}
void DisplayCall(string functionName, dynamic[] args, dynamic result)
{
new ProgramWriter(Console.Out).Write(functionName, args);
Console.WriteLine($"==> {result}");
}
public static async Task<int> Main(string[] args)
{
try
{
MathApp app = new MathApp();
// Un-comment to print auto-generated schema at start:
// Console.WriteLine(app._translator.ApiDef);
await app.RunAsync("➕➖✖️➗🟰> ", args.GetOrNull(0));
}
catch (Exception ex)
{
WriteError(ex);
Console.ReadLine();
return -1;
}
return 0;
}
}