-
Notifications
You must be signed in to change notification settings - Fork 18
[Chat Completions] Response payload definition #22 (backup) #298
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
justinyoo
merged 13 commits into
aliencube:main
from
jhmin99:feature/22-response-payload-definition-backup
Sep 7, 2024
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ab5712c
Add EnumMemberConverter for custom Enum serialization
jhmin99 81e23ce
Define models and enums for CreateChatCompletionResponse
jhmin99 d7ad5e8
Add unit tests for EnumMemberConverter
jhmin99 8a9bbba
Add unit tests for CreateChatCompletionResponse serialization and des…
jhmin99 200fda4
Add schema validation tests for ChatCompletion and related models
jhmin99 fa88aeb
Update `ProxyChatCompletionsEndpoint` to produce `CreateChatCompletio…
jhmin99 6011d52
Added documentation comments to class and methods
jhmin99 ca09db1
Removed `.Produces<ChatCompletion>` line from the `AddChatCompletions…
jhmin99 ec24f06
Added documentation comments to classes, properties, and enums.
jhmin99 f1c469b
Merge branch 'aliencube:main' into feature/22-response-payload-defini…
jhmin99 ba014a9
Add newline between using statements and class declaration in EnumMem…
jhmin99 63efda5
Update summary comments with remark tag and add newlines
jhmin99 85c32e8
Refactor comments adding <summary>
jhmin99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
src/AzureOpenAIProxy.ApiApp/Converters/EnumMemberConverter.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Runtime.Serialization; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
|
||
namespace AzureOpenAIProxy.ApiApp.Converters; | ||
|
||
/// <summary> | ||
/// This represents the converter entity for <see cref="EnumMemberAttribute"/>. | ||
/// </summary> | ||
/// <typeparam name="T">The type of the enum to be converted.</typeparam> | ||
public class EnumMemberConverter<T> : JsonConverter<T> where T : Enum | ||
justinyoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/// <inheritdoc /> | ||
public override T Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) | ||
justinyoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var enumText = reader.GetString(); | ||
|
||
if (enumText == null) | ||
{ | ||
throw new JsonException($"Unable to convert null to Enum \"{typeToConvert}\"."); | ||
} | ||
|
||
foreach (var field in typeToConvert.GetFields()) | ||
{ | ||
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) as EnumMemberAttribute; | ||
|
||
if (attribute != null && attribute.Value == enumText) | ||
{ | ||
var value = field.GetValue(null); | ||
if (value != null) | ||
{ | ||
return (T)value; | ||
} | ||
} | ||
else if (field.Name == enumText) | ||
{ | ||
var value = field.GetValue(null); | ||
if (value != null) | ||
{ | ||
return (T)value; | ||
} | ||
} | ||
} | ||
|
||
throw new JsonException($"Unable to convert \"{enumText}\" to Enum \"{typeToConvert}\"."); | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions options) | ||
justinyoo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
var field = value.GetType().GetField(value.ToString()); | ||
|
||
if (field != null) | ||
{ | ||
var attribute = Attribute.GetCustomAttribute(field, typeof(EnumMemberAttribute)) as EnumMemberAttribute; | ||
|
||
if (attribute != null) | ||
{ | ||
writer.WriteStringValue(attribute.Value); | ||
} | ||
else | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
else | ||
{ | ||
writer.WriteStringValue(value.ToString()); | ||
} | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.