forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAI.Component.Functions.pas
More file actions
236 lines (194 loc) · 6.12 KB
/
OpenAI.Component.Functions.pas
File metadata and controls
236 lines (194 loc) · 6.12 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
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
unit OpenAI.Component.Functions;
interface
uses
System.Classes, System.SysUtils, OpenAI.Chat.Functions;
type
TChatFunction = OpenAI.Chat.Functions.TChatFunction;
EFunctionNotImplemented = class(Exception);
EFunctionNotFound = class(Exception);
TOnFunctionExecute = procedure(Sender: TObject; const Args: string; out Result: string) of object;
TChatFunctionContainer = class(TChatFunction, IChatFunction)
private
FName: string;
FParameters: string;
FDescription: string;
FStrict: Boolean;
FFunction: TOnFunctionExecute;
procedure SetDescription(const Value: string);
procedure SetName(const Value: string);
procedure SetParameters(const Value: string);
procedure SetStrict(const Value: Boolean);
protected
function GetStrict: Boolean; override;
function GetDescription: string; override;
function GetName: string; override;
function GetParameters: string; override;
public
function Execute(const Args: string): string; override;
property Description: string read GetDescription write SetDescription;
property Name: string read GetName write SetName;
property Parameters: string read GetParameters write SetParameters;
property Strict: Boolean read GetStrict write SetStrict;
end;
TFunctionCollectionItem = class(TCollectionItem)
private
FFunction: IChatFunction;
function GetDescription: string;
function GetName: string;
function GetParameters: string;
procedure SetDescription(const Value: string);
procedure SetName(const Value: string);
procedure SetParameters(const Value: string);
procedure SetOnFunctionExecute(const Value: TOnFunctionExecute);
function GetOnFunctionExecute: TOnFunctionExecute;
function GetStrict: Boolean;
procedure SetStrict(const Value: Boolean);
protected
function GetDisplayName: string; override;
published
property Description: string read GetDescription write SetDescription;
property Name: string read GetName write SetName;
property Parameters: string read GetParameters write SetParameters;
property Strict: Boolean read GetStrict write SetStrict;
property OnFunctionExecute: TOnFunctionExecute read GetOnFunctionExecute write SetOnFunctionExecute;
constructor Create(Collection: TCollection); override;
destructor Destroy; override;
end;
TFunctionCollection = class(TOwnedCollection)
end;
TOpenAIChatFunctions = class(TComponent)
private
FItems: TFunctionCollection;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
function GetList: TArray<IChatFunction>;
function Call(const FuncName, FuncArgs: string): string;
published
property Items: TFunctionCollection read FItems write FItems;
end;
implementation
{ TOpenAIChatFunctions }
function TOpenAIChatFunctions.Call(const FuncName, FuncArgs: string): string;
begin
for var Item in FItems do
if TFunctionCollectionItem(Item).Name = FuncName then
Exit(TFunctionCollectionItem(Item).FFunction.Execute(FuncArgs));
raise EFunctionNotFound.CreateFmt('Function "%s" not found', [FuncName]);
end;
constructor TOpenAIChatFunctions.Create(AOwner: TComponent);
begin
inherited;
FItems := TFunctionCollection.Create(Self, TFunctionCollectionItem);
end;
destructor TOpenAIChatFunctions.Destroy;
begin
FItems.Free;
inherited;
end;
function TOpenAIChatFunctions.GetList: TArray<IChatFunction>;
begin
SetLength(Result, FItems.Count);
var i: Integer := 0;
for var Item in FItems do
begin
Result[i] := TFunctionCollectionItem(Item).FFunction;
Inc(i);
end;
end;
{ TFunctionCollectionItem }
constructor TFunctionCollectionItem.Create(Collection: TCollection);
begin
inherited;
FFunction := TChatFunctionContainer.Create;
end;
destructor TFunctionCollectionItem.Destroy;
begin
FFunction := nil;
inherited;
end;
function TFunctionCollectionItem.GetDescription: string;
begin
Result := FFunction.Description;
end;
function TFunctionCollectionItem.GetDisplayName: string;
begin
Result := FFunction.Name;
end;
function TFunctionCollectionItem.GetName: string;
begin
Result := FFunction.Name;
end;
function TFunctionCollectionItem.GetOnFunctionExecute: TOnFunctionExecute;
begin
Result := TChatFunctionContainer(FFunction).FFunction;
end;
function TFunctionCollectionItem.GetParameters: string;
begin
Result := FFunction.Parameters;
end;
function TFunctionCollectionItem.GetStrict: Boolean;
begin
Result := TChatFunctionContainer(FFunction).FStrict;
end;
procedure TFunctionCollectionItem.SetDescription(const Value: string);
begin
TChatFunctionContainer(FFunction).FDescription := Value;
end;
procedure TFunctionCollectionItem.SetName(const Value: string);
begin
TChatFunctionContainer(FFunction).FName := Value;
end;
procedure TFunctionCollectionItem.SetOnFunctionExecute(const Value: TOnFunctionExecute);
begin
TChatFunctionContainer(FFunction).FFunction := Value;
end;
procedure TFunctionCollectionItem.SetParameters(const Value: string);
begin
TChatFunctionContainer(FFunction).FParameters := Value;
end;
procedure TFunctionCollectionItem.SetStrict(const Value: Boolean);
begin
TChatFunctionContainer(FFunction).FStrict := Value;
end;
{ TChatFunctionContainer }
function TChatFunctionContainer.Execute(const Args: string): string;
begin
if Assigned(FFunction) then
FFunction(Self, Args, Result)
else
raise EFunctionNotImplemented.Create('Function not implemented');
end;
function TChatFunctionContainer.GetDescription: string;
begin
Result := FDescription;
end;
function TChatFunctionContainer.GetName: string;
begin
Result := FName;
end;
function TChatFunctionContainer.GetParameters: string;
begin
Result := FParameters;
end;
function TChatFunctionContainer.GetStrict: Boolean;
begin
Result := FStrict;
end;
procedure TChatFunctionContainer.SetDescription(const Value: string);
begin
FDescription := Value;
end;
procedure TChatFunctionContainer.SetName(const Value: string);
begin
FName := Value;
end;
procedure TChatFunctionContainer.SetParameters(const Value: string);
begin
FParameters := Value;
end;
procedure TChatFunctionContainer.SetStrict(const Value: Boolean);
begin
FStrict := Value;
end;
end.