openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
mailinhphan/clientOptions

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Embeddings/Example02_EmbeddingWithOptions.cs

30lines · modecode

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