forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAI.Chat.pas
368 lines (323 loc) · 10.9 KB
/
OpenAI.Chat.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
unit OpenAI.Chat;
interface
uses
System.SysUtils, OpenAI.API.Params, OpenAI.API, System.Classes;
{$SCOPEDENUMS ON}
type
TMessageRole = (System, User, Assistant);
TMessageRoleHelper = record helper for TMessageRole
function ToString: string;
end;
TChatMessageBuild = record
private
FRole: TMessageRole;
FContent: string;
public
property Role: TMessageRole read FRole write FRole;
property Content: string read FContent write FContent;
class function Create(Role: TMessageRole; Content: string): TChatMessageBuild; static;
class function User(Content: string): TChatMessageBuild; static;
class function System(Content: string): TChatMessageBuild; static;
class function Assistant(Content: string): TChatMessageBuild; static;
end;
TChatParams = class(TJSONParam)
/// <summary>
/// ID of the model to use. Currently, only gpt-3.5-turbo and gpt-3.5-turbo-0301 are supported.
/// </summary>
function Model(const Value: string): TChatParams;
/// <summary>
/// The messages to generate chat completions for, in the chat format.
/// </summary>
function Messages(const Value: TArray<TChatMessageBuild>): TChatParams; overload;
/// <summary>
/// What sampling temperature to use, between 0 and 2. Higher values like 0.8 will make the output more random,
/// while lower values like 0.2 will make it more focused and deterministic.
/// We generally recommend altering this or top_p but not both.
/// </summary>
function Temperature(const Value: Single = 1): TChatParams;
/// <summary>
/// An alternative to sampling with temperature, called nucleus sampling, where the model considers the
/// results of the tokens with top_p probability mass. So 0.1 means only the tokens comprising the top 10%
/// probability mass are considered.
/// We generally recommend altering this or temperature but not both.
/// </summary>
function TopP(const Value: Single = 1): TChatParams;
/// <summary>
/// How many chat completion choices to generate for each input message.
/// </summary>
function N(const Value: Integer = 1): TChatParams;
/// <summary>
/// If set, partial message deltas will be sent, like in ChatGPT. Tokens will be sent as
/// data-only server-sent events as they become available, with the stream terminated by a data: [DONE] message.
/// </summary>
function Stream(const Value: Boolean = True): TChatParams;
/// <summary>
/// Up to 4 sequences where the API will stop generating further tokens.
/// </summary>
function Stop(const Value: string): TChatParams; overload;
/// <summary>
/// Up to 4 sequences where the API will stop generating further tokens.
/// </summary>
function Stop(const Value: TArray<string>): TChatParams; overload;
/// <summary>
/// The maximum number of tokens allowed for the generated answer. By default, the number of
/// tokens the model can return will be (4096 - prompt tokens).
/// </summary>
function MaxTokens(const Value: Integer = 16): TChatParams;
/// <summary>
/// Number between -2.0 and 2.0. Positive values penalize new tokens based on whether they appear in the text so far,
/// increasing the model's likelihood to talk about new topics.
/// </summary>
function PresencePenalty(const Value: Single = 0): TChatParams;
/// <summary>
/// Number between -2.0 and 2.0. Positive values penalize new tokens based on their existing frequency in the text so far,
/// decreasing the model's likelihood to repeat the same line verbatim.
/// </summary>
function FrequencyPenalty(const Value: Single = 0): TChatParams;
/// <summary>
/// A unique identifier representing your end-user, which can help OpenAI to monitor and detect abuse.
/// </summary>
function User(const Value: string): TChatParams;
constructor Create; override;
end;
TChatUsage = class
private
FCompletion_tokens: Int64;
FPrompt_tokens: Int64;
FTotal_tokens: Int64;
public
property CompletionTokens: Int64 read FCompletion_tokens write FCompletion_tokens;
property PromptTokens: Int64 read FPrompt_tokens write FPrompt_tokens;
property TotalTokens: Int64 read FTotal_tokens write FTotal_tokens;
end;
TChatMessage = class
private
FRole: string;
FContent: string;
public
property Role: string read FRole write FRole;
property Content: string read FContent write FContent;
end;
TChatChoices = class
private
FIndex: Int64;
FMessage: TChatMessage;
FFinish_reason: string;
FDelta: TChatMessage;
public
property Index: Int64 read FIndex write FIndex;
property Message: TChatMessage read FMessage write FMessage;
property Delta: TChatMessage read FDelta write FDelta;
/// <summary>
/// The possible values for finish_reason are:
/// stop: API returned complete model output
/// length: Incomplete model output due to max_tokens parameter or token limit
/// content_filter: Omitted content due to a flag from our content filters
/// null: API response still in progress or incomplete
/// </summary>
property FinishReason: string read FFinish_reason write FFinish_reason;
destructor Destroy; override;
end;
TChat = class
private
FChoices: TArray<TChatChoices>;
FCreated: Int64;
FId: string;
FObject: string;
FUsage: TChatUsage;
public
property Id: string read FId write FId;
property &Object: string read FObject write FObject;
property Created: Int64 read FCreated write FCreated;
property Choices: TArray<TChatChoices> read FChoices write FChoices;
property Usage: TChatUsage read FUsage write FUsage;
destructor Destroy; override;
end;
TChatEvent = reference to procedure(Chat: TChat; IsDone: Boolean; var Cancel: Boolean);
/// <summary>
/// Given a chat conversation, the model will return a chat completion response.
/// </summary>
TChatRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Creates a completion for the chat message
/// </summary>
function Create(ParamProc: TProc<TChatParams>): TChat;
/// <summary>
/// Creates a completion for the chat message
/// </summary>
function CreateStream(ParamProc: TProc<TChatParams>; Event: TChatEvent): Boolean;
end;
implementation
uses
System.JSON, Rest.Json;
{ TChatRoute }
function TChatRoute.Create(ParamProc: TProc<TChatParams>): TChat;
begin
Result := API.Post<TChat, TChatParams>('chat/completions', ParamProc);
end;
function TChatRoute.CreateStream(ParamProc: TProc<TChatParams>; Event: TChatEvent): Boolean;
var
Response: TStringStream;
RetPos: Integer;
begin
Response := TStringStream.Create('', TEncoding.UTF8);
try
RetPos := 0;
Result := API.Post<TChatParams>('chat/completions', ParamProc, Response,
procedure(const Sender: TObject; AContentLength: Int64; AReadCount: Int64; var AAbort: Boolean)
var
IsDone: Boolean;
Data: string;
Chat: TChat;
TextBuffer: string;
Line: string;
Ret: Integer;
begin
TextBuffer := Response.DataString;
repeat
Ret := TextBuffer.IndexOf(#10, RetPos);
if Ret >= 0 then
begin
Line := TextBuffer.Substring(RetPos, Ret - RetPos);
RetPos := Ret + 1;
if Line.IsEmpty or (Line.StartsWith(#10)) then
Continue;
Chat := nil;
Data := Line.Replace('data: ', '').Trim([' ', #13, #10]);
IsDone := Data = '[DONE]';
if not IsDone then
begin
try
Chat := TJson.JsonToObject<TChat>(Data);
except
Chat := nil;
end;
end;
Event(Chat, IsDone, AAbort);
end;
until Ret < 0;
end);
finally
Response.Free;
end;
end;
{ TChat }
destructor TChat.Destroy;
begin
if Assigned(FUsage) then
FUsage.Free;
for var Item in FChoices do
if Assigned(Item) then
Item.Free;
inherited;
end;
{ TChatParams }
constructor TChatParams.Create;
begin
inherited;
Model('gpt-3.5-turbo');
end;
function TChatParams.FrequencyPenalty(const Value: Single): TChatParams;
begin
Result := TChatParams(Add('frequency_penalty', Value));
end;
function TChatParams.MaxTokens(const Value: Integer): TChatParams;
begin
Result := TChatParams(Add('max_tokens', Value));
end;
function TChatParams.Model(const Value: string): TChatParams;
begin
Result := TChatParams(Add('model', Value));
end;
function TChatParams.N(const Value: Integer): TChatParams;
begin
Result := TChatParams(Add('n', Value));
end;
function TChatParams.PresencePenalty(const Value: Single): TChatParams;
begin
Result := TChatParams(Add('presence_penalty', Value));
end;
function TChatParams.Messages(const Value: TArray<TChatMessageBuild>): TChatParams;
var
Item: TChatMessageBuild;
JSON: TJSONObject;
Items: TJSONArray;
begin
Items := TJSONArray.Create;
for Item in Value do
begin
JSON := TJSONObject.Create;
JSON.AddPair('role', Item.Role.ToString);
JSON.AddPair('content', Item.Content);
Items.Add(JSON);
end;
Result := TChatParams(Add('messages', Items));
end;
function TChatParams.Stop(const Value: TArray<string>): TChatParams;
begin
Result := TChatParams(Add('stop', Value));
end;
function TChatParams.Stop(const Value: string): TChatParams;
begin
Result := TChatParams(Add('stop', Value));
end;
function TChatParams.Stream(const Value: Boolean): TChatParams;
begin
Result := TChatParams(Add('stream', Value));
end;
function TChatParams.Temperature(const Value: Single): TChatParams;
begin
Result := TChatParams(Add('temperature', Value));
end;
function TChatParams.TopP(const Value: Single): TChatParams;
begin
Result := TChatParams(Add('top_p', Value));
end;
function TChatParams.User(const Value: string): TChatParams;
begin
Result := TChatParams(Add('user', Value));
end;
{ TChatMessageBuild }
class function TChatMessageBuild.Assistant(Content: string): TChatMessageBuild;
begin
Result.FRole := TMessageRole.Assistant;
Result.FContent := Content;
end;
class function TChatMessageBuild.Create(Role: TMessageRole; Content: string): TChatMessageBuild;
begin
Result.FRole := Role;
Result.FContent := Content;
end;
class function TChatMessageBuild.System(Content: string): TChatMessageBuild;
begin
Result.FRole := TMessageRole.System;
Result.FContent := Content;
end;
class function TChatMessageBuild.User(Content: string): TChatMessageBuild;
begin
Result.FRole := TMessageRole.User;
Result.FContent := Content;
end;
{ TMessageRoleHelper }
function TMessageRoleHelper.ToString: string;
begin
case Self of
TMessageRole.System:
Result := 'system';
TMessageRole.User:
Result := 'user';
TMessageRole.Assistant:
Result := 'assistant';
end;
end;
{ TChatChoices }
destructor TChatChoices.Destroy;
begin
if Assigned(FMessage) then
FMessage.Free;
if Assigned(FDelta) then
FDelta.Free;
inherited;
end;
end.