-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathOpenAI.Assistants.pas
623 lines (543 loc) · 21.4 KB
/
OpenAI.Assistants.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
unit OpenAI.Assistants;
interface
uses
System.SysUtils, Rest.Json, Rest.Json.Types, REST.JsonReflect, System.JSON,
OpenAI.API, OpenAI.API.Params, OpenAI.Types, OpenAI.Chat.Functions;
type
TAssistantTool = class abstract(TJSONParam)
//Code interpreter tool
//File search tool
//Function tool
end;
TAssistantCodeInterpreterTool = class(TAssistantTool)
public
constructor Create; override;
end;
TAssistantFileSearch = class(TJSONParam)
/// <summary>
/// The maximum number of results the file search tool should output.
/// The default is 20 for gpt-4* models and 5 for gpt-3.5-turbo. This number should be between 1 and 50 inclusive.
/// Note that the file search tool may output fewer than max_num_results results.
/// See the file search tool documentation for more information.
/// </summary>
function MaxNumResults(const Value: Int64): TAssistantFileSearch;
end;
TAssistantFileSearchTool = class(TAssistantTool)
public
constructor Create; override;
/// <summary>
/// Overrides for the file search tool.
/// </summary>
function FileSearch(const Value: TAssistantFileSearch): TAssistantFileSearchTool;
end;
TAssistantFunctionTool = class(TAssistantTool)
public
constructor Create; override;
function &Function(const Value: IChatFunction): TAssistantFunctionTool;
end;
TAssistantListParams = class(TJSONParam)
/// <summary>
/// A cursor for use in pagination. after is an object ID that defines your place in the list.
/// For instance, if you make a list request and receive 100 objects, ending with obj_foo,
/// your subsequent call can include after=obj_foo in order to fetch the next page of the list.
/// </summary>
function After(const Value: string): TAssistantListParams;
/// <summary>
/// A cursor for use in pagination. before is an object ID that defines your place in the list.
/// For instance, if you make a list request and receive 100 objects, ending with obj_foo,
/// your subsequent call can include before=obj_foo in order to fetch the previous page of the list.
/// </summary>
function Before(const Value: string): TAssistantListParams;
/// <summary>
/// A limit on the number of objects to be returned. Limit can range between 1 and 100, and the default is 20.
/// </summary>
function Limit(const Value: Integer): TAssistantListParams;
/// <summary>
/// Sort order by the created_at timestamp of the objects. asc for ascending order and desc for descending order.
/// </summary>
function Order(const Value: string): TAssistantListParams;
end;
TAssistantToolResourcesParams = class(TJSONParam)
function CodeInterpreter(const FileIds: TArray<string>): TAssistantToolResourcesParams;
//function FileSearch(const VectorStoreIds: TArray<string>): TAssistantToolResourcesParams;
end;
TAssistantResponseFormat = class abstract(TJSONParam)
private
/// <summary>
/// The type of response format being defined
/// </summary>
function FormatType(const Value: string): TAssistantResponseFormat;
end;
/// <summary>
/// Default response format. Used to generate text responses.
/// </summary>
TAssistantResponseText = class(TAssistantResponseFormat)
public
constructor Create; override;
end;
/// <summary>
/// JSON object response format. An older method of generating JSON responses.
/// Using json_schema is recommended for models that support it.
/// Note that the model will not generate JSON without a system or user message instructing it to do so.
/// </summary>
TAssistantResponseJsonObject = class(TAssistantResponseFormat)
public
constructor Create; override;
end;
/// <summary>
/// JSON Schema response format. Used to generate structured JSON responses.
/// Structured Outputs configuration options, including a JSON Schema.
/// </summary>
TAssistantResponseJsonSchema = class(TAssistantResponseFormat)
private
FJsonSchema: TJSONObject;
public
constructor Create; override;
/// <summary>
/// Required. The name of the response format. Must be a-z, A-Z, 0-9, or contain underscores and dashes, with a maximum length of 64.
/// </summary>
function Name(const Value: string): TAssistantResponseJsonSchema;
/// <summary>
/// A description of what the response format is for, used by the model to determine how to respond in the format.
/// </summary>
function Description(const Value: string): TAssistantResponseJsonSchema;
/// <summary>
/// The schema for the response format, described as a JSON Schema object.
/// </summary>
function Schema(const Value: TJSONObject): TAssistantResponseJsonSchema;
/// <summary>
/// Whether to enable strict schema adherence when generating the output.
/// If set to true, the model will always follow the exact schema defined in the schema field.
/// Only a subset of JSON Schema is supported when strict is true. To learn more, read the Structured Outputs guide.
/// </summary>
function strict(const Value: Boolean = True): TAssistantResponseJsonSchema;
end;
TAssistantParams = class(TJSONParam)
/// <summary>
/// ID of the model to use. You can use the List models API to see all of your available models,
/// or see our Model overview for descriptions of them.
/// </summary>
function Model(const Value: string): TAssistantParams;
/// <summary>
/// The description of the assistant. The maximum length is 512 characters.
/// </summary>
function Description(const Value: string): TAssistantParams; overload;
/// <summary>
/// The system instructions that the assistant uses. The maximum length is 32768 characters.
/// </summary>
function Instructions(const Value: string): TAssistantParams; overload;
/// <summary>
/// Set of 16 key-value pairs that can be attached to an object. This can be useful
/// for storing additional information about the object in a structured format.
/// Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
/// </summary>
function Metadata(const Value: TJSONParam): TAssistantParams;
/// <summary>
/// The name of the assistant. The maximum length is 256 characters.
/// </summary>
function Name(const Value: string): TAssistantParams; overload;
/// <summary>
/// o-series models only.
/// Constrains effort on reasoning for reasoning models.
/// Currently supported values are low, medium, and high. Reducing reasoning
/// effort can result in faster responses and fewer tokens used on reasoning in a response.
/// </summary>
function ReasoningEffort(const Value: string): TAssistantParams; overload;
/// <summary>
/// Specifies the format that the model must output. Compatible with GPT-4o, GPT-4 Turbo,
/// and all GPT-3.5 Turbo models since gpt-3.5-turbo-1106.
///
/// Setting to { "type": "json_schema", "json_schema": {...} } enables Structured Outputs which
/// ensures the model will match your supplied JSON schema. Learn more in the Structured Outputs guide.
///
/// Setting to { "type": "json_object" } enables JSON mode, which ensures the message the model generates is valid JSON.
///
/// Important: when using JSON mode, you must also instruct the model to produce JSON yourself via a
/// system or user message. Without this, the model may generate an unending stream of whitespace
/// until the generation reaches the token limit, resulting in a long-running and seemingly "stuck"
/// request. Also note that the message content may be partially cut off if finish_reason="length",
/// which indicates the generation exceeded max_tokens or the conversation exceeded the max
/// context length.
/// </summary>
function ResponseFormat(const Value: TAssistantResponseFormat): TAssistantParams; 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.
/// </summary>
function Temperature(const Value: Extended): TAssistantParams; overload;
/// <summary>
/// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant.
/// Tools can be of types code_interpreter, file_search, or function.
/// </summary>
function Tools(const Value: TArray<TAssistantTool>): TAssistantParams; overload;
/// <summary>
/// A set of resources that are used by the assistant's tools. The resources are specific to the type of tool.
/// For example, the code_interpreter tool requires a list of file IDs,
/// while the file_search tool requires a list of vector store IDs.
/// </summary>
function ToolResources(const Value: TAssistantToolResourcesParams): TAssistantParams; overload;
/// <summary>
/// A list of file IDs attached to this assistant. There can be a maximum of 20 files
/// attached to the assistant. Files are ordered by their creation date in ascending order.
/// </summary>
function FileIds(const Value: TArray<string>): TAssistantParams;
constructor Create; override;
end;
TMetadata = class
end;
TFunctionParameters = class
end;
TToolFunction = class
private
[JsonNameAttribute('name')]
FName: string;
[JsonNameAttribute('description')]
FDescription: string;
[JsonNameAttribute('parameters')]
FParameters: TFunctionParameters;
public
/// <summary>
/// The name of the function to be called. Must be a-z, A-Z, 0-9, or contain underscores and dashes,
/// with a maximum length of 64.
/// </summary>
property Name: string read FName write FName;
/// <summary>
/// A description of what the function does, used by the model to choose when and how to call the function.
/// </summary>
property Description: string read FDescription write FDescription;
/// <summary>
/// The parameters the functions accepts, described as a JSON Schema object. See the guide for examples,
/// and the JSON Schema reference for documentation about the format.
/// To describe a function that accepts no parameters, provide the value {"type": "object", "properties": {}}.
/// </summary>
property Parameters: TFunctionParameters read FParameters write FParameters;
destructor Destroy; override;
end;
TTool = class
private
[JsonNameAttribute('type')]
FType: string;
[JsonNameAttribute('function')]
FFunction: TToolFunction;
public
property &Type: string read FType write FType;
property &Function: TToolFunction read FFunction write FFunction;
destructor Destroy; override;
end;
/// <summary>
/// Represents an assistant that can call the model and use tools.
/// </summary>
TAssistant = class
private
[JsonNameAttribute('id')]
FId: string;
[JsonNameAttribute('object')]
FObject: string;
[JsonNameAttribute('created_at')]
FCreatedAt: Int64;
[JsonNameAttribute('name')]
FName: string;
[JsonNameAttribute('description')]
FDescription: string;
[JsonNameAttribute('model')]
FModel: string;
[JsonNameAttribute('instructions')]
FInstructions: string;
[JsonNameAttribute('tools')]
FTools: TArray<TTool>;
[JsonNameAttribute('file_ids')]
FFileIds: TArray<string>;
[JsonNameAttribute('metadata')]
FMetadata: TMetadata;
public
/// <summary>
/// The identifier, which can be referenced in API endpoints.
/// </summary>
property Id: string read FId write FId;
/// <summary>
/// The object type, which is always assistant.
/// </summary>
property &Object: string read FObject write FObject;
/// <summary>
/// The Unix timestamp (in seconds) for when the assistant was created.
/// </summary>
property CreatedAt: Int64 read FCreatedAt write FCreatedAt;
/// <summary>
/// The name of the assistant. The maximum length is 256 characters.
/// </summary>
property Name: string read FName write FName;
/// <summary>
/// The description of the assistant. The maximum length is 512 characters.
/// </summary>
property Description: string read FDescription write FDescription;
/// <summary>
/// ID of the model to use. You can use the List models API to see all of your available models,
/// or see our Model overview for descriptions of them.
/// </summary>
property Model: string read FModel write FModel;
/// <summary>
/// The system instructions that the assistant uses. The maximum length is 32768 characters.
/// </summary>
property Instructions: string read FInstructions write FInstructions;
/// <summary>
/// A list of tool enabled on the assistant. There can be a maximum of 128 tools per assistant.
/// Tools can be of types code_interpreter, retrieval, or function.
/// </summary>
property Tools: TArray<TTool> read FTools write FTools;
/// <summary>
/// A list of file IDs attached to this assistant.
/// There can be a maximum of 20 files attached to the assistant.
/// Files are ordered by their creation date in ascending order.
/// </summary>
property FileIds: TArray<string> read FFileIds write FFileIds;
/// <summary>
/// Set of 16 key-value pairs that can be attached to an object.
/// This can be useful for storing additional information about the object in a structured format.
/// Keys can be a maximum of 64 characters long and values can be a maxium of 512 characters long.
/// </summary>
property Metadata: TMetadata read FMetadata write FMetadata;
destructor Destroy; override;
end;
TAssistants = class
private
FObject: string;
FData: TArray<TAssistant>;
FHas_more: Boolean;
FLast_id: string;
FFirst_id: string;
public
property &Object: string read FObject write FObject;
property Data: TArray<TAssistant> read FData write FData;
property HasMore: Boolean read FHas_more write FHas_more;
property FirstId: string read FFirst_id write FFirst_id;
property LastId: string read FLast_id write FLast_id;
destructor Destroy; override;
end;
TAssistantsRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Create an assistant with a model and instructions.
/// </summary>
function Create(ParamProc: TProc<TAssistantParams>): TAssistant;
/// <summary>
/// Retrieves an assistant.
/// </summary>
/// <param name="AssistantId">The ID of the assistant to retrieve.</param>
function Retrieve(const AssistantId: string): TAssistant;
/// <summary>
/// Modifies an assistant.
/// </summary>
/// <param name="AssistantId">The ID of the assistant to modify.</param>
/// <param name="ParamProc">Params</param>
function Modify(const AssistantId: string; ParamProc: TProc<TAssistantParams>): TAssistant;
/// <summary>
/// Delete an assistant.
/// </summary>
/// <param name="AssistantId">The ID of the assistant to delete.</param>
function Delete(const AssistantId: string): TDeletionStatus;
/// <summary>
/// Retrieves an assistant.
/// </summary>
function List(ParamProc: TProc<TAssistantListParams> = nil): TAssistants;
end;
implementation
{ TAssistant }
destructor TAssistant.Destroy;
var
Item: TTool;
begin
for Item in FTools do
Item.Free;
FMetadata.Free;
inherited;
end;
{ TAssistantsRoute }
function TAssistantsRoute.Create(ParamProc: TProc<TAssistantParams>): TAssistant;
begin
Result := API.Post<TAssistant, TAssistantParams>('assistants', ParamProc);
end;
function TAssistantsRoute.Delete(const AssistantId: string): TDeletionStatus;
begin
Result := API.Delete<TDeletionStatus>('assistants/' + AssistantId);
end;
function TAssistantsRoute.List(ParamProc: TProc<TAssistantListParams>): TAssistants;
begin
Result := API.Get<TAssistants, TAssistantListParams>('assistants', ParamProc);
end;
function TAssistantsRoute.Modify(const AssistantId: string; ParamProc: TProc<TAssistantParams>): TAssistant;
begin
Result := API.Post<TAssistant, TAssistantParams>('assistants/' + AssistantId, ParamProc);
end;
function TAssistantsRoute.Retrieve(const AssistantId: string): TAssistant;
begin
Result := API.Get<TAssistant>('assistants/' + AssistantId);
end;
{ TAssistantParams }
constructor TAssistantParams.Create;
begin
inherited;
end;
function TAssistantParams.Description(const Value: string): TAssistantParams;
begin
Result := TAssistantParams(Add('description', Value));
end;
function TAssistantParams.FileIds(const Value: TArray<string>): TAssistantParams;
begin
Result := TAssistantParams(Add('file_ids', Value));
end;
function TAssistantParams.Instructions(const Value: string): TAssistantParams;
begin
Result := TAssistantParams(Add('instructions', Value));
end;
function TAssistantParams.Metadata(const Value: TJSONParam): TAssistantParams;
begin
Result := TAssistantParams(Add('metadata', Value));
end;
function TAssistantParams.Model(const Value: string): TAssistantParams;
begin
Result := TAssistantParams(Add('model', Value));
end;
function TAssistantParams.Name(const Value: string): TAssistantParams;
begin
Result := TAssistantParams(Add('name', Value));
end;
function TAssistantParams.ReasoningEffort(const Value: string): TAssistantParams;
begin
Result := TAssistantParams(Add('reasoning_effort', Value));
end;
function TAssistantParams.ResponseFormat(const Value: TAssistantResponseFormat): TAssistantParams;
begin
Result := TAssistantParams(Add('response_format', Value));
end;
function TAssistantParams.Temperature(const Value: Extended): TAssistantParams;
begin
Result := TAssistantParams(Add('temperature', Value));
end;
function TAssistantParams.ToolResources(const Value: TAssistantToolResourcesParams): TAssistantParams;
begin
Result := TAssistantParams(Add('tool_resources', Value));
end;
function TAssistantParams.Tools(const Value: TArray<TAssistantTool>): TAssistantParams;
begin
Result := TAssistantParams(Add('tools', TArray<TJSONParam>(Value)));
end;
{ TAssistants }
destructor TAssistants.Destroy;
var
Item: TAssistant;
begin
for Item in FData do
Item.Free;
inherited;
end;
{ TAssistantListParams }
function TAssistantListParams.After(const Value: string): TAssistantListParams;
begin
Result := TAssistantListParams(Add('after', Value));
end;
function TAssistantListParams.Before(const Value: string): TAssistantListParams;
begin
Result := TAssistantListParams(Add('before', Value));
end;
function TAssistantListParams.Limit(const Value: Integer): TAssistantListParams;
begin
Result := TAssistantListParams(Add('limit', Value));
end;
function TAssistantListParams.Order(const Value: string): TAssistantListParams;
begin
Result := TAssistantListParams(Add('order', Value));
end;
{ TTool }
destructor TTool.Destroy;
begin
FFunction.Free;
inherited;
end;
{ TToolFunction }
destructor TToolFunction.Destroy;
begin
FParameters.Free;
inherited;
end;
{ TAssistantFunctionTool }
function TAssistantFunctionTool.&Function(const Value: IChatFunction): TAssistantFunctionTool;
begin
Result := TAssistantFunctionTool(Add('function', TChatFunction.ToJson(Value)));
end;
constructor TAssistantFunctionTool.Create;
begin
inherited;
Add('type', 'function');
end;
{ TAssistantCodeInterpreterTool }
constructor TAssistantCodeInterpreterTool.Create;
begin
inherited;
Add('type', 'code_interpreter');
end;
{ TAssistantFileSearchTool }
constructor TAssistantFileSearchTool.Create;
begin
inherited;
Add('type', 'file_search');
end;
function TAssistantFileSearchTool.FileSearch(const Value: TAssistantFileSearch): TAssistantFileSearchTool;
begin
Result := TAssistantFileSearchTool(Add('file_search', Value));
end;
{ TAssistantFileSearch }
function TAssistantFileSearch.MaxNumResults(const Value: Int64): TAssistantFileSearch;
begin
Result := TAssistantFileSearch(Add('max_num_results', Value));
end;
{ TAssistantToolResourcesParams }
function TAssistantToolResourcesParams.CodeInterpreter(const FileIds: TArray<string>): TAssistantToolResourcesParams;
begin
Result := TAssistantToolResourcesParams(Add('code_interpreter', TJSONParam.Create.Add('file_ids', FileIds)));
end;
{ TAssistantResponseFormat }
function TAssistantResponseFormat.FormatType(const Value: string): TAssistantResponseFormat;
begin
Result := TAssistantResponseFormat(Add('type', Value));
end;
{ TAssistantResponseText }
constructor TAssistantResponseText.Create;
begin
inherited;
FormatType('text');
end;
{ TAssistantResponseJsonObject }
constructor TAssistantResponseJsonObject.Create;
begin
inherited;
FormatType('json_object');
end;
{ TAssistantResponseJsonSchema }
constructor TAssistantResponseJsonSchema.Create;
begin
inherited;
FormatType('json_schema');
FJsonSchema := TJSONObject.Create;
Add('json_schema', FJsonSchema);
end;
function TAssistantResponseJsonSchema.Description(const Value: string): TAssistantResponseJsonSchema;
begin
FJsonSchema.AddPair('description', Value);
Result := TAssistantResponseJsonSchema(Self);
end;
function TAssistantResponseJsonSchema.Name(const Value: string): TAssistantResponseJsonSchema;
begin
FJsonSchema.AddPair('name', Value);
Result := TAssistantResponseJsonSchema(Self);
end;
function TAssistantResponseJsonSchema.Schema(const Value: TJSONObject): TAssistantResponseJsonSchema;
begin
FJsonSchema.AddPair('schema', Value);
Result := TAssistantResponseJsonSchema(Self);
end;
function TAssistantResponseJsonSchema.strict(const Value: Boolean): TAssistantResponseJsonSchema;
begin
FJsonSchema.AddPair('strict', Value);
Result := TAssistantResponseJsonSchema(Self);
end;
end.