openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.1.0-beta.2

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Embeddings/Example03_MultipleEmbeddings.cs

37lines · modecode

1using NUnit.Framework;
2using OpenAI.Embeddings;
3using System;
4using System.Collections.Generic;
5
6namespace OpenAI.Examples;
7
8public partial class EmbeddingExamples
9{
10 [Test]
11 public void Example03_MultipleEmbeddings()
12 {
13 EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
14
15 string category = "Luxury";
16 string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
17 + " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
18 + " attractions. We highly recommend this hotel.";
19 List<string> inputs = [category, description];
20
21 OpenAIEmbeddingCollection collection = client.GenerateEmbeddings(inputs);
22
23 foreach (OpenAIEmbedding embedding in collection)
24 {
25 ReadOnlyMemory<float> vector = embedding.ToFloats();
26
27 Console.WriteLine($"Dimension: {vector.Length}");
28 Console.WriteLine($"Floats: ");
29 for (int i = 0; i < vector.Length; i++)
30 {
31 Console.WriteLine($" [{i,4}] = {vector.Span[i]}");
32 }
33
34 Console.WriteLine();
35 }
36 }
37}
38