-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApiOpenAIData.cs
392 lines (340 loc) · 10.4 KB
/
ApiOpenAIData.cs
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
// Copyright (c) Sanat. All rights reserved.
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
namespace Sanat.ApiOpenAI
{
#region Text Generation
[Serializable]
public class CompletionRequest
{
public string model;
public string prompt;
public float temperature;
public float top_p;
public float frequency_penalty;
public float presence_penalty;
public int max_tokens;
public CompletionRequest(string modelName, string prompt,
float temperature, float top_p,
float frequency_penalty, float presence_penalty,
int maxTokens)
{
this.model = modelName;
this.prompt = prompt;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = top_p;
this.frequency_penalty = frequency_penalty;
this.presence_penalty = presence_penalty;
}
public CompletionRequest(string modelName, string prompt,
float temperature,
int maxTokens)
{
this.model = modelName;
this.prompt = prompt;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = 1;
this.frequency_penalty = 0;
this.presence_penalty = 0;
}
}
[Serializable]
public class CompletionResponse
{
public string id;
public string @object;
public int created;
public string model;
public Choice[] choices;
public Usage usage;
}
[Serializable]
public class Choice
{
public Message message;
public string finish_reason;
}
[Serializable]
public class Usage
{
public int prompt_tokens;
public int completion_tokens;
public int total_tokens;
}
[Serializable]
public class EditRequest
{
public string model;
public string input;
public string instruction;
public float temperature;
public float top_p;
public float frequency_penalty;
public float presence_penalty;
public int max_tokens;
public EditRequest(string modelName, string input, string instruction,
float temperature, float top_p,
float frequency_penalty, float presence_penalty,
int maxTokens)
{
this.model = modelName;
this.input = input;
this.instruction = instruction;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = top_p;
this.frequency_penalty = frequency_penalty;
this.presence_penalty = presence_penalty;
}
public EditRequest(string modelName, string input, string instruction,
float temperature,
int maxTokens)
{
this.model = modelName;
this.input = input;
this.instruction = instruction;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = 1;
this.frequency_penalty = 0;
this.presence_penalty = 0;
}
}
[Serializable]
public class EditResponse
{
public string @object;
public int created;
public Choice[] choices;
public Usage usage;
}
[Serializable]
public class ChatRequest
{
public string model;
public List<ChatMessage> messages;
public float temperature;
public float top_p;
public float frequency_penalty;
public float presence_penalty;
public int max_tokens;
public ChatRequest(string modelName, List<ChatMessage> messages,
float temperature, float top_p,
float frequency_penalty, float presence_penalty,
int maxTokens)
{
this.model = modelName;
this.messages = messages;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = top_p;
this.frequency_penalty = frequency_penalty;
this.presence_penalty = presence_penalty;
}
public ChatRequest(string modelName, List<ChatMessage> messages,
float temperature, int maxTokens)
{
this.model = modelName;
this.messages = messages;
this.temperature = temperature;
this.max_tokens = maxTokens;
this.top_p = 1;
this.frequency_penalty = 0;
this.presence_penalty = 0;
}
}
[Serializable]
public class ChatMessage
{
public string role;
public string content;
public ChatMessage(string role, string content)
{
this.role = role;
this.content = content;
}
}
[Serializable]
public class Function
{
public string arguments;
public string name;
}
[Serializable]
public class ChatResponse
{
public Choice[] choices;
public Usage usage;
}
[Serializable]
public class ChatChoice
{
public ChatMessage message;
public string finish_reason;
public int index;
}
#endregion
#region Image Generation
[Serializable]
public class ImageGenerationRequest
{
public string prompt;
public int n; // Number of images to generate.
public string size; // Must be 256x256, 512x512, or 1024x1024.
public string response_format;
public string user;
public ImageGenerationRequest(string prompt, int n, string size, string response_format, string user)
{
this.prompt = prompt;
this.n = n;
this.size = size;
this.response_format = response_format;
this.user = user;
}
}
[Serializable]
public class ImageEditRequest
{
public string image;
public string mask;
public string prompt;
public int n; // Number of images to generate.
public string size; // Must be 256x256, 512x512, or 1024x1024.
public string response_format;
public string user;
public ImageEditRequest(string image, string mask, string prompt, int n, string size, string response_format, string user)
{
this.image = image;
this.mask = mask;
this.prompt = prompt;
this.n = n;
this.size = size;
this.response_format = response_format;
this.user = user;
}
}
[Serializable]
public class ImagesResponse
{
public int created;
public List<ImageResult> data;
}
[Serializable]
public class ImageResult
{
public string url;
public string b64_json;
}
#endregion
#region Audio
public enum AudioResponseFormat { Json, Text, SRT, Verbose_Json, VTT }
[Serializable]
public class AudioTranscriptionResponse
{
public string text;
}
#endregion
#region Tool
[Serializable]
public class FunctionCall
{
public string name;
public string arguments;
}
[Serializable]
public class Tool
{
public string type;
public ToolFunction function;
public Tool(string type, ToolFunction function)
{
this.type = type;
this.function = function;
}
}
[Serializable]
public class ToolFunction
{
public string name;
public string description;
public Parameter parameters;
public ToolFunction(string name, string description, Parameter parameters)
{
this.name = name;
this.description = description;
this.parameters = parameters;
}
}
[System.Serializable]
public class Parameter
{
[JsonProperty("type")]
public string Type { get; set; } = DataTypes.OBJECT;
[JsonProperty("properties")]
public Dictionary<string, Property> Properties { get; set; } = new Dictionary<string, Property>();
[JsonProperty("required")]
public List<string> Required { get; set; } = new List<string>();
[JsonProperty("additionalProperties")]
public bool AdditionalProperties { get; set; } = false;
/// <summary>
/// Create parameter for the function call.
/// Use AddProperty() to add more properties.
/// Use DataTypes class to access data types available for propertyType
/// </summary>
public Parameter(string propertyName, string propertyType, string description)
{
Property property = new Property();
property.Description = description;
property.Type = propertyType;
Properties.Add(propertyName, property);
}
public Parameter() { }
/// <summary>
/// Add a Property to the parameter.
/// Use DataTypes class to access data types available for propertyType
/// </summary>
/// <param name="propertyName"></param>
/// <param name="propertyType"></param>
/// <param name="description"></param>
public void AddProperty(string propertyName, string propertyType, string description)
{
Property property = new Property();
property.Description = description;
property.Type = propertyType;
Properties.Add(propertyName, property);
}
}
[System.Serializable]
public class Property
{
[JsonProperty("type")]
public string Type { get; set; }
[JsonProperty("description")]
public string Description { get; set; }
}
[System.Serializable]
public class Message
{
public string role;
public string content;
public ToolCalls[] tool_calls;
}
[System.Serializable]
public class ToolCalls
{
public string id;
public string type;
public FunctionCall function;
}
public static class DataTypes
{
public static readonly string STRING = "string";
public static readonly string ARRAY = "array";
public static readonly string NUMBER = "number";
public static readonly string OBJECT = "object";
}
#endregion
}