-
Notifications
You must be signed in to change notification settings - Fork 4
/
ChatGPT4Delphi.pas
76 lines (68 loc) · 2.15 KB
/
ChatGPT4Delphi.pas
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
unit ChatGPT4Delphi;
interface
uses
IdHTTP, IdGlobal, IdSSLOpenSSL, System.Classes, IdStack, System.SysUtils,
System.JSON, IdException;
type
TChatGPT4Delphi = class
private
FAccessKey: string;
FResponse: string;
procedure SetAccessKey(const Value: string);
public
constructor Create(AccessKey: string);
function Query(sMessage: string): string;
property AccessKey: string read FAccessKey write SetAccessKey;
property Response: string read FResponse;
end;
implementation
constructor TChatGPT4Delphi.Create(AccessKey: string);
begin
FAccessKey := AccessKey;
end;
procedure TChatGPT4Delphi.SetAccessKey(const Value: string);
begin
FAccessKey := Value;
end;
function TChatGPT4Delphi.Query(sMessage: string): string;
var
http: TIdHTTP;
sslHandler: TIdSSLIOHandlerSocketOpenSSL;
url, body, Response: string;
JSON: TJSONObject;
Choices: TJSONArray;
begin
url := 'https://api.openai.com/v1/chat/completions';
body := Format
('{"model": "%s", "messages": [{"role": "user", "content": "%s"}]}',
['gpt-3.5-turbo', sMessage]);
http := TIdHTTP.Create(nil);
sslHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
try
sslHandler.SSLOptions.Method := sslvTLSv1_2;
http.IOHandler := sslHandler;
http.Request.ContentType := 'application/json';
http.Request.Accept := 'application/json';
http.Request.CharSet := 'UTF-8';
http.Request.CustomHeaders.AddValue('Authorization',
Format('Bearer %s', [FAccessKey]));
try
Response := http.Post(url, TStringStream.Create(body, TEncoding.UTF8));
except
on E: EIdHTTPProtocolException do
raise Exception.CreateFmt('Indy exception (%d): %s',
[E.ErrorCode, E.Message + sLineBreak + E.ErrorMessage]);
// retorna a exceção com raise
end;
Response := IndyTextEncoding_UTF8.GetString
(IndyTextEncoding_OSDefault.GetBytes(Response));
JSON := TJSONObject.ParseJSONValue(Response) as TJSONObject;
Choices := JSON.GetValue('choices') as TJSONArray;
Result := Choices.Items[0].GetValue<TJSONObject>('message')
.GetValue('content').Value;
finally
http.Free;
sslHandler.Free;
end;
end;
end.