-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
185 lines (155 loc) · 6.68 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
using Microsoft.Extensions.Configuration;
using OpenAI.GPT3.Managers;
using OpenAI.GPT3;
using OpenAI.GPT3.ObjectModels.RequestModels;
using OpenAI.GPT3.ObjectModels;
using System.Net;
using System.Diagnostics;
using System.Reflection;
using OpenAI.GPT3.ObjectModels.SharedModels;
using OpenAI.GPT3.Interfaces;
using OpenAI.GPT3.ObjectModels.ResponseModels.ImageResponseModel;
namespace ChatGPT_API_Test_Console_App
{
internal class Program
{
static async Task Main(string[] args)
{
bool exit = false;
var config = new ConfigurationBuilder().AddUserSecrets<Program>().Build();
var secretProvider = config.Providers.First();
secretProvider.TryGet("ChatGPT:ServiceApiKey", out var secretPass);
var openAiService = new OpenAIService(new OpenAiOptions()
{
ApiKey = secretPass
});
while (!exit)
{
switch (Program.ShowMenu())
{
case 0:
Console.WriteLine("");
exit = true;
break;
case 1:
Console.WriteLine("");
await PromptChatGpt(openAiService);
break;
case 2:
Console.WriteLine("");
await PromptDalle(openAiService);
break;
}
}
}
private static async Task PromptDalle(IOpenAIService service, string prompt = "Flying super dog against a blue sky with clouds and sun", int imageCount = 2)
{
Console.WriteLine("Current prompt: " + prompt);
Console.WriteLine("Enter prompt: ");
var input = Console.ReadLine().ToLower();
if (!string.IsNullOrEmpty(input))
{
prompt = input;
}
Console.WriteLine("Current Image generation count: " + imageCount);
Console.WriteLine("Enter to continue, or type the desired amount of images (max is 10): ");
input = Console.ReadLine().ToLower();
if (!string.IsNullOrEmpty(input))
{
imageCount = int.Parse(input);
if (imageCount > 10)
{
imageCount = 10;
}
else if (imageCount < 1)
{
imageCount = 1;
}
}
else
{
imageCount = 2;
}
Console.WriteLine("Sending Request to DALL-E, please wait.");
var imageResult = await service.Image.CreateImage(new ImageCreateRequest
{
Prompt = prompt,
N = imageCount,
Size = StaticValues.ImageStatics.Size.Size1024,
ResponseFormat = StaticValues.ImageStatics.ResponseFormat.Url,
User = "TestUser"
});
if (imageResult.Successful)
{
//create the output dir if it doesnt exist
System.IO.Directory.CreateDirectory("output");
using (WebClient client = new WebClient())
{
int i = 0;
foreach (var u in imageResult.Results)
{
//cheesy way of making the filenames differ
var filename = @"output\image-" + DateTime.Now.ToShortDateString().Replace('/', '-') + "-" + DateTime.Now.ToLongTimeString().Replace(':', '-') + ".png";
//download the output image to the working directory
client.DownloadFile(new Uri(u.Url), filename);
//ghetto way of making sure the file name is different due to using timestamps
System.Threading.Thread.Sleep(1000);
}
}
//display the file urls
Console.WriteLine(string.Join("\n\n", Enumerable.Select<ImageCreateResponse.ImageDataResult, string>(imageResult.Results, r => r.Url)));
//open explorer to see the output files
var uriString = Path.GetDirectoryName(path: Assembly.GetExecutingAssembly().CodeBase);
_ = Process.Start("explorer.exe", "/open, " + new Uri(uriString).LocalPath + "\\output\\");
Console.WriteLine("\nPress any key to continue.");
Console.ReadLine();
}
}
private static async Task PromptChatGpt(IOpenAIService service, string prompt = "Who won the last three FIFA world cups?")
{
Console.WriteLine("Current prompt: " + prompt);
Console.WriteLine("Enter prompt: ");
var input = Console.ReadLine().ToLower();
if (!string.IsNullOrEmpty(input))
{
prompt = input;
}
Console.WriteLine("Sending ChatCompletion request, please wait.");
var completionResult = await service.ChatCompletion.CreateCompletion(new ChatCompletionCreateRequest
{
Messages = new List<ChatMessage>
{
ChatMessage.FromUser(prompt)
},
Model = Models.ChatGpt3_5Turbo,
MaxTokens = 150 //optional
});
Console.WriteLine(completionResult.Successful ? Enumerable.First<ChatChoiceResponse>(completionResult.Choices).Message.Content : $"{completionResult.Error.Code}: {completionResult.Error.Message}");
Console.WriteLine("\nPress any key to continue.");
Console.ReadLine();
}
private static int ShowMenu()
{
Console.Clear();
Console.WriteLine("Hello, ChatGPT world!");
Console.WriteLine("==================================================");
Console.WriteLine("Menu: ");
Console.WriteLine("1 - Ask ChatGPT a question. ");
Console.WriteLine("2 - Ask DALL-E to generate image(s) from a prompt.");
Console.WriteLine("Q - Quit. ");
Console.WriteLine("==================================================");
Console.WriteLine("Enter Choice: ");
var input = Console.ReadLine().ToLower();
if (input.Contains('q'))
{
return 0;
}
else if (input.Contains('1') || input.Contains('2'))
{
return int.Parse(input);
}
ShowMenu();
return 0;
}
}
}