-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
107 lines (83 loc) · 3.66 KB
/
Program.cs
File metadata and controls
107 lines (83 loc) · 3.66 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
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
using Microsoft.Extensions.Configuration;
using Spectre.Console;
using Console = Spectre.Console.AnsiConsole;
using System.Net;
using System.Net.Quic;
using System.Diagnostics;
using Newtonsoft.Json;
using nl2sql.Services;
namespace nl2sql
{
internal class Program
{
static OpenAIService openAIService = null;
static async Task Main(string[] args)
{
Console.WriteLine("");
var configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT")}.json", optional: true);
var config = configuration.Build();
const string search = "1.\tAsk AI Assistant";
const string exit = "2.\tExit this Application";
while (true)
{
var selectedOption = AnsiConsole.Prompt(
new SelectionPrompt<string>()
.Title("Select an option to continue")
.PageSize(10)
.MoreChoicesText("[grey](Move up and down to reveal more options)[/]")
.AddChoices(new[] {
search, exit
}));
switch (selectedOption)
{
case search:
PerformSearch(config);
break;
case exit:
return;
}
}
}
private static OpenAIService initOpenAIService(IConfiguration config)
{
string endpoint = config["OpenAIEndpoint"];
string key = config["OpenAIKey"];
string embeddingDeployment = config["OpenAIEmbeddingDeployment"];
string completionsDeployment = config["OpenAIcompletionsDeployment"];
string maxToken = config["OpenAIMaxToken"];
return new OpenAIService(endpoint, key, embeddingDeployment, completionsDeployment, maxToken);
}
private static void PerformSearch(IConfiguration config)
{
Dictionary<string, float[]> dictEmbeddings = new Dictionary<string, float[]>();
string chatCompletion = string.Empty;
string userQuery = Console.Prompt(
new TextPrompt<string>("Type your question and hit enter when ready.")
.PromptStyle("teal")
);
AnsiConsole.Status()
.Start("Processing...", ctx =>
{
ctx.Spinner(Spinner.Known.Star);
ctx.SpinnerStyle(Style.Parse("green"));
if (openAIService == null)
{
ctx.Status("Connecting to Open AI Service..");
openAIService = initOpenAIService(config);
}
ctx.Status($"Processing user prompt to generate Completion using OpenAI Service..");
(string completion, int promptTokens, int completionTokens) = openAIService.GetChatCompletionAsync(userQuery).GetAwaiter().GetResult();
chatCompletion = completion;
});
Console.WriteLine("");
Console.Write(new Rule($"[silver]AI Assistant Response[/]") { Justification = Justify.Center });
AnsiConsole.MarkupLine(chatCompletion);
Console.WriteLine("");
Console.WriteLine("");
Console.Write(new Rule($"[yellow]****[/]") { Justification = Justify.Center });
Console.WriteLine("");
}
}
}