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