-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatgpt.pas
More file actions
133 lines (117 loc) · 3.39 KB
/
chatgpt.pas
File metadata and controls
133 lines (117 loc) · 3.39 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
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
unit ChatGPT;
interface
uses
System.SysUtils, System.Classes, System.JSON, System.Net.HttpClientComponent,
System.Net.HttpClient, System.Net.URLClient, System.Threading;
type
TChatGPT = class(TComponent)
private
FToken: String;
FResponse: String;
FChatModel: String;
FHttpClient: TNetHTTPClient; // Klient HTTP
FOnResponse: TProc<string>; // Callback do obsługi odpowiedzi
function RequestJson(const URL, Token, Question: string): string;
function ExtractMessage(const JSON: string): string;
public
property Token: String read FToken write FToken;
property Response: String read FResponse;
property ChatModel: String read FChatModel write FChatModel;
property OnResponse: TProc<string> read FOnResponse write FOnResponse; // Obsługa odpowiedzi
procedure SendQuestionAsync(const Question: String);
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
implementation
{ TChatGPT }
constructor TChatGPT.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FChatModel := 'gpt-4'; // Domyślny model
FHttpClient := TNetHTTPClient.Create(nil); // Tworzenie klienta HTTP
end;
destructor TChatGPT.Destroy;
begin
FHttpClient.Free; // Zwolnienie klienta HTTP
inherited;
end;
function TChatGPT.RequestJson(const URL, Token, Question: string): string;
var
RequestBody: TStringStream;
Response: IHTTPResponse;
Params: string;
begin
Result := '';
Params := Format(
'{ "model": "%s", "messages": [ { "role": "user", "content": "%s" } ] }',
[FChatModel, Question]
);
RequestBody := TStringStream.Create(Params, TEncoding.UTF8);
try
FHttpClient.ContentType := 'application/json';
FHttpClient.CustomHeaders['Authorization'] := 'Bearer ' + Token;
try
Response := FHttpClient.Post(URL, RequestBody);
Result := Response.ContentAsString(TEncoding.UTF8);
except
on E: ENetHTTPClientException do
Result := '{"error": "' + E.Message + '"}';
end;
finally
RequestBody.Free;
end;
end;
function TChatGPT.ExtractMessage(const JSON: string): string;
var
JSONObj: TJSONObject;
ChoicesArray: TJSONArray;
MessageObj: TJSONObject;
begin
Result := '';
JSONObj := TJSONObject.ParseJSONValue(JSON) as TJSONObject;
try
if Assigned(JSONObj) and JSONObj.TryGetValue<TJSONArray>('choices', ChoicesArray) then
begin
if ChoicesArray.Count > 0 then
begin
MessageObj := ChoicesArray.Items[0] as TJSONObject;
if MessageObj.TryGetValue<string>('message.content', Result) then
Exit;
end;
end;
finally
JSONObj.Free;
end;
end;
procedure TChatGPT.SendQuestionAsync(const Question: String);
var
URL: String;
begin
URL := 'https://api.openai.com/v1/chat/completions';
// Uruchomienie wątku asynchronicznego
TTask.Run(
procedure
var
JsonResponse: string;
ExtractedMessage: string;
begin
try
JsonResponse := RequestJson(URL, FToken, Question);
ExtractedMessage := ExtractMessage(JsonResponse);
except
on E: Exception do
ExtractedMessage := '{"error": "' + E.Message + '"}';
end;
// Wywołanie zwrotne w głównym wątku
TThread.Synchronize(nil,
procedure
begin
FResponse := ExtractedMessage;
if Assigned(FOnResponse) then
FOnResponse(FResponse);
end
);
end
);
end;
end.