-
Notifications
You must be signed in to change notification settings - Fork 420
Expand file tree
/
Copy pathProgram.cs
More file actions
56 lines (48 loc) · 1.84 KB
/
Copy pathProgram.cs
File metadata and controls
56 lines (48 loc) · 1.84 KB
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
using System;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
namespace OpenAI_Example.ConsoleApp
{
internal sealed class Program
{
private static async Task Main()
{
Console.WriteLine("Welcome to the OpenAI Example program.");
Console.WriteLine("Please provide a valid OpenAI Api Key:");
var apiKey = Console.ReadLine();
if (String.IsNullOrWhiteSpace(apiKey))
{
throw new InvalidOperationException("Cannot authorize with OpenAI when no valid API Key is provided.");
}
Console.WriteLine("What do you want to do?");
Type[] types = GetExamples();
_ = int.TryParse(Console.ReadLine(), out var programToRun);
var instance = (IProgram?)Activator.CreateInstance(types[programToRun]);
if (instance is not null)
{
await instance.RunAsync(apiKey);
}
Console.WriteLine("Thank you for using the OpenAI Example program.");
Console.WriteLine("Exiting now....");
}
private static Type[] GetExamples()
{
var type = typeof(IProgram);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(type.IsAssignableFrom)
.ToArray();
for (var i = 0; i < types.Length; i++)
{
var option = types[i];
if (option.Name == nameof(IProgram)) // Don't include the IProgram type.
{
continue;
}
Console.WriteLine($"\t{i}){option.Name}");
}
return types;
}
}
}