forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAI.Chat.Functions.pas
More file actions
96 lines (84 loc) · 3.04 KB
/
Copy pathOpenAI.Chat.Functions.pas
File metadata and controls
96 lines (84 loc) · 3.04 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
unit OpenAI.Chat.Functions;
interface
uses
System.JSON;
type
IChatFunction = interface
['{F2B4D026-5FA9-4499-B5D1-3FEA4885C511}']
function GetDescription: string;
function GetName: string;
function GetParameters: string;
function Execute(const Args: string): string;
/// <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 GetDescription;
/// <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 GetName;
/// <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>
/// <seealso>https://json-schema.org/understanding-json-schema/</seealso>
property Parameters: string read GetParameters;
end;
TChatFunction = class abstract(TInterfacedObject, IChatFunction)
protected
function GetDescription: string; virtual; abstract;
function GetName: string; virtual; abstract;
function GetParameters: string; virtual; abstract;
public
/// <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 GetDescription;
/// <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 GetName;
/// <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>
/// <seealso>https://json-schema.org/understanding-json-schema/</seealso>
property Parameters: string read GetParameters;
function Execute(const Args: string): string; virtual;
class function ToJson(Value: IChatFunction): TJSONObject;
constructor Create; virtual;
end;
implementation
uses
System.SysUtils;
{ TChatFunction }
constructor TChatFunction.Create;
begin
inherited;
end;
function TChatFunction.Execute(const Args: string): string;
begin
Result := '';
end;
class function TChatFunction.ToJson(Value: IChatFunction): TJSONObject;
begin
Result := TJSONObject.Create;
try
Result.AddPair('name', Value.GetName);
Result.AddPair('description', Value.GetDescription);
Result.AddPair('parameters', TJSONObject.ParseJSONValue(Value.GetParameters));
except
on E: Exception do
begin
Result.Free;
raise;
end;
end;
end;
end.