-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathChatGPT.Functions.pas
69 lines (56 loc) · 1.56 KB
/
ChatGPT.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
unit ChatGPT.Functions;
interface
uses
{$IFDEF MSWINDOWS}
Winapi.Windows,
{$ENDIF}
System.SysUtils, System.Generics.Collections, OpenAI.Chat.Functions,
ChatGPT.Functions.external, ChatGPT.Functions.external.Intf;
const
LibExtMask = {$IFDEF MSWINDOWS} '*.dll'; {$ENDIF}
{$IFDEF LINUX} '*.so'; {$ENDIF}
{$IF DEFINED(MACOS) OR
DEFINED(IOS) OR
DEFINED(IOS64)} '*.o'; {$ENDIF}
{$IFDEF ANDROID} '*.so'; {$ENDIF}
type
TGPTFunctionsFunc = function: TArray<IChatFunctionExternal>;
function LoadExternalFunctions: TArray<IChatFunction>;
var
FLoadedLibs: TList<NativeInt>;
implementation
uses
System.IOUtils;
function LoadExternalFunctions: TArray<IChatFunction>;
begin
if TDirectory.Exists('funcs') then
begin
for var Lib in TDirectory.GetFiles('funcs', LibExtMask) do
try
var LibHandle := LoadLibrary(PChar(Lib));
try
var Func: TGPTFunctionsFunc := GetProcAddress(LibHandle, 'gptfunctions');
if @Func <> nil then
begin
for var Item in Func() do
begin
SetLength(Result, Length(Result) + 1);
Result[Length(Result) - 1] := TChatFunctionExternal.Create(Item);
end;
FLoadedLibs.Add(LibHandle);
end;
except
FreeLibrary(LibHandle);
end;
except
//
end;
end;
end;
initialization
FLoadedLibs := TList<NativeInt>.Create;
finalization
for var Lib in FLoadedLibs do
FreeLibrary(Lib);
FLoadedLibs.Free;
end.