-
Notifications
You must be signed in to change notification settings - Fork 58
/
OpenAI.pas
1048 lines (938 loc) · 34.3 KB
/
OpenAI.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit OpenAI;
interface
uses
System.SysUtils, System.Classes, OpenAI.Completions, OpenAI.Edits,
OpenAI.Images, OpenAI.Models, OpenAI.Embeddings, OpenAI.API,
OpenAI.Moderations, OpenAI.Engines, OpenAI.Files, OpenAI.FineTunes,
OpenAI.Chat, OpenAI.Audio, OpenAI.FineTuning, System.Net.URLClient,
OpenAI.Assistants;
type
THeaderItem = System.Net.URLClient.TNameValuePair;
IOpenAI = interface
['{F4CF7FB9-9B73-48FB-A3FE-1E98CCEFCAF0}']
procedure SetToken(const Value: string);
function GetToken: string;
function GetBaseUrl: string;
procedure SetBaseUrl(const Value: string);
function GetOrganization: string;
procedure SetOrganization(const Value: string);
function GetAPI: TOpenAIAPI;
function GetCompletionsRoute: TCompletionsRoute;
function GetEditsRoute: TEditsRoute;
function GetImagesRoute: TImagesRoute;
function GetModelsRoute: TModelsRoute;
function GetEmbeddingsRoute: TEmbeddingsRoute;
function GetModerationsRoute: TModerationsRoute;
function GetEnginesRoute: TEnginesRoute;
function GetFilesRoute: TFilesRoute;
function GetFineTunesRoute: TFineTunesRoute;
function GetFineTuningRoute: TFineTuningRoute;
function GetChatRoute: TChatRoute;
function GetAudioRoute: TAudioRoute;
function GetAssistantsRoute: TAssistantsRoute;
/// <summary>
/// Direct access to queries
/// </summary>
property API: TOpenAIAPI read GetAPI;
/// <summary>
/// The OpenAI API uses API keys for authentication.
/// Visit your API Keys page (https://beta.openai.com/account/api-keys) to retrieve the API key you'll use in your requests.
/// Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps).
/// Production requests must be routed through your own backend server where your API key can be securely
/// loaded from an environment variable or key management service.
/// </summary>
property Token: string read GetToken write SetToken;
/// <summary>
/// Base Url (https://api.openai.com/v1)
/// </summary>
property BaseURL: string read GetBaseUrl write SetBaseUrl;
/// <summary>
/// For users who belong to multiple organizations, you can pass a header to specify which organization
/// is used for an API request. Usage from these API requests will count against the specified organization's
/// subscription quota.
/// </summary>
property Organization: string read GetOrganization write SetOrganization;
/// <summary>
/// Given a prompt, the model will return one or more predicted completions,
/// and can also return the probabilities of alternative tokens at each position.
/// </summary>
property Completion: TCompletionsRoute read GetCompletionsRoute;
/// <summary>
/// Given a prompt and an instruction, the model will return an edited version of the prompt.
/// </summary>
property Edit: TEditsRoute read GetEditsRoute;
/// <summary>
/// Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
property Image: TImagesRoute read GetImagesRoute;
/// <summary>
/// List and describe the various models available in the API.
/// You can refer to the Models documentation to understand what models are available and the differences between them.
/// </summary>
property Model: TModelsRoute read GetModelsRoute;
/// <summary>
/// Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
/// </summary>
property Embedding: TEmbeddingsRoute read GetEmbeddingsRoute;
/// <summary>
/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
/// </summary>
property Moderation: TModerationsRoute read GetModerationsRoute;
/// <summary>
/// These endpoints describe and provide access to the various engines available in the API.
/// The Engines endpoints are deprecated.
/// Please use their replacement, Models, instead.
/// </summary>
property Engine: TEnginesRoute read GetEnginesRoute;
/// <summary>
/// Files are used to upload documents that can be used with features like Fine-tuning.
/// </summary>
property &File: TFilesRoute read GetFilesRoute;
/// <summary>
/// Manage legacy fine-tuning jobs to tailor a model to your specific training data.
/// We recommend transitioning to the updating FineTuning API.
/// Creates a job that fine-tunes a specified model from a given dataset.
/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
/// </summary>
property FineTune: TFineTunesRoute read GetFineTunesRoute;
/// <summary>
/// Manage fine-tuning jobs to tailor a model to your specific training data.
/// </summary>
property FineTuning: TFineTuningRoute read GetFineTuningRoute;
/// <summary>
/// Given a chat conversation, the model will return a chat completion response.
/// </summary>
property Chat: TChatRoute read GetChatRoute;
/// <summary>
/// Learn how to turn audio into text.
/// </summary>
property Audio: TAudioRoute read GetAudioRoute;
/// <summary>
/// Build assistants that can call models and use tools to perform tasks.
/// </summary>
property Assistants: TAssistantsRoute read GetAssistantsRoute;
end;
TOpenAI = class(TInterfacedObject, IOpenAI)
private
FAPI: TOpenAIAPI;
FCompletionsRoute: TCompletionsRoute;
FEditsRoute: TEditsRoute;
FImagesRoute: TImagesRoute;
FImagesAzureRoute: TImagesAzureRoute;
FModelsRoute: TModelsRoute;
FEmbeddingsRoute: TEmbeddingsRoute;
FModerationsRoute: TModerationsRoute;
FEnginesRoute: TEnginesRoute;
FFilesRoute: TFilesRoute;
FFineTunesRoute: TFineTunesRoute;
FFineTuningRoute: TFineTuningRoute;
FChatRoute: TChatRoute;
FAudioRoute: TAudioRoute;
FAssistantsRoute: TAssistantsRoute;
procedure SetToken(const Value: string);
function GetToken: string;
function GetBaseUrl: string;
procedure SetBaseUrl(const Value: string);
function GetOrganization: string;
procedure SetOrganization(const Value: string);
function GetAPI: TOpenAIAPI;
function GetCompletionsRoute: TCompletionsRoute;
function GetEditsRoute: TEditsRoute;
function GetImagesRoute: TImagesRoute;
function GetModelsRoute: TModelsRoute;
function GetEmbeddingsRoute: TEmbeddingsRoute;
function GetModerationsRoute: TModerationsRoute;
function GetEnginesRoute: TEnginesRoute;
function GetFilesRoute: TFilesRoute;
function GetFineTunesRoute: TFineTunesRoute;
function GetChatRoute: TChatRoute;
function GetAudioRoute: TAudioRoute;
function GetAssistantsRoute: TAssistantsRoute;
function GetAzureAPIVersion: string;
function GetAzureDeployment: string;
function GetIsAzure: Boolean;
procedure SetAzureAPIVersion(const Value: string);
procedure SetAzureDeployment(const Value: string);
procedure SetIsAzure(const Value: Boolean);
function GetImagesAzureRoute: TImagesAzureRoute;
function GetFineTuningRoute: TFineTuningRoute;
public
constructor Create; overload;
constructor Create(const AToken: string); overload;
destructor Destroy; override;
public
/// <summary>
/// Direct access to API
/// </summary>
property API: TOpenAIAPI read GetAPI;
/// <summary>
/// The OpenAI API uses API keys for authentication.
/// Visit your API Keys page (https://beta.openai.com/account/api-keys) to retrieve the API key you'll use in your requests.
/// Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps).
/// Production requests must be routed through your own backend server where your API key can be securely
/// loaded from an environment variable or key management service.
/// </summary>
property Token: string read GetToken write SetToken;
/// <summary>
/// Base Url (https://api.openai.com/v1)
/// </summary>
property BaseURL: string read GetBaseUrl write SetBaseUrl;
/// <summary>
/// For users who belong to multiple organizations, you can pass a header to specify which organization
/// is used for an API request. Usage from these API requests will count against the specified organization's
/// subscription quota.
/// </summary>
property Organization: string read GetOrganization write SetOrganization;
property IsAzure: Boolean read GetIsAzure write SetIsAzure;
property AzureApiVersion: string read GetAzureAPIVersion write SetAzureAPIVersion;
property AzureDeployment: string read GetAzureDeployment write SetAzureDeployment;
public
/// <summary>
/// Given a prompt, the model will return one or more predicted completions,
/// and can also return the probabilities of alternative tokens at each position.
/// </summary>
property Completion: TCompletionsRoute read GetCompletionsRoute;
/// <summary>
/// Given a prompt and an instruction, the model will return an edited version of the prompt.
/// </summary>
property Edit: TEditsRoute read GetEditsRoute;
/// <summary>
/// Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
property Image: TImagesRoute read GetImagesRoute;
/// <summary>
/// Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
property ImageAzure: TImagesAzureRoute read GetImagesAzureRoute;
/// <summary>
/// List and describe the various models available in the API.
/// You can refer to the Models documentation to understand what models are available and the differences between them.
/// </summary>
property Model: TModelsRoute read GetModelsRoute;
/// <summary>
/// Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
/// </summary>
property Embedding: TEmbeddingsRoute read GetEmbeddingsRoute;
/// <summary>
/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
/// </summary>
property Moderation: TModerationsRoute read GetModerationsRoute;
/// <summary>
/// These endpoints describe and provide access to the various engines available in the API.
/// The Engines endpoints are deprecated.
/// Please use their replacement, Models, instead.
/// </summary>
property Engine: TEnginesRoute read GetEnginesRoute;
/// <summary>
/// Files are used to upload documents that can be used with features like Fine-tuning.
/// </summary>
property &File: TFilesRoute read GetFilesRoute;
/// <summary>
/// Manage legacy fine-tuning jobs to tailor a model to your specific training data.
/// We recommend transitioning to the updating FineTuning API.
/// Creates a job that fine-tunes a specified model from a given dataset.
/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
/// </summary>
property FineTune: TFineTunesRoute read GetFineTunesRoute;
/// <summary>
/// Manage fine-tuning jobs to tailor a model to your specific training data.
/// </summary>
property FineTuning: TFineTuningRoute read GetFineTuningRoute;
/// <summary>
/// Given a chat conversation, the model will return a chat completion response.
/// </summary>
property Chat: TChatRoute read GetChatRoute;
/// <summary>
/// Learn how to turn audio into text.
/// </summary>
property Audio: TAudioRoute read GetAudioRoute;
/// <summary>
/// Build assistants that can call models and use tools to perform tasks.
/// </summary>
property Assistants: TAssistantsRoute read GetAssistantsRoute;
end;
[ComponentPlatformsAttribute(pidAllPlatforms)]
TOpenAIComponent = class(TComponent, IOpenAI)
private
type
THTTPProxy = class(TPersistent)
private
FOwner: TOpenAIComponent;
procedure SetIP(const Value: string);
procedure SetPassword(const Value: string);
procedure SetPort(const Value: Integer);
procedure SetUserName(const Value: string);
function GetIP: string;
function GetPassword: string;
function GetPort: Integer;
function GetUserName: string;
public
constructor Create(AOwner: TOpenAIComponent);
procedure SetProxy(AIP: string; APort: Integer; AUserName: string = ''; APassword: string = '');
published
property IP: string read GetIP write SetIP;
property Port: Integer read GetPort write SetPort;
property UserName: string read GetUserName write SetUserName;
property Password: string read GetPassword write SetPassword;
end;
THTTPHeader = class(TCollectionItem)
private
FName: string;
FValue: string;
procedure SetName(const Value: string);
procedure SetValue(const Value: string);
published
property Name: string read FName write SetName;
property Value: string read FValue write SetValue;
end;
THTTPHeaders = class(TOwnedCollection)
private
FOnChange: TNotifyEvent;
procedure SetOnChange(const Value: TNotifyEvent);
protected
procedure Notify(Item: TCollectionItem; Action: TCollectionNotification); override;
property OnChange: TNotifyEvent read FOnChange write SetOnChange;
end;
private
FOpenAI: TOpenAI;
FProxy: THTTPProxy;
FCustomHeaders: THTTPHeaders;
procedure FOnChangeHeaders(Sender: TObject);
procedure SetToken(const Value: string);
function GetToken: string;
function GetBaseUrl: string;
procedure SetBaseUrl(const Value: string);
function GetOrganization: string;
procedure SetOrganization(const Value: string);
function GetAPI: TOpenAIAPI;
function GetCompletionsRoute: TCompletionsRoute;
function GetEditsRoute: TEditsRoute;
function GetImagesRoute: TImagesRoute;
function GetModelsRoute: TModelsRoute;
function GetEmbeddingsRoute: TEmbeddingsRoute;
function GetModerationsRoute: TModerationsRoute;
function GetEnginesRoute: TEnginesRoute;
function GetFilesRoute: TFilesRoute;
function GetFineTunesRoute: TFineTunesRoute;
function GetChatRoute: TChatRoute;
function GetAudioRoute: TAudioRoute;
function GetAssistantsRoute: TAssistantsRoute;
function GetFineTuningRoute: TFineTuningRoute;
function GetConnectionTimeout: Integer;
function GetResponseTimeout: Integer;
function GetSendTimeout: Integer;
procedure SetConnectionTimeout(const Value: Integer);
procedure SetResponseTimeout(const Value: Integer);
procedure SetSendTimeout(const Value: Integer);
function GetAzureAPIVersion: string;
function GetAzureDeployment: string;
function GetIsAzure: Boolean;
procedure SetAzureAPIVersion(const Value: string);
procedure SetAzureDeployment(const Value: string);
procedure SetIsAzure(const Value: Boolean);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
public
/// <summary>
/// Direct access to queries
/// </summary>
property API: TOpenAIAPI read GetAPI;
public
/// <summary>
/// Given a prompt, the model will return one or more predicted completions,
/// and can also return the probabilities of alternative tokens at each position.
/// </summary>
property Completion: TCompletionsRoute read GetCompletionsRoute;
/// <summary>
/// Given a prompt and an instruction, the model will return an edited version of the prompt.
/// </summary>
property Edit: TEditsRoute read GetEditsRoute;
/// <summary>
/// Given a prompt and/or an input image, the model will generate a new image.
/// </summary>
property Image: TImagesRoute read GetImagesRoute;
/// <summary>
/// List and describe the various models available in the API.
/// You can refer to the Models documentation to understand what models are available and the differences between them.
/// </summary>
property Model: TModelsRoute read GetModelsRoute;
/// <summary>
/// Get a vector representation of a given input that can be easily consumed by machine learning models and algorithms.
/// </summary>
property Embedding: TEmbeddingsRoute read GetEmbeddingsRoute;
/// <summary>
/// Given a input text, outputs if the model classifies it as violating OpenAI's content policy.
/// </summary>
property Moderation: TModerationsRoute read GetModerationsRoute;
/// <summary>
/// These endpoints describe and provide access to the various engines available in the API.
/// The Engines endpoints are deprecated.
/// Please use their replacement, Models, instead.
/// </summary>
property Engine: TEnginesRoute read GetEnginesRoute;
/// <summary>
/// Files are used to upload documents that can be used with features like Fine-tuning.
/// </summary>
property &File: TFilesRoute read GetFilesRoute;
/// <summary>
/// Manage legacy fine-tuning jobs to tailor a model to your specific training data.
/// We recommend transitioning to the updating FineTuning API.
/// Creates a job that fine-tunes a specified model from a given dataset.
/// Response includes details of the enqueued job including job status and the name of the fine-tuned models once complete.
/// </summary>
property FineTune: TFineTunesRoute read GetFineTunesRoute;
/// <summary>
/// Manage fine-tuning jobs to tailor a model to your specific training data.
/// </summary>
property FineTuning: TFineTuningRoute read GetFineTuningRoute;
/// <summary>
/// Given a chat conversation, the model will return a chat completion response.
/// </summary>
property Chat: TChatRoute read GetChatRoute;
/// <summary>
/// Learn how to turn audio into text.
/// </summary>
property Audio: TAudioRoute read GetAudioRoute;
/// <summary>
/// Build assistants that can call models and use tools to perform tasks.
/// </summary>
property Assistants: TAssistantsRoute read GetAssistantsRoute;
published
/// <summary>
/// The OpenAI API uses API keys for authentication.
/// Visit your API Keys page (https://beta.openai.com/account/api-keys) to retrieve the API key you'll use in your requests.
/// Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps).
/// Production requests must be routed through your own backend server where your API key can be securely
/// loaded from an environment variable or key management service.
/// </summary>
/// <seealso>https://beta.openai.com/account/api-keys</seealso>
property Token: string read GetToken write SetToken;
/// <summary>
/// Base Url (https://api.openai.com/v1)
/// </summary>
property BaseURL: string read GetBaseUrl write SetBaseUrl;
/// <summary>
/// For users who belong to multiple organizations, you can pass a header to specify which organization
/// is used for an API request. Usage from these API requests will count against the specified organization's
/// subscription quota.
/// </summary>
property Organization: string read GetOrganization write SetOrganization;
/// <summary>
/// Http client proxy
/// </summary>
property Proxy: THTTPProxy read FProxy;
/// <summary> Property to set/get the ConnectionTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by Windows, Linux, Android platforms.
/// </summary>
property ConnectionTimeout: Integer read GetConnectionTimeout write SetConnectionTimeout;
/// <summary> Property to set/get the SendTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by Windows, macOS platforms.
/// </summary>
property SendTimeout: Integer read GetSendTimeout write SetSendTimeout;
/// <summary> Property to set/get the ResponseTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by all platforms.
/// </summary>
property ResponseTimeout: Integer read GetResponseTimeout write SetResponseTimeout;
property IsAzure: Boolean read GetIsAzure write SetIsAzure;
property AzureApiVersion: string read GetAzureAPIVersion write SetAzureAPIVersion;
property AzureDeployment: string read GetAzureDeployment write SetAzureDeployment;
property CustomHeaders: THTTPHeaders read FCustomHeaders;
end;
TOpenAIClient = class(TOpenAIComponent)
published
/// <summary>
/// The OpenAI API uses API keys for authentication.
/// Visit your API Keys page (https://beta.openai.com/account/api-keys) to retrieve the API key you'll use in your requests.
/// Remember that your API key is a secret! Do not share it with others or expose it in any client-side code (browsers, apps).
/// Production requests must be routed through your own backend server where your API key can be securely
/// loaded from an environment variable or key management service.
/// </summary>
/// <seealso>https://beta.openai.com/account/api-keys</seealso>
property Token;
/// <summary>
/// Base Url (https://api.openai.com/v1)
/// </summary>
property BaseURL;
/// <summary>
/// For users who belong to multiple organizations, you can pass a header to specify which organization
/// is used for an API request. Usage from these API requests will count against the specified organization's
/// subscription quota.
/// </summary>
property Organization;
/// <summary>
/// Http client proxy
/// </summary>
property Proxy;
/// <summary> Property to set/get the ConnectionTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by Windows, Linux, Android platforms.
/// </summary>
property ConnectionTimeout default TURLClient.DefaultConnectionTimeout;
/// <summary> Property to set/get the SendTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by Windows, macOS platforms.
/// </summary>
property SendTimeout default TURLClient.DefaultSendTimeout;
/// <summary> Property to set/get the ResponseTimeout. Value is in milliseconds.
/// -1 - Infinite timeout. 0 - platform specific timeout. Supported by all platforms.
/// </summary>
property ResponseTimeout default TURLClient.DefaultResponseTimeout;
property IsAzure default False;
property AzureApiVersion;
property AzureDeployment;
property CustomHeaders;
end;
function chat(const Token, Prompt: string; const Model: string = 'gpt-4'): string;
function input(const Print: string = ''): string;
procedure print(const Text: string; const NewLine: Boolean = True);
implementation
uses
OpenAI.Utils.ChatHistory;
procedure print(const Text: string; const NewLine: Boolean);
begin
if NewLine then
Writeln(Text)
else
write(Text);
end;
function input(const Print: string): string;
begin
write(Print);
readln(Result);
end;
var
ConsoleHistory: TChatHistory = nil;
function chat(const Token, Prompt: string; const Model: string): string;
begin
if ConsoleHistory = nil then
begin
ConsoleHistory := TChatHistory.Create;
ConsoleHistory.AutoTrim := True;
end;
try
ConsoleHistory.New(TMessageRole.User, Prompt, TGUID.NewGuid.ToString);
var API: IOpenAI := TOpenAI.Create(Token);
var Chat := API.Chat.Create(
procedure(Params: TChatParams)
begin
Params.Model(Model);
Params.Messages(ConsoleHistory.ToArray);
end);
try
if Length(Chat.Choices) > 0 then
begin
Result := Chat.Choices[0].Message.Content;
ConsoleHistory.New(TMessageRole.Assistant, Result, TGUID.NewGuid.ToString);
end;
finally
Chat.Free;
end;
except
on E: Exception do
Result := 'Error "' + E.Message + '"';
end;
end;
{ TOpenAI }
constructor TOpenAI.Create;
begin
inherited;
FAPI := TOpenAIAPI.Create;
end;
constructor TOpenAI.Create(const AToken: string);
begin
Create;
Token := AToken;
end;
destructor TOpenAI.Destroy;
begin
FCompletionsRoute.Free;
FEditsRoute.Free;
FImagesRoute.Free;
FImagesAzureRoute.Free;
FModelsRoute.Free;
FEmbeddingsRoute.Free;
FModerationsRoute.Free;
FEnginesRoute.Free;
FFilesRoute.Free;
FFineTunesRoute.Free;
FFineTuningRoute.Free;
FChatRoute.Free;
FAudioRoute.Free;
FAPI.Free;
inherited;
end;
function TOpenAI.GetAPI: TOpenAIAPI;
begin
Result := FAPI;
end;
function TOpenAI.GetAssistantsRoute: TAssistantsRoute;
begin
if not Assigned(FAssistantsRoute) then
FAssistantsRoute := TAssistantsRoute.CreateRoute(API);
Result := FAssistantsRoute;
end;
function TOpenAI.GetAudioRoute: TAudioRoute;
begin
if not Assigned(FAudioRoute) then
FAudioRoute := TAudioRoute.CreateRoute(API);
Result := FAudioRoute;
end;
function TOpenAI.GetAzureAPIVersion: string;
begin
Result := FAPI.AzureApiVersion;
end;
function TOpenAI.GetAzureDeployment: string;
begin
Result := FAPI.AzureDeployment;
end;
function TOpenAI.GetBaseUrl: string;
begin
Result := FAPI.BaseURL;
end;
function TOpenAI.GetChatRoute: TChatRoute;
begin
if not Assigned(FChatRoute) then
FChatRoute := TChatRoute.CreateRoute(API);
Result := FChatRoute;
end;
function TOpenAI.GetCompletionsRoute: TCompletionsRoute;
begin
if not Assigned(FCompletionsRoute) then
FCompletionsRoute := TCompletionsRoute.CreateRoute(API);
Result := FCompletionsRoute;
end;
function TOpenAI.GetEditsRoute: TEditsRoute;
begin
if not Assigned(FEditsRoute) then
FEditsRoute := TEditsRoute.CreateRoute(API);
Result := FEditsRoute;
end;
function TOpenAI.GetEmbeddingsRoute: TEmbeddingsRoute;
begin
if not Assigned(FEmbeddingsRoute) then
FEmbeddingsRoute := TEmbeddingsRoute.CreateRoute(API);
Result := FEmbeddingsRoute;
end;
function TOpenAI.GetEnginesRoute: TEnginesRoute;
begin
if not Assigned(FEnginesRoute) then
FEnginesRoute := TEnginesRoute.CreateRoute(API);
Result := FEnginesRoute;
end;
function TOpenAI.GetFilesRoute: TFilesRoute;
begin
if not Assigned(FFilesRoute) then
FFilesRoute := TFilesRoute.CreateRoute(API);
Result := FFilesRoute;
end;
function TOpenAI.GetFineTunesRoute: TFineTunesRoute;
begin
if not Assigned(FFineTunesRoute) then
FFineTunesRoute := TFineTunesRoute.CreateRoute(API);
Result := FFineTunesRoute;
end;
function TOpenAI.GetFineTuningRoute: TFineTuningRoute;
begin
if not Assigned(FFineTuningRoute) then
FFineTuningRoute := TFineTuningRoute.CreateRoute(API);
Result := FFineTuningRoute;
end;
function TOpenAI.GetImagesAzureRoute: TImagesAzureRoute;
begin
if not Assigned(FImagesAzureRoute) then
FImagesAzureRoute := TImagesAzureRoute.CreateRoute(API);
Result := FImagesAzureRoute;
end;
function TOpenAI.GetImagesRoute: TImagesRoute;
begin
if not Assigned(FImagesRoute) then
FImagesRoute := TImagesRoute.CreateRoute(API);
Result := FImagesRoute;
end;
function TOpenAI.GetIsAzure: Boolean;
begin
Result := FAPI.IsAzure;
end;
function TOpenAI.GetModelsRoute: TModelsRoute;
begin
if not Assigned(FModelsRoute) then
FModelsRoute := TModelsRoute.CreateRoute(API);
Result := FModelsRoute;
end;
function TOpenAI.GetModerationsRoute: TModerationsRoute;
begin
if not Assigned(FModerationsRoute) then
FModerationsRoute := TModerationsRoute.CreateRoute(API);
Result := FModerationsRoute;
end;
function TOpenAI.GetOrganization: string;
begin
Result := FAPI.Organization;
end;
function TOpenAI.GetToken: string;
begin
Result := FAPI.Token;
end;
procedure TOpenAI.SetAzureAPIVersion(const Value: string);
begin
FAPI.AzureApiVersion := Value;
end;
procedure TOpenAI.SetAzureDeployment(const Value: string);
begin
FAPI.AzureDeployment := Value;
end;
procedure TOpenAI.SetBaseUrl(const Value: string);
begin
FAPI.BaseURL := Value;
end;
procedure TOpenAI.SetIsAzure(const Value: Boolean);
begin
FAPI.IsAzure := Value;
end;
procedure TOpenAI.SetOrganization(const Value: string);
begin
FAPI.Organization := Value;
end;
procedure TOpenAI.SetToken(const Value: string);
begin
FAPI.Token := Value;
end;
{ TOpenAIComponent }
constructor TOpenAIComponent.Create(AOwner: TComponent);
begin
inherited;
FOpenAI := TOpenAI.Create;
FCustomHeaders := THTTPHeaders.Create(Self, THTTPHeader);
FCustomHeaders.OnChange := FOnChangeHeaders;
FProxy := THTTPProxy.Create(Self);
end;
destructor TOpenAIComponent.Destroy;
begin
FOpenAI.Free;
FCustomHeaders.Free;
FProxy.Free;
inherited;
end;
procedure TOpenAIComponent.FOnChangeHeaders(Sender: TObject);
var
i: Integer;
FHeaders: TNetHeaders;
begin
for i := 0 to FCustomHeaders.Count - 1 do
FHeaders := FHeaders + [TNameValuePair.Create(
THTTPHeader(FCustomHeaders.Items[i]).Name,
THTTPHeader(FCustomHeaders.Items[i]).Value)];
API.CustomHeaders := FHeaders;
end;
function TOpenAIComponent.GetAPI: TOpenAIAPI;
begin
Result := FOpenAI.API;
end;
function TOpenAIComponent.GetAssistantsRoute: TAssistantsRoute;
begin
Result := FOpenAI.GetAssistantsRoute;
end;
function TOpenAIComponent.GetAudioRoute: TAudioRoute;
begin
Result := FOpenAI.GetAudioRoute;
end;
function TOpenAIComponent.GetAzureAPIVersion: string;
begin
Result := FOpenAI.API.AzureApiVersion;
end;
function TOpenAIComponent.GetAzureDeployment: string;
begin
Result := FOpenAI.API.AzureDeployment;
end;
function TOpenAIComponent.GetBaseUrl: string;
begin
Result := FOpenAI.GetBaseUrl;
end;
function TOpenAIComponent.GetChatRoute: TChatRoute;
begin
Result := FOpenAI.GetChatRoute;
end;
function TOpenAIComponent.GetCompletionsRoute: TCompletionsRoute;
begin
Result := FOpenAI.GetCompletionsRoute;
end;
function TOpenAIComponent.GetConnectionTimeout: Integer;
begin
Result := FOpenAI.API.ConnectionTimeout;
end;
function TOpenAIComponent.GetEditsRoute: TEditsRoute;
begin
Result := FOpenAI.GetEditsRoute;
end;
function TOpenAIComponent.GetEmbeddingsRoute: TEmbeddingsRoute;
begin
Result := FOpenAI.GetEmbeddingsRoute;
end;
function TOpenAIComponent.GetEnginesRoute: TEnginesRoute;
begin
Result := FOpenAI.GetEnginesRoute;
end;
function TOpenAIComponent.GetFilesRoute: TFilesRoute;
begin
Result := FOpenAI.GetFilesRoute;
end;
function TOpenAIComponent.GetFineTunesRoute: TFineTunesRoute;
begin
Result := FOpenAI.GetFineTunesRoute;
end;
function TOpenAIComponent.GetFineTuningRoute: TFineTuningRoute;
begin
Result := FOpenAI.GetFineTuningRoute;
end;
function TOpenAIComponent.GetImagesRoute: TImagesRoute;
begin
Result := FOpenAI.GetImagesRoute;
end;
function TOpenAIComponent.GetIsAzure: Boolean;
begin
Result := FOpenAI.API.IsAzure;
end;
function TOpenAIComponent.GetModelsRoute: TModelsRoute;
begin
Result := FOpenAI.GetModelsRoute;
end;
function TOpenAIComponent.GetModerationsRoute: TModerationsRoute;
begin
Result := FOpenAI.GetModerationsRoute;
end;
function TOpenAIComponent.GetOrganization: string;
begin
Result := FOpenAI.GetOrganization;
end;
function TOpenAIComponent.GetResponseTimeout: Integer;
begin
Result := FOpenAI.API.ResponseTimeout;
end;
function TOpenAIComponent.GetSendTimeout: Integer;
begin
Result := FOpenAI.API.SendTimeout;
end;
function TOpenAIComponent.GetToken: string;
begin
Result := FOpenAI.GetToken;
end;
procedure TOpenAIComponent.SetAzureAPIVersion(const Value: string);
begin
FOpenAI.API.AzureApiVersion := Value;
end;
procedure TOpenAIComponent.SetAzureDeployment(const Value: string);
begin
FOpenAI.API.AzureDeployment := Value;
end;
procedure TOpenAIComponent.SetBaseUrl(const Value: string);
begin
FOpenAI.SetBaseUrl(Value);
end;
procedure TOpenAIComponent.SetConnectionTimeout(const Value: Integer);
begin
FOpenAI.API.ConnectionTimeout := Value;
end;
procedure TOpenAIComponent.SetIsAzure(const Value: Boolean);
begin
FOpenAI.API.IsAzure := Value;
end;
procedure TOpenAIComponent.SetOrganization(const Value: string);
begin
FOpenAI.SetOrganization(Value);
end;
procedure TOpenAIComponent.SetResponseTimeout(const Value: Integer);
begin
FOpenAI.API.ResponseTimeout := Value;
end;
procedure TOpenAIComponent.SetSendTimeout(const Value: Integer);
begin
FOpenAI.API.SendTimeout := Value;
end;
procedure TOpenAIComponent.SetToken(const Value: string);
begin
FOpenAI.SetToken(Value);
end;
{ TOpenAIComponent.THTTPProxy }
constructor TOpenAIComponent.THTTPProxy.Create(AOwner: TOpenAIComponent);
begin
inherited Create;
FOwner := AOwner;
end;
function TOpenAIComponent.THTTPProxy.GetIP: string;
begin
Result := FOwner.API.ProxySettings.Host;
end;
function TOpenAIComponent.THTTPProxy.GetPassword: string;
begin
Result := FOwner.API.ProxySettings.Password;
end;
function TOpenAIComponent.THTTPProxy.GetPort: Integer;
begin
Result := FOwner.API.ProxySettings.Port;
end;
function TOpenAIComponent.THTTPProxy.GetUserName: string;
begin
Result := FOwner.API.ProxySettings.UserName;
end;
procedure TOpenAIComponent.THTTPProxy.SetIP(const Value: string);
var
Proxy: TProxySettings;
begin
Proxy := FOwner.API.ProxySettings;
Proxy.Host := Value;
FOwner.API.ProxySettings := Proxy;
end;
procedure TOpenAIComponent.THTTPProxy.SetPassword(const Value: string);
var
Proxy: TProxySettings;
begin
Proxy := FOwner.API.ProxySettings;
Proxy.Password := Value;
FOwner.API.ProxySettings := Proxy;
end;
procedure TOpenAIComponent.THTTPProxy.SetPort(const Value: Integer);
var
Proxy: TProxySettings;
begin
Proxy := FOwner.API.ProxySettings;
Proxy.Port := Value;
FOwner.API.ProxySettings := Proxy;
end;
procedure TOpenAIComponent.THTTPProxy.SetProxy(AIP: string; APort: Integer; AUserName, APassword: string);
begin