-
Notifications
You must be signed in to change notification settings - Fork 58
/
OpenAI.Edits.pas
182 lines (160 loc) · 5.3 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
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
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 "text-davinci-edit-001" or
/// "code-davinci-edit-001" model with this endpoint.
/// </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, between 0 and 2.
/// Higher values like 0.8 will make the output more random,
/// while lower values like 0.2 will make it more focused and deterministic.
/// 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
/// <summary>
/// Number of tokens in the generated completion.
/// </summary>
property CompletionTokens: Int64 read FCompletion_tokens write FCompletion_tokens;
/// <summary>
/// Number of tokens in the prompt.
/// </summary>
property PromptTokens: Int64 read FPrompt_tokens write FPrompt_tokens;
/// <summary>
/// Total number of tokens used in the request (prompt + completion).
/// </summary>
property TotalTokens: Int64 read FTotal_tokens write FTotal_tokens;
end;
TEditChoices = class
private
FIndex: Int64;
FText: string;
FFinish_reason: string;
public
/// <summary>
/// The index of the choice in the list of choices.
/// </summary>
property Index: Int64 read FIndex write FIndex;
/// <summary>
/// The edited result.
/// </summary>
property Text: string read FText write FText;
/// <summary>
/// The reason the model stopped generating tokens.
/// This will be "stop" if the model hit a natural stop point or a provided stop sequence,
/// "length" if the maximum number of tokens specified in the request was reached,
/// or "content_filter" if content was omitted due to a flag from our content filters.
/// </summary>
property FinishReason: string read FFinish_reason write FFinish_reason;
end;
TEdits = class
private
FChoices: TArray<TEditChoices>;
FCreated: Int64;
FObject: string;
FUsage: TEditUsage;
public
/// <summary>
/// The object type, which is always "edit".
/// </summary>
property &Object: string read FObject write FObject;
/// <summary>
/// A list of edit choices. Can be more than one if n is greater than 1.
/// </summary>
property Choices: TArray<TEditChoices> read FChoices write FChoices;
/// <summary>
/// The Unix timestamp (in seconds) of when the edit was created.
/// </summary>
property Created: Int64 read FCreated write FCreated;
/// <summary>
/// Usage statistics for the completion request.
/// </summary>
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; deprecated;
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
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.