openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/fix-security-dependencies

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

examples/Embeddings/Example01_SimpleEmbeddingAsync.cs

29lines · modecode

1using NUnit.Framework;
2using OpenAI.Embeddings;
3using System;
4using System.Threading.Tasks;
5
6namespace OpenAI.Examples;
7
8public 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