-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathProgram.cs
46 lines (37 loc) · 1.34 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
namespace openai_csharp_example;
using Microsoft.Extensions.Configuration;
internal class Program
{
private static async Task Main(string[] args)
{
var builder = new ConfigurationBuilder()
.AddUserSecrets<Program>();
var configuration = builder.Build();
var apiKey = configuration["OpenAI:ApiKey"];
using var httpClient = new HttpClient();
var openAIService = new OpenAIService(httpClient, apiKey);
var chatSession = new ChatSession();
while (true)
{
Console.Write("You: ");
var userInput = Console.ReadLine();
if (string.IsNullOrWhiteSpace(userInput))
{
Console.WriteLine("Input can't be empty. Please try again.");
continue;
}
chatSession.AddMessage("user", userInput);
try
{
var response = await openAIService.SendPromptAndGetResponse(chatSession.GetMessages());
Console.WriteLine($"OpenAI: {response}");
chatSession.AddMessage("assistant", response);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
break;
}
}
}
}