openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Embeddings/EmbeddingsTests.cs
208lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Embeddings; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.Collections.Generic; |
| 7 | using System.Threading.Tasks; |
| 8 | using static OpenAI.Tests.TestHelpers; |
| 9 | |
| 10 | namespace OpenAI.Tests.Embeddings; |
| 11 | |
| 12 | [TestFixture(true)] |
| 13 | [TestFixture(false)] |
| 14 | [Parallelizable(ParallelScope.All)] |
| 15 | [Category("Embeddings")] |
| 16 | public class EmbeddingsTests : SyncAsyncTestBase |
| 17 | { |
| 18 | private static EmbeddingClient GetTestClient() => GetTestClient<EmbeddingClient>(TestScenario.Embeddings); |
| 19 | |
| 20 | public EmbeddingsTests(bool isAsync) : base(isAsync) |
| 21 | { |
| 22 | } |
| 23 | |
| 24 | public enum EmbeddingsInputKind |
| 25 | { |
| 26 | UsingStrings, |
| 27 | UsingIntegers, |
| 28 | } |
| 29 | |
| 30 | [Test] |
| 31 | public async Task GenerateSingleEmbedding() |
| 32 | { |
| 33 | EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 34 | |
| 35 | string input = "Hello, world!"; |
| 36 | |
| 37 | OpenAIEmbedding embedding = IsAsync |
| 38 | ? await client.GenerateEmbeddingAsync(input) |
| 39 | : client.GenerateEmbedding(input); |
| 40 | Assert.That(embedding, Is.Not.Null); |
| 41 | Assert.That(embedding.Index, Is.EqualTo(0)); |
| 42 | |
| 43 | ReadOnlyMemory<float> vector = embedding.ToFloats(); |
| 44 | Assert.That(vector, Is.Not.Null); |
| 45 | Assert.That(vector.Span.Length, Is.EqualTo(1536)); |
| 46 | |
| 47 | float[] array = vector.ToArray(); |
| 48 | Assert.That(array.Length, Is.EqualTo(1536)); |
| 49 | } |
| 50 | |
| 51 | [Test] |
| 52 | [TestCase(EmbeddingsInputKind.UsingStrings)] |
| 53 | [TestCase(EmbeddingsInputKind.UsingIntegers)] |
| 54 | public async Task GenerateMultipleEmbeddings(EmbeddingsInputKind embeddingsInputKind) |
| 55 | { |
| 56 | EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 57 | |
| 58 | const int Dimensions = 456; |
| 59 | |
| 60 | EmbeddingGenerationOptions options = new() |
| 61 | { |
| 62 | Dimensions = Dimensions, |
| 63 | }; |
| 64 | |
| 65 | OpenAIEmbeddingCollection embeddings = null; |
| 66 | |
| 67 | if (embeddingsInputKind == EmbeddingsInputKind.UsingStrings) |
| 68 | { |
| 69 | List<string> prompts = |
| 70 | [ |
| 71 | "Hello, world!", |
| 72 | "This is a test.", |
| 73 | "Goodbye!" |
| 74 | ]; |
| 75 | |
| 76 | embeddings = IsAsync |
| 77 | ? await client.GenerateEmbeddingsAsync(prompts, options) |
| 78 | : client.GenerateEmbeddings(prompts, options); |
| 79 | } |
| 80 | else if (embeddingsInputKind == EmbeddingsInputKind.UsingIntegers) |
| 81 | { |
| 82 | List<ReadOnlyMemory<int>> prompts = |
| 83 | [ |
| 84 | new[] { 104, 101, 108, 108, 111 }, |
| 85 | new[] { 119, 111, 114, 108, 100 }, |
| 86 | new[] { 84, 69, 83, 84 } |
| 87 | ]; |
| 88 | |
| 89 | embeddings = IsAsync |
| 90 | ? await client.GenerateEmbeddingsAsync(prompts, options) |
| 91 | : client.GenerateEmbeddings(prompts, options); |
| 92 | } |
| 93 | |
| 94 | Assert.That(embeddings, Is.Not.Null); |
| 95 | Assert.That(embeddings.Count, Is.EqualTo(3)); |
| 96 | Assert.That(embeddings.Model, Is.EqualTo("text-embedding-3-small")); |
| 97 | Assert.That(embeddings.Usage.InputTokenCount, Is.GreaterThan(0)); |
| 98 | Assert.That(embeddings.Usage.TotalTokenCount, Is.GreaterThan(0)); |
| 99 | |
| 100 | for (int i = 0; i < embeddings.Count; i++) |
| 101 | { |
| 102 | Assert.That(embeddings[i].Index, Is.EqualTo(i)); |
| 103 | |
| 104 | ReadOnlyMemory<float> vector = embeddings[i].ToFloats(); |
| 105 | Assert.That(vector, Is.Not.Null); |
| 106 | Assert.That(vector.Span.Length, Is.EqualTo(Dimensions)); |
| 107 | |
| 108 | float[] array = vector.ToArray(); |
| 109 | Assert.That(array.Length, Is.EqualTo(Dimensions)); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | [Test] |
| 114 | public async Task BadOptions() |
| 115 | { |
| 116 | EmbeddingClient client = GetTestClient(); |
| 117 | |
| 118 | EmbeddingGenerationOptions options = new() |
| 119 | { |
| 120 | Dimensions = -42, |
| 121 | }; |
| 122 | |
| 123 | Exception caughtException = null; |
| 124 | |
| 125 | try |
| 126 | { |
| 127 | _ = IsAsync |
| 128 | ? await client.GenerateEmbeddingAsync("foo", options) |
| 129 | : client.GenerateEmbedding("foo", options); |
| 130 | } |
| 131 | catch (Exception ex) |
| 132 | { |
| 133 | caughtException = ex; |
| 134 | } |
| 135 | |
| 136 | Assert.That(caughtException, Is.InstanceOf<ClientResultException>()); |
| 137 | Assert.That(caughtException.Message, Contains.Substring("dimensions")); |
| 138 | } |
| 139 | |
| 140 | [Test] |
| 141 | [TestCase(EmbeddingsInputKind.UsingStrings)] |
| 142 | [TestCase(EmbeddingsInputKind.UsingIntegers)] |
| 143 | public async Task GenerateMultipleEmbeddingsWithBadOptions(EmbeddingsInputKind embeddingsInputKind) |
| 144 | { |
| 145 | EmbeddingClient client = GetTestClient(); |
| 146 | |
| 147 | EmbeddingGenerationOptions options = new() |
| 148 | { |
| 149 | Dimensions = -42, |
| 150 | }; |
| 151 | |
| 152 | Exception caughtException = null; |
| 153 | |
| 154 | try |
| 155 | { |
| 156 | if (embeddingsInputKind == EmbeddingsInputKind.UsingStrings) |
| 157 | { |
| 158 | _ = IsAsync |
| 159 | ? await client.GenerateEmbeddingsAsync(["prompt"], options) |
| 160 | : client.GenerateEmbeddings(["prompt"], options); |
| 161 | } |
| 162 | else if (embeddingsInputKind == EmbeddingsInputKind.UsingIntegers) |
| 163 | { |
| 164 | _ = IsAsync |
| 165 | ? await client.GenerateEmbeddingsAsync([new[] { 1 }], options) |
| 166 | : client.GenerateEmbeddings([new[] { 1 }], options); |
| 167 | } |
| 168 | } |
| 169 | catch (Exception ex) |
| 170 | { |
| 171 | caughtException = ex; |
| 172 | } |
| 173 | |
| 174 | Assert.That(caughtException, Is.InstanceOf<ClientResultException>()); |
| 175 | Assert.That(caughtException.Message, Contains.Substring("dimensions")); |
| 176 | } |
| 177 | |
| 178 | [Test] |
| 179 | public async Task EmbeddingFromStringAndEmbeddingFromTokenIdsAreEqual() |
| 180 | { |
| 181 | EmbeddingClient client = GetTestClient(); |
| 182 | |
| 183 | List<string> input1 = new List<string> { "Hello, world!" }; |
| 184 | List<ReadOnlyMemory<int>> input2 = new List<ReadOnlyMemory<int>> { new[] { 9906, 11, 1917, 0 } }; |
| 185 | |
| 186 | OpenAIEmbeddingCollection results1 = IsAsync |
| 187 | ? await client.GenerateEmbeddingsAsync(input1) |
| 188 | : client.GenerateEmbeddings(input1); |
| 189 | |
| 190 | OpenAIEmbeddingCollection results2 = IsAsync |
| 191 | ? await client.GenerateEmbeddingsAsync(input2) |
| 192 | : client.GenerateEmbeddings(input2); |
| 193 | |
| 194 | ReadOnlyMemory<float> vector1 = results1[0].ToFloats(); |
| 195 | ReadOnlyMemory<float> vector2 = results2[0].ToFloats(); |
| 196 | |
| 197 | for (int i = 0; i < vector1.Length; i++) |
| 198 | { |
| 199 | Assert.That(vector1.Span[i], Is.EqualTo(vector2.Span[i]).Within(0.0005)); |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | [Test] |
| 204 | public void SerializeEmbeddingCollection() |
| 205 | { |
| 206 | // TODO: Add this test. |
| 207 | } |
| 208 | } |
| 209 | |