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