openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
copilot/diagnose-responses-test-issue

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Embeddings/Example01_SimpleEmbedding.cs

28lines · modecode

1using NUnit.Framework;
2using OpenAI.Embeddings;
3using System;
4
5namespace OpenAI.Examples;
6
7public partial class EmbeddingExamples
8{
9 [Test]
10 public void Example01_SimpleEmbedding()
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 OpenAIEmbedding embedding = client.GenerateEmbedding(description);
19 ReadOnlyMemory<float> vector = embedding.ToFloats();
20
21 Console.WriteLine($"Dimension: {vector.Length}");
22 Console.WriteLine($"Floats: ");
23 for (int i = 0; i < vector.Length; i++)
24 {
25 Console.WriteLine($" [{i,4}] = {vector.Span[i]}");
26 }
27 }
28}
29