-
Notifications
You must be signed in to change notification settings - Fork 58
/
OpenAI.Chat.Functions.Samples.pas
96 lines (80 loc) · 1.97 KB
/
OpenAI.Chat.Functions.Samples.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
unit OpenAI.Chat.Functions.Samples;
interface
uses
System.SysUtils, OpenAI.Chat.Functions;
type
TChatFunctionWeather = class(TChatFunction)
protected
function GetDescription: string; override;
function GetName: string; override;
function GetParameters: string; override;
public
constructor Create; override;
function Execute(const Args: string): string; override;
end;
implementation
uses
System.JSON;
{ TChatFunctionWeather }
constructor TChatFunctionWeather.Create;
begin
inherited;
end;
function TChatFunctionWeather.Execute(const Args: string): string;
var
JSON: TJSONObject;
Location: string;
UnitKind: string;
begin
Result := '';
Location := '';
UnitKind := '';
// Parse arguments
try
JSON := TJSONObject.ParseJSONValue(Args) as TJSONObject;
if Assigned(JSON) then
try
Location := JSON.GetValue('location', '');
UnitKind := JSON.GetValue('unit', '');
finally
JSON.Free;
end;
except
Location := '';
end;
// Invalid arguments
if Location.IsEmpty then
Exit;
// Generate response
JSON := TJSONObject.Create;
try
JSON.AddPair('location', Location);
JSON.AddPair('unit', UnitKind);
JSON.AddPair('temperature', TJSONNumber.Create(72));
JSON.AddPair('forecast', TJSONArray.Create('sunny', 'windy'));
Result := JSON.ToJSON;
finally
JSON.Free;
end;
end;
function TChatFunctionWeather.GetDescription: string;
begin
Result := 'Get the current weather in a given location';
end;
function TChatFunctionWeather.GetName: string;
begin
Result := 'get_current_weather';
end;
function TChatFunctionWeather.GetParameters: string;
begin
Result := // json scheme
'{' +
' "type": "object",' +
' "properties": {' +
' "location": {"type": "string", "description": "The city and state, e.g. San Francisco, CA"},' +
' "unit": {"type": "string", "enum": ["celsius", "fahrenheit"]}' +
' },' +
' "required": ["location"]' +
'}';
end;
end.