-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathOpenAIModels.cs
More file actions
487 lines (417 loc) · 15.9 KB
/
OpenAIModels.cs
File metadata and controls
487 lines (417 loc) · 15.9 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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using DevProxy.Abstractions.Utils;
using Microsoft.Extensions.Logging;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace DevProxy.Abstractions.LanguageModel;
public class OpenAIRequest
{
[JsonPropertyName("frequency_penalty")]
public long? FrequencyPenalty { get; set; }
[JsonPropertyName("max_tokens")]
public long? MaxTokens { get; set; }
public string Model { get; set; } = string.Empty;
[JsonPropertyName("presence_penalty")]
public long? PresencePenalty { get; set; }
public object? Stop { get; set; }
public bool? Stream { get; set; }
public double? Temperature { get; set; }
[JsonPropertyName("top_p")]
public double? TopP { get; set; }
public static bool TryGetOpenAIRequest(string content, ILogger logger, out OpenAIRequest? request)
{
logger.LogTrace("{Method} called", nameof(TryGetOpenAIRequest));
request = null;
if (string.IsNullOrEmpty(content))
{
logger.LogDebug("Request content is empty or null");
return false;
}
try
{
logger.LogDebug("Checking if the request is an OpenAI request...");
var rawRequest = JsonSerializer.Deserialize<JsonElement>(content, ProxyUtils.JsonSerializerOptions);
// Responses API request (check first as it's the recommended API)
if (rawRequest.TryGetProperty("input", out _) &&
rawRequest.TryGetProperty("modalities", out _))
{
logger.LogDebug("Request is a Responses API request");
request = JsonSerializer.Deserialize<OpenAIResponsesRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Check for completion request (has "prompt", but not specific to image)
if (rawRequest.TryGetProperty("prompt", out _) &&
!rawRequest.TryGetProperty("size", out _) &&
!rawRequest.TryGetProperty("n", out _))
{
logger.LogDebug("Request is a completion request");
request = JsonSerializer.Deserialize<OpenAICompletionRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Chat completion request
if (rawRequest.TryGetProperty("messages", out _))
{
logger.LogDebug("Request is a chat completion request");
request = JsonSerializer.Deserialize<OpenAIChatCompletionRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Embedding request
if (rawRequest.TryGetProperty("input", out _) &&
rawRequest.TryGetProperty("model", out _) &&
!rawRequest.TryGetProperty("voice", out _) &&
!rawRequest.TryGetProperty("modalities", out _))
{
logger.LogDebug("Request is an embedding request");
request = JsonSerializer.Deserialize<OpenAIEmbeddingRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Image generation request
if (rawRequest.TryGetProperty("prompt", out _) &&
(rawRequest.TryGetProperty("size", out _) || rawRequest.TryGetProperty("n", out _)))
{
logger.LogDebug("Request is an image generation request");
request = JsonSerializer.Deserialize<OpenAIImageRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Audio transcription request
if (rawRequest.TryGetProperty("file", out _))
{
logger.LogDebug("Request is an audio transcription request");
request = JsonSerializer.Deserialize<OpenAIAudioRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Audio speech synthesis request
if (rawRequest.TryGetProperty("input", out _) && rawRequest.TryGetProperty("voice", out _))
{
logger.LogDebug("Request is an audio speech synthesis request");
request = JsonSerializer.Deserialize<OpenAIAudioSpeechRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
// Fine-tuning request
if (rawRequest.TryGetProperty("training_file", out _))
{
logger.LogDebug("Request is a fine-tuning request");
request = JsonSerializer.Deserialize<OpenAIFineTuneRequest>(content, ProxyUtils.JsonSerializerOptions);
return true;
}
logger.LogDebug("Request is not an OpenAI request.");
return false;
}
catch (JsonException ex)
{
logger.LogDebug(ex, "Failed to deserialize OpenAI request.");
return false;
}
}
}
public class OpenAIResponse : ILanguageModelCompletionResponse
{
public long Created { get; set; }
public OpenAIError? Error { get; set; }
public string Id { get; set; } = string.Empty;
public string Model { get; set; } = string.Empty;
#pragma warning disable CA1720
public string Object { get; set; } = "text_completion";
#pragma warning restore CA1720
[JsonPropertyName("prompt_filter_results")]
public IEnumerable<OpenAIResponsePromptFilterResult> PromptFilterResults { get; set; } = [];
public OpenAIResponseUsage Usage { get; set; } = new();
public string? RequestUrl { get; set; }
public string? ErrorMessage => Error?.Message;
public virtual string? Response { get; }
public OpenAIResponse ConvertToOpenAIResponse() => this;
}
public class OpenAIResponse<TChoice> : OpenAIResponse
{
public IEnumerable<TChoice>? Choices { get; set; }
}
public abstract class OpenAIResponseChoice
{
[JsonPropertyName("content_filter_results")]
#pragma warning disable CA2227
public Dictionary<string, OpenAIResponseContentFilterResult> ContentFilterResults { get; set; } = [];
#pragma warning restore CA2227
[JsonPropertyName("finish_reason")]
public string FinishReason { get; set; } = "stop";
public long Index { get; set; }
[JsonPropertyName("logprobs")]
public int? LogProbabilities { get; set; }
}
public class OpenAICompletionRequest : OpenAIRequest
{
public string Prompt { get; set; } = string.Empty;
}
public class OpenAIChatCompletionRequest : OpenAIRequest
{
public IEnumerable<OpenAIChatCompletionMessage> Messages { get; set; } = [];
}
public class OpenAIError
{
public string? Code { get; set; }
public string? Message { get; set; }
}
public class OpenAIResponseUsage
{
[JsonPropertyName("completion_tokens")]
public long CompletionTokens { get; set; }
[JsonPropertyName("prompt_tokens")]
public long PromptTokens { get; set; }
[JsonPropertyName("prompt_tokens_details")]
public PromptTokenDetails? PromptTokensDetails { get; set; }
[JsonPropertyName("total_tokens")]
public long TotalTokens { get; set; }
}
public class PromptTokenDetails
{
[JsonPropertyName("cached_tokens")]
public long CachedTokens { get; set; }
}
public class OpenAIResponsePromptFilterResult
{
[JsonPropertyName("content_filter_results")]
#pragma warning disable CA2227
public Dictionary<string, OpenAIResponseContentFilterResult> ContentFilterResults { get; set; } = [];
#pragma warning restore CA2227
[JsonPropertyName("prompt_index")]
public long PromptIndex { get; set; }
}
public class OpenAIResponseContentFilterResult
{
public bool Filtered { get; set; }
public string Severity { get; set; } = "safe";
}
public class OpenAICompletionResponse : OpenAIResponse<OpenAICompletionResponseChoice>
{
public override string? Response => Choices is not null && Choices.Any() ? Choices.Last().Text : null;
}
public class OpenAICompletionResponseChoice : OpenAIResponseChoice
{
public string Text { get; set; } = string.Empty;
}
#region content parts
public abstract class OpenAIContentPart
{
public string? Type { get; set; }
}
public class OpenAITextContentPart : OpenAIContentPart
{
public string? Text { get; set; }
}
public class OpenAIImageContentPartUrl
{
public string? Detail { get; set; } = "auto";
public string? Url { get; set; }
}
public class OpenAIImageContentPart : OpenAIContentPart
{
[JsonPropertyName("image_url")]
public OpenAIImageContentPartUrl? Url { get; set; }
}
public class OpenAIAudioContentPartInputAudio
{
public string? Data { get; set; }
public string? Format { get; set; }
}
public class OpenAIAudioContentPart : OpenAIContentPart
{
[JsonPropertyName("input_audio")]
public OpenAIAudioContentPartInputAudio? InputAudio { get; set; }
}
public class OpenAIFileContentPartFile
{
[JsonPropertyName("file_data")]
public string? Data { get; set; }
[JsonPropertyName("file_id")]
public string? Id { get; set; }
[JsonPropertyName("filename")]
public string? Name { get; set; }
}
public class OpenAIFileContentPart : OpenAIContentPart
{
public OpenAIFileContentPartFile? File { get; set; }
}
#endregion
public class OpenAIChatCompletionMessage : ILanguageModelChatCompletionMessage
{
[JsonConverter(typeof(OpenAIContentPartJsonConverter))]
public object Content { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
public override bool Equals(object? obj)
{
if (obj is null || GetType() != obj.GetType())
{
return false;
}
var m = (OpenAIChatCompletionMessage)obj;
return Content == m.Content && Role == m.Role;
}
public override int GetHashCode()
{
return HashCode.Combine(Content, Role);
}
}
public class OpenAIChatCompletionResponse : OpenAIResponse<OpenAIChatCompletionResponseChoice>
{
public override string? Response => Choices is not null && Choices.Any() ?
Choices.Last().Message.Content : null;
}
public class OpenAIChatCompletionResponseChoice : OpenAIResponseChoice
{
public OpenAIChatCompletionResponseChoiceMessage Message { get; set; } = new();
}
public class OpenAIChatCompletionResponseChoiceMessage
{
public string Content { get; set; } = string.Empty;
public string Role { get; set; } = string.Empty;
}
public class OpenAIAudioRequest : OpenAIRequest
{
public string File { get; set; } = string.Empty;
[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }
public string? Prompt { get; set; }
public string? Language { get; set; }
}
public class OpenAIAudioSpeechRequest : OpenAIRequest
{
public string Input { get; set; } = string.Empty;
public string Voice { get; set; } = string.Empty;
[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }
public double? Speed { get; set; }
}
public class OpenAIAudioTranscriptionResponse : OpenAIResponse
{
public string Text { get; set; } = string.Empty;
public override string? Response => Text;
}
public class OpenAIEmbeddingRequest : OpenAIRequest
{
public string? Input { get; set; }
[JsonPropertyName("encoding_format")]
public string? EncodingFormat { get; set; }
public int? Dimensions { get; set; }
}
public class OpenAIEmbeddingResponse : OpenAIResponse
{
public IEnumerable<OpenAIEmbeddingData>? Data { get; set; }
public override string? Response => null; // Embeddings don't have a text response
}
public class OpenAIEmbeddingData
{
public IEnumerable<float>? Embedding { get; set; }
public int Index { get; set; }
#pragma warning disable CA1720
public string? Object { get; set; }
#pragma warning restore CA1720
}
public class OpenAIFineTuneRequest : OpenAIRequest
{
[JsonPropertyName("training_file")]
public string TrainingFile { get; set; } = string.Empty;
[JsonPropertyName("validation_file")]
public string? ValidationFile { get; set; }
public int? Epochs { get; set; }
[JsonPropertyName("batch_size")]
public int? BatchSize { get; set; }
[JsonPropertyName("learning_rate_multiplier")]
public double? LearningRateMultiplier { get; set; }
public string? Suffix { get; set; }
}
public class OpenAIFineTuneResponse : OpenAIResponse
{
[JsonPropertyName("fine_tuned_model")]
public string? FineTunedModel { get; set; }
public string Status { get; set; } = string.Empty;
public string? Organization { get; set; }
public long CreatedAt { get; set; }
public long UpdatedAt { get; set; }
[JsonPropertyName("training_file")]
public string TrainingFile { get; set; } = string.Empty;
[JsonPropertyName("validation_file")]
public string? ValidationFile { get; set; }
[JsonPropertyName("result_files")]
public IEnumerable<object>? ResultFiles { get; set; }
public override string? Response => FineTunedModel;
}
public class OpenAIImageRequest : OpenAIRequest
{
public string Prompt { get; set; } = string.Empty;
public int? N { get; set; }
public string? Size { get; set; }
[JsonPropertyName("response_format")]
public string? ResponseFormat { get; set; }
public string? User { get; set; }
public string? Quality { get; set; }
public string? Style { get; set; }
}
public class OpenAIImageResponse : OpenAIResponse
{
public IEnumerable<OpenAIImageData>? Data { get; set; }
public override string? Response => null; // Image responses don't have a text response
}
public class OpenAIImageData
{
public string? Url { get; set; }
[JsonPropertyName("b64_json")]
public string? Base64Json { get; set; }
[JsonPropertyName("revised_prompt")]
public string? RevisedPrompt { get; set; }
}
#region Responses API
public class OpenAIResponsesRequest : OpenAIRequest
{
public object? Input { get; set; }
public IEnumerable<string>? Modalities { get; set; }
public string? Instructions { get; set; }
public bool? Store { get; set; }
[JsonPropertyName("previous_response_id")]
public string? PreviousResponseId { get; set; }
public object? Tools { get; set; }
[JsonPropertyName("max_output_tokens")]
public long? MaxOutputTokens { get; set; }
}
public class OpenAIResponsesResponse : OpenAIResponse
{
public IEnumerable<OpenAIResponsesOutputItem>? Output { get; set; }
[JsonPropertyName("created_at")]
public long CreatedAt { get; set; }
public string? Status { get; set; }
public override string? Response
{
get
{
if (Output is null || !Output.Any())
{
return null;
}
// Find the last message-type output item with text content
var lastMessage = Output
.Where(item => item.Type == "message")
.LastOrDefault();
if (lastMessage?.Content is null)
{
return null;
}
// Extract text from content array
var textContent = lastMessage.Content
.Where(c => c.Type == "output_text")
.LastOrDefault();
return textContent?.Text;
}
}
}
public class OpenAIResponsesOutputItem
{
public string? Type { get; set; }
public string? Role { get; set; }
public IEnumerable<OpenAIResponsesContentPart>? Content { get; set; }
}
public class OpenAIResponsesContentPart
{
public string? Type { get; set; }
public string? Text { get; set; }
}
#endregion