openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Embeddings/Example03_MultipleEmbeddingsAsync.cs
38lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Embeddings; |
| 3 | using System; |
| 4 | using System.Collections.Generic; |
| 5 | using System.Threading.Tasks; |
| 6 | |
| 7 | namespace OpenAI.Examples; |
| 8 | |
| 9 | public partial class EmbeddingExamples |
| 10 | { |
| 11 | [Test] |
| 12 | public async Task Example03_MultipleEmbeddingsAsync() |
| 13 | { |
| 14 | EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 15 | |
| 16 | string category = "Luxury"; |
| 17 | string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa," |
| 18 | + " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist" |
| 19 | + " attractions. We highly recommend this hotel."; |
| 20 | List<string> inputs = [category, description]; |
| 21 | |
| 22 | OpenAIEmbeddingCollection collection = await client.GenerateEmbeddingsAsync(inputs); |
| 23 | |
| 24 | foreach (OpenAIEmbedding embedding in collection) |
| 25 | { |
| 26 | ReadOnlyMemory<float> vector = embedding.ToFloats(); |
| 27 | |
| 28 | Console.WriteLine($"Dimension: {vector.Length}"); |
| 29 | Console.WriteLine($"Floats: "); |
| 30 | for (int i = 0; i < vector.Length; i++) |
| 31 | { |
| 32 | Console.WriteLine($" [{i,4}] = {vector.Span[i]}"); |
| 33 | } |
| 34 | |
| 35 | Console.WriteLine(); |
| 36 | } |
| 37 | } |
| 38 | } |
| 39 | |