forked from srcnalt/OpenAI-Unity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataTypes.cs
More file actions
425 lines (373 loc) · 12.7 KB
/
DataTypes.cs
File metadata and controls
425 lines (373 loc) · 12.7 KB
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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
using System.Collections.Generic;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace OpenAI
{
#region Common Data Types
public struct Choice
{
public string Text { get; set; }
public int? Index { get; set; }
public int? Logprobs { get; set; }
public string FinishReason { get; set; }
}
public struct Usage
{
public string PromptTokens { get; set; }
public string CompletionTokens { get; set; }
public string TotalTokens { get; set; }
}
public class OpenAIFile
{
public string Prompt { get; set; }
public object Completion { get; set; }
public string Id { get; set; }
public string Object { get; set; }
public long Bytes { get; set; }
public long CreatedAt { get; set; }
public string Filename { get; set; }
public string Purpose { get; set; }
public object StatusDetails { get; set; }
public string Status { get; set; }
}
public class OpenAIFileResponse : OpenAIFile, IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
}
public class ApiError
{
public string Message;
public string Type;
public object Param;
public object Code;
}
public struct Auth
{
[JsonRequired]
public string ApiKey { get; set; }
public string Organization { get; set; }
}
#endregion
#region Models API Data Types
public struct ListModelsResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Object { get; set; }
public List<OpenAIModel> Data { get; set; }
}
public class OpenAIModel
{
public string Id { get; set; }
public string Object { get; set; }
public string OwnedBy { get; set; }
public long Created { get; set; }
public string Root { get; set; }
public string Parent { get; set; }
public List<Dictionary<string, object>> Permission { get; set; }
}
public class OpenAIModelResponse : OpenAIModel, IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
}
#endregion
#region Chat API Data Types
public enum ResponseFormat
{
Text,
JsonObject
}
public sealed class CreateChatCompletionRequest
{
public string Model { get; set; }
public List<ChatMessage> Messages { get; set; }
public float? Temperature { get; set; } = 1;
public int N { get; set; } = 1;
public bool Stream { get; set; } = false;
public string Stop { get; set; }
public int? MaxTokens { get; set; }
public float? PresencePenalty { get; set; } = 0;
public float? FrequencyPenalty { get; set; } = 0;
public Dictionary<string, string> LogitBias { get; set; }
public string User { get; set; }
public string SystemFingerprint { get; set; }
[JsonConverter(typeof(ResponseFormatJsonConverter))]
public ResponseFormat? ResponseFormat { get; set; }
}
public class ResponseFormatJsonConverter : JsonConverter<ResponseFormat>
{
public override void WriteJson(JsonWriter writer, ResponseFormat value, JsonSerializer serializer)
{
if (value == ResponseFormat.JsonObject)
{
writer.WriteStartObject();
writer.WritePropertyName("type");
writer.WriteValue("json_object");
writer.WriteEndObject();
} else
{
writer.WriteNull();
}
}
public override ResponseFormat ReadJson(JsonReader reader, System.Type objectType, ResponseFormat existingValue, bool hasExistingValue, JsonSerializer serializer)
{
if (reader.TokenType == JsonToken.StartObject)
{
JObject obj = JObject.Load(reader);
if (obj.TryGetValue("type", out JToken typeToken) && typeToken.ToString() == "json_object")
{
return ResponseFormat.JsonObject;
}
}
return ResponseFormat.Text;
}
}
public struct CreateChatCompletionResponse : IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Model { get; set; }
public string Id { get; set; }
public string Object { get; set; }
public long Created { get; set; }
public List<ChatChoice> Choices { get; set; }
public Usage Usage { get; set; }
public string SystemFingerprint { get; set; }
}
public struct ChatChoice
{
public ChatMessage Message { get; set; }
public ChatMessage Delta { get; set; }
public int? Index { get; set; }
public string FinishReason { get; set; }
public string Logprobs { get; set; }
}
public struct ChatMessage
{
public string Role { get; set; }
public string Content { get; set; }
}
#endregion
#region Audio Transcriptions Data Types
public struct FileData
{
public byte[] Data;
public string Name;
}
public class CreateAudioRequestBase
{
public string File { get; set; }
public FileData FileData { get; set; }
public string Model { get; set; }
public string Prompt { get; set; }
public string ResponseFormat { get; set; } = AudioResponseFormat.Json;
public float? Temperature { get; set; } = 0;
}
public class CreateAudioTranscriptionsRequest: CreateAudioRequestBase
{
public string Language { get; set; }
}
public class CreateAudioTranslationRequest: CreateAudioRequestBase { }
public struct CreateAudioResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Text { get; set; }
}
#endregion
#region Images API Data Types
public class CreateImageRequestBase
{
public int? N { get; set; } = 1;
public string Size { get; set; } = ImageSize.Size1024;
public string ResponseFormat { get; set; } = ImageResponseFormat.Url;
public string User { get; set; }
}
public sealed class CreateImageRequest: CreateImageRequestBase
{
public string Prompt { get; set; }
}
public sealed class CreateImageEditRequest: CreateImageRequestBase
{
public string Image { get; set; }
public string Mask { get; set; }
public string Prompt { get; set; }
}
public sealed class CreateImageVariationRequest: CreateImageRequestBase
{
public string Image { get; set; }
}
public struct CreateImageResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public long Created { get; set; }
public List<ImageData> Data { get; set; }
}
public struct ImageData
{
public string Url { get; set; }
public string B64Json { get; set; }
}
#endregion
#region Embeddins API Data Types
public struct CreateEmbeddingsRequest
{
public string Model { get; set; }
public string Input { get; set; }
public string User { get; set; }
}
public struct CreateEmbeddingsResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Object { get; set; }
public List<EmbeddingData> Data;
public string Model { get; set; }
public Usage Usage { get; set; }
}
public struct EmbeddingData
{
public string Object { get; set; }
public List<float> Embedding { get; set; }
public int Index { get; set; }
}
#endregion
#region Files API Data Types
public struct ListFilesResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Object { get; set; }
public List<OpenAIFile> Data { get; set; }
public bool HasMore { get; set; }
}
public struct DeleteResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Id { get; set; }
public string Object { get; set; }
public bool Deleted { get; set; }
}
public struct CreateFileRequest
{
public string File { get; set; }
public string Purpose { get; set; }
}
#endregion
#region FineTunes API Data Types
public class CreateFineTuneRequest
{
public string TrainingFile { get; set; }
public string ValidationFile { get; set; }
public string Model { get; set; }
public int NEpochs { get; set; } = 4;
public int? BatchSize { get; set; } = null;
public float? LearningRateMultiplier { get; set; } = null;
public float PromptLossWeight { get; set; } = 0.01f;
public bool ComputeClassificationMetrics { get; set; } = false;
public int? ClassificationNClasses { get; set; } = null;
public string ClassificationPositiveClass { get; set; }
public List<float> ClassificationBetas { get; set; }
public string Suffix { get; set; }
}
public struct ListFineTunesResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Object { get; set; }
public List<FineTune> Data { get; set; }
public object NextStartingAfter { get; set; }
}
public struct ListFineTuneEventsResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Object { get; set; }
public List<FineTuneEvent> Data { get; set; }
}
public class FineTune
{
public string Id { get; set; }
public string Object { get; set; }
public long CreatedAt { get; set; }
public long UpdatedAt { get; set; }
public string Model { get; set; }
public string FineTunedModel { get; set; }
public string OrganizationId { get; set; }
public string Status { get; set; }
public Dictionary<string, object> Hyperparams { get; set; }
public List<OpenAIFile> TrainingFiles { get; set; }
public List<OpenAIFile> ValidationFiles { get; set; }
public List<OpenAIFile> ResultFiles { get; set; }
public List<FineTuneEvent> Events { get; set; }
}
public class FineTuneResponse : FineTune, IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
}
public struct FineTuneEvent
{
public string Object { get; set; }
public long CreatedAt { get; set; }
public string Level { get; set; }
public string Message { get; set; }
}
#endregion
#region Moderations API Data Types
public class CreateModerationRequest
{
public string Input { get; set; }
public string Model { get; set; } = ModerationModel.Latest;
}
public struct CreateModerationResponse: IResponse
{
public ApiError Error { get; set; }
public string Warning { get; set; }
public string Id { get; set; }
public string Model { get; set; }
public List<ModerationResult> Results { get; set; }
}
public struct ModerationResult
{
public bool Flagged { get; set; }
public Dictionary<string, bool> Categories { get; set; }
public Dictionary<string, float> CategoryScores { get; set; }
}
#endregion
#region Static String Types
public static class ContentType
{
public const string MultipartFormData = "multipart/form-data";
public const string ApplicationJson = "application/json";
}
public static class ImageSize
{
public const string Size256 = "256x256";
public const string Size512 = "512x512";
public const string Size1024 = "1024x1024";
}
public static class ImageResponseFormat
{
public const string Url = "url";
public const string Base64Json = "b64_json";
}
public static class AudioResponseFormat
{
public const string Json = "json";
public const string Text = "text";
public const string Srt = "srt";
public const string VerboseJson = "verbose_json";
public const string Vtt = "vtt";
}
public static class ModerationModel
{
public const string Stable = "text-moderation-stable";
public const string Latest = "text-moderation-latest";
}
#endregion
}