Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions codegen/nuget.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="generator" value="https://pkgs.dev.azure.com/azure-sdk/public/_packaging/azure-sdk-for-net/nuget/v3/index.json" />
</packageSources>
</configuration>
42 changes: 37 additions & 5 deletions src/Custom/Embeddings/OpenAIEmbedding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Buffers.Text;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text.Json;

namespace OpenAI.Embeddings;

Expand Down Expand Up @@ -107,13 +108,18 @@ internal OpenAIEmbedding(int index, ReadOnlyMemory<float> vector)
// CUSTOM: Implemented custom logic to transform from BinaryData to ReadOnlyMemory<float>.
private static ReadOnlyMemory<float> ConvertToVectorOfFloats(BinaryData binaryData)
{
ReadOnlySpan<byte> base64 = binaryData.ToMemory().Span;
ReadOnlySpan<byte> bytes = binaryData.ToMemory().Span;

// Remove quotes around base64 string.
if (base64.Length < 2 || base64[0] != (byte)'"' || base64[base64.Length - 1] != (byte)'"')
if (bytes.Length > 2 && bytes[0] == (byte)'"' && bytes[bytes.Length - 1] == (byte)'"')
{
ThrowInvalidData();
return ConvertFromBase64(bytes);
}
return ConvertFromJsonArray(binaryData);
}

private static ReadOnlyMemory<float> ConvertFromBase64(ReadOnlySpan<byte> base64)
{
base64 = base64.Slice(1, base64.Length - 2);

// Decode base64 string to bytes.
Expand Down Expand Up @@ -153,7 +159,33 @@ private static ReadOnlyMemory<float> ConvertToVectorOfFloats(BinaryData binaryDa
}
}

static void ThrowInvalidData() =>
throw new FormatException("The input is not a valid Base64 string of encoded floats.");
static void ThrowInvalidData()
=> throw new FormatException("The input is not a valid Base64 string of encoded floats.");
}

private static ReadOnlyMemory<float> ConvertFromJsonArray(BinaryData jsonArray)
{
using JsonDocument document = JsonDocument.Parse(jsonArray);
JsonElement array = document.RootElement;
if (array.ValueKind != JsonValueKind.Array)
{
throw new FormatException("The input is not a valid JSON array");
}

int arrayLength = array.GetArrayLength();
float[] vector = new float[arrayLength];
int index = 0;
try
{
foreach (JsonElement value in array.EnumerateArray())
{
vector[index++] = value.GetSingle();
}
return vector.AsMemory();
}
catch
{
throw new FormatException("The input is not a valid JSON array of float values");
}
}
}
31 changes: 31 additions & 0 deletions tests/Embeddings/EmbeddingsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using OpenAI.Tests.Utility;
using System;
using System.ClientModel;
using System.ClientModel.Primitives;
using System.Collections.Generic;
using System.Threading.Tasks;
using static OpenAI.Tests.TestHelpers;
Expand Down Expand Up @@ -205,4 +206,34 @@ public void SerializeEmbeddingCollection()
{
// TODO: Add this test.
}

[Test]
public void JsonArraySupport()
{
string json = """
{
"object":"list",
"data":[
{
"object":"embedding",
"embedding":[-0.011229509,0.107915245,-0.15163477]
}
]
}
""";

BinaryData binaryData = BinaryData.FromString(json);

OpenAIEmbeddingCollection embeddings = ModelReaderWriter.Read<OpenAIEmbeddingCollection>(binaryData);

Assert.That(embeddings, Is.Not.Null);
Assert.That(embeddings.Count, Is.EqualTo(1));
var embedding = embeddings[0];
Assert.That(embedding, Is.Not.Null);
ReadOnlySpan<float> vector = embedding.ToFloats().Span;
Assert.That(vector.Length, Is.EqualTo(3));
Assert.That(vector[0], Is.EqualTo(-0.011229509f));
Assert.That(vector[1], Is.EqualTo(0.107915245f));
Assert.That(vector[2], Is.EqualTo(-0.15163477f));
}
}