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/Example04_EmbeddingProtocolAsync.cs

45lines · modepreview

using NUnit.Framework;
using OpenAI.Embeddings;
using System;
using System.ClientModel;
using System.Text.Json;
using System.Threading.Tasks;

namespace OpenAI.Examples;

public partial class EmbeddingExamples
{
    [Test]
    public async Task Example04_EmbeddingProtocolAsync()
    {
        EmbeddingClient client = new("text-embedding-3-small", Environment.GetEnvironmentVariable("OPENAI_API_KEY"));

        string description = "Best hotel in town if you like luxury hotels. They have an amazing infinity pool, a spa,"
            + " and a really helpful concierge. The location is perfect -- right downtown, close to all the tourist"
            + " attractions. We highly recommend this hotel.";

        BinaryData input = BinaryData.FromObjectAsJson(new
        {
            model = "text-embedding-3-small",
            input = description,
            encoding_format = "float"
        });

        using BinaryContent content = BinaryContent.Create(input);
        ClientResult result = await client.GenerateEmbeddingsAsync(content);
        BinaryData output = result.GetRawResponse().Content;

        using JsonDocument outputAsJson = JsonDocument.Parse(output.ToString());
        JsonElement vector = outputAsJson.RootElement
            .GetProperty("data"u8)[0]
            .GetProperty("embedding"u8);

        Console.WriteLine($"Dimension: {vector.GetArrayLength()}");
        Console.WriteLine($"Floats: ");
        int i = 0;
        foreach (JsonElement element in vector.EnumerateArray())
        {
            Console.WriteLine($"  [{i++,4}] = {element.GetDouble()}");
        }
    }
}