forked from dotnet/dev-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIModels.cs
More file actions
311 lines (267 loc) · 9.53 KB
/
OpenAIModels.cs
File metadata and controls
311 lines (267 loc) · 9.53 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
// 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 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 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("total_tokens")]
public long TotalTokens { 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; }
}