-
Notifications
You must be signed in to change notification settings - Fork 58
/
OpenAI.Component.Functions.pas
187 lines (153 loc) · 4.58 KB
/
OpenAI.Component.Functions.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
unit OpenAI.Component.Functions;
interface
uses
System.Classes, OpenAI.Chat.Functions;
type
TChatFunction = OpenAI.Chat.Functions.TChatFunction;
TOnFunctionExecute = procedure(Sender: TObject; const Args: string; out Result: string) of object;
TChatFunctionContainer = class(TChatFunction, IChatFunction)
private
FName: string;
FParameters: string;
FDescription: string;
procedure SetDescription(const Value: string);
procedure SetName(const Value: string);
procedure SetParameters(const Value: string);
protected
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;
end;
TFunctionCollectionItem = class(TCollectionItem)
private
FFunction: IChatFunction;
FOnFunctionExecute: TOnFunctionExecute;
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);
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 OnFunctionExecute: TOnFunctionExecute read FOnFunctionExecute 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>;
published
property Items: TFunctionCollection read FItems write FItems;
end;
implementation
{ TOpenAIChatFunctions }
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.GetParameters: string;
begin
Result := FFunction.Parameters;
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
FOnFunctionExecute := Value;
end;
procedure TFunctionCollectionItem.SetParameters(const Value: string);
begin
TChatFunctionContainer(FFunction).FParameters := Value;
end;
{ TChatFunctionContainer }
function TChatFunctionContainer.Execute(const Args: string): string;
begin
Result := '';
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;
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;
end.