openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Embeddings/EmbeddingsMockTests.cs
247lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Embeddings; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System; |
| 5 | using System.ClientModel; |
| 6 | using System.Linq; |
| 7 | using System.Threading; |
| 8 | using System.Threading.Tasks; |
| 9 | |
| 10 | namespace OpenAI.Tests.Embeddings; |
| 11 | |
| 12 | [TestFixture(true)] |
| 13 | [TestFixture(false)] |
| 14 | [Parallelizable(ParallelScope.All)] |
| 15 | [Category("Embeddings")] |
| 16 | [Category("Smoke")] |
| 17 | public class EmbeddingsMockTests : SyncAsyncTestBase |
| 18 | { |
| 19 | private static readonly ApiKeyCredential s_fakeCredential = new ApiKeyCredential("key"); |
| 20 | |
| 21 | public EmbeddingsMockTests(bool isAsync) |
| 22 | : base(isAsync) |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | [Test] |
| 27 | public async Task GenerateEmbeddingDeserializesVector() |
| 28 | { |
| 29 | string base64Embedding = Convert.ToBase64String([ |
| 30 | 0x00, |
| 31 | 0x00, |
| 32 | 0x80, |
| 33 | 0x3F, // 1.0f |
| 34 | 0x00, |
| 35 | 0x00, |
| 36 | 0x00, |
| 37 | 0x40, // 2.0f |
| 38 | 0x00, |
| 39 | 0x00, |
| 40 | 0x40, |
| 41 | 0x40 // 3.0f |
| 42 | ]); |
| 43 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 44 | { |
| 45 | "data": [ |
| 46 | { |
| 47 | "embedding": "{{base64Embedding}}" |
| 48 | } |
| 49 | ] |
| 50 | } |
| 51 | """); |
| 52 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential, clientOptions); |
| 53 | |
| 54 | OpenAIEmbedding embedding = IsAsync |
| 55 | ? await client.GenerateEmbeddingAsync("prompt") |
| 56 | : client.GenerateEmbedding("prompt"); |
| 57 | |
| 58 | float[] vector = embedding.ToFloats().ToArray(); |
| 59 | Assert.That(vector.SequenceEqual([1f, 2f, 3f])); |
| 60 | } |
| 61 | |
| 62 | [Test] |
| 63 | public void GenerateEmbeddingRespectsTheCancellationToken() |
| 64 | { |
| 65 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential); |
| 66 | using CancellationTokenSource cancellationSource = new(); |
| 67 | cancellationSource.Cancel(); |
| 68 | |
| 69 | if (IsAsync) |
| 70 | { |
| 71 | Assert.That(async () => await client.GenerateEmbeddingAsync("prompt", cancellationToken: cancellationSource.Token), |
| 72 | Throws.InstanceOf<OperationCanceledException>()); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | Assert.That(() => client.GenerateEmbedding("prompt", cancellationToken: cancellationSource.Token), |
| 77 | Throws.InstanceOf<OperationCanceledException>()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | [Test] |
| 82 | public async Task GenerateEmbeddingsWithStringsDeserializesUsage() |
| 83 | { |
| 84 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 85 | { |
| 86 | "data": [], |
| 87 | "usage": { |
| 88 | "prompt_tokens": 10, |
| 89 | "total_tokens": 20 |
| 90 | } |
| 91 | } |
| 92 | """); |
| 93 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential, clientOptions); |
| 94 | |
| 95 | OpenAIEmbeddingCollection embeddings = IsAsync |
| 96 | ? await client.GenerateEmbeddingsAsync(["prompt"]) |
| 97 | : client.GenerateEmbeddings(["prompt"]); |
| 98 | |
| 99 | Assert.That(embeddings.Usage.InputTokenCount, Is.EqualTo(10)); |
| 100 | Assert.That(embeddings.Usage.TotalTokenCount, Is.EqualTo(20)); |
| 101 | } |
| 102 | |
| 103 | [Test] |
| 104 | public async Task GenerateEmbeddingsWithStringsDeserializesVector() |
| 105 | { |
| 106 | string base64Embedding = Convert.ToBase64String([ |
| 107 | 0x00, |
| 108 | 0x00, |
| 109 | 0x80, |
| 110 | 0x3F, // 1.0f |
| 111 | 0x00, |
| 112 | 0x00, |
| 113 | 0x00, |
| 114 | 0x40, // 2.0f |
| 115 | 0x00, |
| 116 | 0x00, |
| 117 | 0x40, |
| 118 | 0x40 // 3.0f |
| 119 | ]); |
| 120 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 121 | { |
| 122 | "data": [ |
| 123 | { |
| 124 | "embedding": "{{base64Embedding}}" |
| 125 | } |
| 126 | ] |
| 127 | } |
| 128 | """); |
| 129 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential, clientOptions); |
| 130 | |
| 131 | OpenAIEmbeddingCollection embeddings = IsAsync |
| 132 | ? await client.GenerateEmbeddingsAsync(["prompt"]) |
| 133 | : client.GenerateEmbeddings(["prompt"]); |
| 134 | OpenAIEmbedding embedding = embeddings.Single(); |
| 135 | |
| 136 | float[] vector = embedding.ToFloats().ToArray(); |
| 137 | Assert.That(vector.SequenceEqual([1f, 2f, 3f])); |
| 138 | } |
| 139 | |
| 140 | [Test] |
| 141 | public void GenerateEmbeddingsWithStringsRespectsTheCancellationToken() |
| 142 | { |
| 143 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential); |
| 144 | using CancellationTokenSource cancellationSource = new(); |
| 145 | cancellationSource.Cancel(); |
| 146 | |
| 147 | if (IsAsync) |
| 148 | { |
| 149 | Assert.That(async () => await client.GenerateEmbeddingsAsync(["prompt"], cancellationToken: cancellationSource.Token), |
| 150 | Throws.InstanceOf<OperationCanceledException>()); |
| 151 | } |
| 152 | else |
| 153 | { |
| 154 | Assert.That(() => client.GenerateEmbeddings(["prompt"], cancellationToken: cancellationSource.Token), |
| 155 | Throws.InstanceOf<OperationCanceledException>()); |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | [Test] |
| 160 | public async Task GenerateEmbeddingsWithIntegersDeserializesUsage() |
| 161 | { |
| 162 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, """ |
| 163 | { |
| 164 | "data": [], |
| 165 | "usage": { |
| 166 | "prompt_tokens": 10, |
| 167 | "total_tokens": 20 |
| 168 | } |
| 169 | } |
| 170 | """); |
| 171 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential, clientOptions); |
| 172 | |
| 173 | OpenAIEmbeddingCollection embeddings = IsAsync |
| 174 | ? await client.GenerateEmbeddingsAsync([new[] { 1 }]) |
| 175 | : client.GenerateEmbeddings([new[] { 1 }]); |
| 176 | |
| 177 | Assert.That(embeddings.Usage.InputTokenCount, Is.EqualTo(10)); |
| 178 | Assert.That(embeddings.Usage.TotalTokenCount, Is.EqualTo(20)); |
| 179 | } |
| 180 | |
| 181 | [Test] |
| 182 | public async Task GenerateEmbeddingsWithIntegersDeserializesVector() |
| 183 | { |
| 184 | string base64Embedding = Convert.ToBase64String([ |
| 185 | 0x00, |
| 186 | 0x00, |
| 187 | 0x80, |
| 188 | 0x3F, // 1.0f |
| 189 | 0x00, |
| 190 | 0x00, |
| 191 | 0x00, |
| 192 | 0x40, // 2.0f |
| 193 | 0x00, |
| 194 | 0x00, |
| 195 | 0x40, |
| 196 | 0x40 // 3.0f |
| 197 | ]); |
| 198 | OpenAIClientOptions clientOptions = GetClientOptionsWithMockResponse(200, $$""" |
| 199 | { |
| 200 | "data": [ |
| 201 | { |
| 202 | "embedding": "{{base64Embedding}}" |
| 203 | } |
| 204 | ] |
| 205 | } |
| 206 | """); |
| 207 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential, clientOptions); |
| 208 | |
| 209 | OpenAIEmbeddingCollection embeddings = IsAsync |
| 210 | ? await client.GenerateEmbeddingsAsync([new[] { 1 }]) |
| 211 | : client.GenerateEmbeddings([new[] { 1 }]); |
| 212 | OpenAIEmbedding embedding = embeddings.Single(); |
| 213 | |
| 214 | float[] vector = embedding.ToFloats().ToArray(); |
| 215 | Assert.That(vector.SequenceEqual([1f, 2f, 3f])); |
| 216 | } |
| 217 | |
| 218 | [Test] |
| 219 | public void GenerateEmbeddingsWithIntegersRespectsTheCancellationToken() |
| 220 | { |
| 221 | EmbeddingClient client = new EmbeddingClient("model", s_fakeCredential); |
| 222 | using CancellationTokenSource cancellationSource = new(); |
| 223 | cancellationSource.Cancel(); |
| 224 | |
| 225 | if (IsAsync) |
| 226 | { |
| 227 | Assert.That(async () => await client.GenerateEmbeddingsAsync([new[] { 1 }], cancellationToken: cancellationSource.Token), |
| 228 | Throws.InstanceOf<OperationCanceledException>()); |
| 229 | } |
| 230 | else |
| 231 | { |
| 232 | Assert.That(() => client.GenerateEmbeddings([new[] { 1 }], cancellationToken: cancellationSource.Token), |
| 233 | Throws.InstanceOf<OperationCanceledException>()); |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | private OpenAIClientOptions GetClientOptionsWithMockResponse(int status, string content) |
| 238 | { |
| 239 | MockPipelineResponse response = new MockPipelineResponse(status); |
| 240 | response.SetContent(content); |
| 241 | |
| 242 | return new OpenAIClientOptions() |
| 243 | { |
| 244 | Transport = new MockPipelineTransport(response) |
| 245 | }; |
| 246 | } |
| 247 | } |
| 248 | |