openai/openai-dotnet

Public

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

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.10.0

Branches

Tags

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

Clone

HTTPS

Download ZIP

examples/Embeddings/Example04_EmbeddingProtocol.cs

44lines · modeblame

39941cacJose Arriaga Maldonado2 years ago1using NUnit.Framework;
2using OpenAI.Embeddings;
3using System;
4using System.ClientModel;
5using System.Text.Json;
6
7namespace OpenAI.Examples;
8
9public partial class EmbeddingExamples
10{
11[Test]
7a8bc8beJose Arriaga Maldonado1 years ago12public void Example04_EmbeddingProtocol()
39941cacJose Arriaga Maldonado2 years ago13{
14EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));
15
16string 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
79014abcShivangiReja1 years ago20BinaryData input = BinaryData.FromObjectAsJson(new
21{
22model = "text-embedding-3-small",
23input = description,
24encoding_format = "float"
25});
39941cacJose Arriaga Maldonado2 years ago26
27using BinaryContent content = BinaryContent.Create(input);
28ClientResult result = client.GenerateEmbeddings(content);
29BinaryData output = result.GetRawResponse().Content;
30
31using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
32JsonElement vector = outputAsJson.RootElement
33.GetProperty("data"u8)[0]
34.GetProperty("embedding"u8);
35
36Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
37Console.WriteLine($"Floats: ");
38int i = 0;
39foreach (JsonElement element in vector.EnumerateArray())
40{
41Console.WriteLine($" [{i++,4}] = {element.GetDouble()}");
42}
43}
44}