-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Azure OpenAI double ?, and JSON test issues (#44457)
* Fix double ? issues with generated URIs * Test fixes - Fixes JSON issues due to moving to older JSON version - Fixes vector store test that was subject to race condition in status
- Loading branch information
1 parent
410dc90
commit a4b9ab1
Showing
4 changed files
with
235 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,185 @@ | ||
using System; | ||
using System.Buffers; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
using System.Text.Json; | ||
|
||
#nullable enable | ||
|
||
namespace Azure.AI.OpenAI.Tests.Utils; | ||
|
||
/// <summary> | ||
/// A helper class to make working with older versions of System.Text.Json simpler | ||
/// </summary> | ||
internal static class JsonHelpers | ||
{ | ||
// TODO FIXME once we update to newer versions of System.Text.JSon we should switch to using | ||
// JsonNamingPolicy.SnakeCaseLower | ||
public static JsonNamingPolicy SnakeCaseLower { get; } = | ||
new SnakeCaseNamingPolicy(); | ||
|
||
// TODO FIXME once we move to newer versions of System.Text.Json we can directly call | ||
// JsonSerializer.Serialize(...) with a stream | ||
public static void Serialize<T>(Stream stream, T value, JsonSerializerOptions? options = null) | ||
{ | ||
#if NETFRAMEWORK | ||
using Utf8JsonWriter writer = new(stream, new JsonWriterOptions() | ||
{ | ||
Encoder = options?.Encoder, | ||
Indented = options?.WriteIndented == true, | ||
SkipValidation = false | ||
}); | ||
|
||
JsonSerializer.Serialize(writer, value, options); | ||
#else | ||
JsonSerializer.Serialize(stream, value, options); | ||
#endif | ||
} | ||
|
||
#if NET6_0_OR_GREATER | ||
// .Net 6 and newer already have the extension method we need defined in JsonsSerializer | ||
#else | ||
// TODO FIXME once we move to newer versions of System.Text.Json we can directly use the | ||
// JsonSerializer extension method for elements | ||
public static T? Deserialize<T>(this JsonElement element, JsonSerializerOptions? options = null) | ||
{ | ||
using MemoryStream stream = new(); | ||
using Utf8JsonWriter writer = new(stream, new() | ||
{ | ||
Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping, | ||
Indented = false, | ||
SkipValidation = true | ||
}); | ||
element.WriteTo(writer); | ||
writer.Flush(); | ||
|
||
stream.Seek(0, SeekOrigin.Begin); | ||
if (((ulong)stream.Length & 0xffffffff00000000) != 0ul) | ||
{ | ||
throw new ArgumentOutOfRangeException("JsonElement is too large"); | ||
} | ||
|
||
ReadOnlySpan<byte> span = new(stream.GetBuffer(), 0, (int)stream.Length); | ||
return JsonSerializer.Deserialize<T>(span, options); | ||
} | ||
#endif | ||
|
||
// Ported over from the source code for newer versions of System.Text.Json | ||
internal class SnakeCaseNamingPolicy : JsonNamingPolicy | ||
{ | ||
private enum SeparatorState | ||
{ | ||
NotStarted, | ||
UppercaseLetter, | ||
LowercaseLetterOrDigit, | ||
SpaceSeparator | ||
} | ||
|
||
public override string ConvertName(string name) | ||
{ | ||
if (string.IsNullOrEmpty(name)) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
return ConvertName('_', name.AsSpan()); | ||
} | ||
|
||
internal static string ConvertName(char separator, ReadOnlySpan<char> chars) | ||
{ | ||
char[]? rentedBuffer = null; | ||
|
||
int num = (int)(1.2 * chars.Length); | ||
Span<char> output = num > 128 | ||
? (rentedBuffer = ArrayPool<char>.Shared.Rent(num))! | ||
: stackalloc char[128]; | ||
|
||
SeparatorState separatorState = SeparatorState.NotStarted; | ||
int charsWritten = 0; | ||
|
||
for (int i = 0; i < chars.Length; i++) | ||
{ | ||
char c = chars[i]; | ||
UnicodeCategory unicodeCategory = char.GetUnicodeCategory(c); | ||
switch (unicodeCategory) | ||
{ | ||
case UnicodeCategory.UppercaseLetter: | ||
switch (separatorState) | ||
{ | ||
case SeparatorState.LowercaseLetterOrDigit: | ||
case SeparatorState.SpaceSeparator: | ||
WriteChar(separator, ref output); | ||
break; | ||
case SeparatorState.UppercaseLetter: | ||
if (i + 1 < chars.Length && char.IsLower(chars[i + 1])) | ||
{ | ||
WriteChar(separator, ref output); | ||
} | ||
break; | ||
} | ||
|
||
c = char.ToLowerInvariant(c); | ||
WriteChar(c, ref output); | ||
separatorState = SeparatorState.UppercaseLetter; | ||
break; | ||
|
||
case UnicodeCategory.LowercaseLetter: | ||
case UnicodeCategory.DecimalDigitNumber: | ||
if (separatorState == SeparatorState.SpaceSeparator) | ||
{ | ||
WriteChar(separator, ref output); | ||
} | ||
|
||
WriteChar(c, ref output); | ||
separatorState = SeparatorState.LowercaseLetterOrDigit; | ||
break; | ||
|
||
case UnicodeCategory.SpaceSeparator: | ||
if (separatorState != 0) | ||
{ | ||
separatorState = SeparatorState.SpaceSeparator; | ||
} | ||
break; | ||
|
||
default: | ||
WriteChar(c, ref output); | ||
separatorState = SeparatorState.NotStarted; | ||
break; | ||
} | ||
} | ||
|
||
string result = output.Slice(0, charsWritten).ToString(); | ||
if (rentedBuffer != null) | ||
{ | ||
output.Slice(0, charsWritten).Clear(); | ||
ArrayPool<char>.Shared.Return(rentedBuffer); | ||
} | ||
return result; | ||
|
||
void ExpandBuffer(ref Span<char> destination) | ||
{ | ||
int minimumLength = checked(destination.Length * 2); | ||
char[] array = ArrayPool<char>.Shared.Rent(minimumLength); | ||
destination.CopyTo(array); | ||
if (rentedBuffer != null) | ||
{ | ||
destination.Slice(0, charsWritten).Clear(); | ||
ArrayPool<char>.Shared.Return(rentedBuffer); | ||
} | ||
rentedBuffer = array; | ||
destination = rentedBuffer; | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
void WriteChar(char value, ref Span<char> destination) | ||
{ | ||
if (charsWritten == destination.Length) | ||
{ | ||
ExpandBuffer(ref destination); | ||
} | ||
destination[charsWritten++] = value; | ||
} | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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