forked from HemulGM/DelphiOpenAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAI.Edits.pas
147 lines (125 loc) · 4.07 KB
/
OpenAI.Edits.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
unit OpenAI.Edits;
interface
uses
System.SysUtils, OpenAI.API.Params, OpenAI.API;
type
TEditParams = class(TJSONParam)
/// <summary>
/// ID of the model to use. You can use the List models API to see all of your available models,
/// or see our Model overview for descriptions of them.
/// </summary>
function Model(const Value: string): TEditParams;
/// <summary>
/// The input text to use as a starting point for the edit.
/// </summary>
function Input(const Value: string): TEditParams; overload;
/// <summary>
/// The instruction that tells the model how to edit the prompt.
/// </summary>
function Instruction(const Value: string): TEditParams; overload;
/// <summary>
/// What sampling temperature to use. Higher values means the model will take more risks.
/// Try 0.9 for more creative applications, and 0 (argmax sampling) for ones with a well-defined answer.
/// We generally recommend altering this or top_p but not both.
/// </summary>
function Temperature(const Value: Single = 1): TEditParams;
/// <summary>
/// An alternative to sampling with temperature, called nucleus sampling,
/// where the model considers the results of the tokens with top_p probability mass.
/// So 0.1 means only the tokens comprising the top 10% probability mass are considered.
/// We generally recommend altering this or temperature but not both.
/// </summary>
function TopP(const Value: Single = 1): TEditParams;
/// <summary>
/// How many edits to generate for the input and instruction.
/// </summary>
function N(const Value: Integer = 1): TEditParams;
constructor Create; override;
end;
TEditUsage = class
private
FCompletion_tokens: Int64;
FPrompt_tokens: Int64;
FTotal_tokens: Int64;
public
property CompletionTokens: Int64 read FCompletion_tokens write FCompletion_tokens;
property PromptTokens: Int64 read FPrompt_tokens write FPrompt_tokens;
property TotalTokens: Int64 read FTotal_tokens write FTotal_tokens;
end;
TEditChoices = class
private
FIndex: Int64;
FText: string;
public
property Index: Int64 read FIndex write FIndex;
property Text: string read FText write FText;
end;
TEdits = class
private
FChoices: TArray<TEditChoices>;
FCreated: Int64;
FObject: string;
FUsage: TEditUsage;
public
property &Object: string read FObject write FObject;
property Choices: TArray<TEditChoices> read FChoices write FChoices;
property Created: Int64 read FCreated write FCreated;
property Usage: TEditUsage read FUsage write FUsage;
destructor Destroy; override;
end;
TEditsRoute = class(TOpenAIAPIRoute)
public
/// <summary>
/// Creates a new edit for the provided input, instruction, and parameters
/// </summary>
function Create(ParamProc: TProc<TEditParams>): TEdits;
end;
implementation
{ TEditsRoute }
function TEditsRoute.Create(ParamProc: TProc<TEditParams>): TEdits;
begin
Result := API.Post<TEdits, TEditParams>('edits', ParamProc);
end;
{ TEdits }
destructor TEdits.Destroy;
var
Item: TEditChoices;
begin
if Assigned(FUsage) then
FUsage.Free;
for Item in FChoices do
if Assigned(Item) then
Item.Free;
inherited;
end;
{ TEditParams }
constructor TEditParams.Create;
begin
inherited;
Model('text-davinci-edit-001');
end;
function TEditParams.Model(const Value: string): TEditParams;
begin
Result := TEditParams(Add('model', Value));
end;
function TEditParams.N(const Value: Integer): TEditParams;
begin
Result := TEditParams(Add('n', Value));
end;
function TEditParams.Input(const Value: string): TEditParams;
begin
Result := TEditParams(Add('input', Value));
end;
function TEditParams.Instruction(const Value: string): TEditParams;
begin
Result := TEditParams(Add('instruction', Value));
end;
function TEditParams.Temperature(const Value: Single): TEditParams;
begin
Result := TEditParams(Add('temperature', Value));
end;
function TEditParams.TopP(const Value: Single): TEditParams;
begin
Result := TEditParams(Add('top_p', Value));
end;
end.