openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
jsquire-patch-1

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Embeddings/Example03_MultipleEmbeddingsAsync.cs

38lines · modecode

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