forked from HemulGM/ChatGPT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatGPT_Console.dpr
73 lines (66 loc) · 1.72 KB
/
ChatGPT_Console.dpr
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
program ChatGPT_Console;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
System.Classes,
OpenAI.Chat,
OpenAI.Utils.ChatHistory,
OpenAI;
var
History: TChatHistory;
begin
try
var OpenAI := TOpenAI.Create({$include MY_TOKEN.txt});
History := TChatHistory.Create;
Writeln('ChatGPT is ready');
var Prompt: string := '';
repeat
Write('Human: ');
Readln(Prompt);
if Prompt.IsEmpty then
Break;
History.New(TMessageRole.User, Prompt, '');
try {
OpenAI.Chat.CreateStream(
procedure(Params: TChatParams)
begin
Params.Messages(History.ToArray);
Params.MaxTokens(1024);
Params.Stream;
end,
procedure(Chat: TChat; IsDone: Boolean; var Cancel: Boolean)
begin
if (not IsDone) and Assigned(Chat) then
Writeln(Chat.Choices[0].Delta.Content)
else if IsDone then
Writeln('DONE!');
Writeln('-------');
Sleep(80);
end); }
var Chat := OpenAI.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Messages(History.ToArray);
Params.MaxTokens(1024);
end);
try
for var Choise in Chat.Choices do
begin
History.New(TMessageRole.Assistant, Choise.Message.Content, '');
Writeln(Choise.Message.Content.Trim([#13, #10, ' ']));
end;
finally
Chat.Free;
end;
except
on E: Exception do
Writeln('Error: ', E.Message);
end;
until False;
History.Free;
except
on E: Exception do
Writeln(E.ClassName, ': ', E.Message);
end;
end.