Skip to content

Commit c8c1e83

Browse files
authored
[Chat Completions] Response payload definition #22 (backup) (#298)
1 parent 54ef75a commit c8c1e83

File tree

6 files changed

+2364
-1
lines changed

6 files changed

+2364
-1
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System.Runtime.Serialization;
2+
using System.Text.Json;
3+
using System.Text.Json.Serialization;
4+
5+
namespace AzureOpenAIProxy.ApiApp.Converters;
6+
7+
/// <summary>
8+
/// This represents the converter entity for <see cref="EnumMemberAttribute"/>.
9+
/// </summary>
10+
/// <typeparam name="T">The type of the enum to be converted.</typeparam>
11+
public class EnumMemberConverter<T> : JsonConverter<T> where T : Enum
12+
{
13+
/// <inheritdoc />
14+
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
15+
{
16+
var enumText = reader.GetString();
17+
18+
if (enumText == null)
19+
{
20+
throw new JsonException($"Unable to convert null to Enum \"{typeToConvert}\".");
21+
}
22+
23+
foreach (var field in typeToConvert.GetFields())
24+
{
25+
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) as EnumMemberAttribute;
26+
27+
if (attribute != null && attribute.Value == enumText)
28+
{
29+
var value = field.GetValue(null);
30+
if (value != null)
31+
{
32+
return (T)value;
33+
}
34+
}
35+
else if (field.Name == enumText)
36+
{
37+
var value = field.GetValue(null);
38+
if (value != null)
39+
{
40+
return (T)value;
41+
}
42+
}
43+
}
44+
45+
throw new JsonException($"Unable to convert \"{enumText}\" to Enum \"{typeToConvert}\".");
46+
}
47+
48+
/// <inheritdoc />
49+
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options)
50+
{
51+
var field = value.GetType().GetField(value.ToString());
52+
53+
if (field != null)
54+
{
55+
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) as EnumMemberAttribute;
56+
57+
if (attribute != null)
58+
{
59+
writer.WriteStringValue(attribute.Value);
60+
}
61+
else
62+
{
63+
writer.WriteStringValue(value.ToString());
64+
}
65+
}
66+
else
67+
{
68+
writer.WriteStringValue(value.ToString());
69+
}
70+
}
71+
72+
}

src/AzureOpenAIProxy.ApiApp/Endpoints/ProxyChatCompletionsEndpoint.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Text.Json;
22

33
using AzureOpenAIProxy.ApiApp.Attributes;
4+
using AzureOpenAIProxy.ApiApp.Models;
45
using AzureOpenAIProxy.ApiApp.Services;
56

67
using Microsoft.AspNetCore.Mvc;
@@ -54,7 +55,7 @@ public static RouteHandlerBuilder AddChatCompletions(this WebApplication app)
5455
})
5556
// TODO: Check both request/response payloads
5657
.Accepts<ChatCompletionOptions>(contentType: "application/json")
57-
.Produces<ChatCompletion>(statusCode: StatusCodes.Status200OK, contentType: "application/json")
58+
.Produces<CreateChatCompletionResponse>(statusCode: StatusCodes.Status200OK, contentType: "application/json")
5859
// TODO: Check both request/response payloads
5960
.Produces(statusCode: StatusCodes.Status401Unauthorized)
6061
.Produces<string>(statusCode: StatusCodes.Status500InternalServerError, contentType: "text/plain")

0 commit comments

Comments
 (0)