-
Notifications
You must be signed in to change notification settings - Fork 281
Expand file tree
/
Copy pathEmbeddingClient.cs
More file actions
81 lines (67 loc) · 3.15 KB
/
EmbeddingClient.cs
File metadata and controls
81 lines (67 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// --------------------------------------------------------------------------------------------------------------------
// <copyright company="Microsoft">
// Copyright (c) Microsoft. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------
namespace Microsoft.AI.Foundry.Local;
using Betalgo.Ranul.OpenAI.ObjectModels.ResponseModels;
using Microsoft.AI.Foundry.Local.Detail;
using Microsoft.AI.Foundry.Local.OpenAI;
using Microsoft.Extensions.Logging;
/// <summary>
/// Embedding Client that uses the OpenAI API.
/// Implemented using Betalgo.Ranul.OpenAI SDK types.
/// </summary>
public class OpenAIEmbeddingClient
{
private readonly string _modelId;
private readonly ICoreInterop _coreInterop = FoundryLocalManager.Instance.CoreInterop;
private readonly ILogger _logger = FoundryLocalManager.Instance.Logger;
internal OpenAIEmbeddingClient(string modelId)
{
_modelId = modelId;
}
/// <summary>
/// Settings that are supported by Foundry Local for embeddings.
/// </summary>
public record EmbeddingSettings
{
/// <summary>
/// The number of dimensions the resulting output embeddings should have.
/// Only supported by some models.
/// </summary>
public int? Dimensions { get; set; }
/// <summary>
/// The format to return the embeddings in. Can be either "float" or "base64".
/// </summary>
public string? EncodingFormat { get; set; }
}
/// <summary>
/// Settings to use for embedding requests using this client.
/// </summary>
public EmbeddingSettings Settings { get; } = new();
/// <summary>
/// Generate embeddings for the given input text.
/// </summary>
/// <param name="input">The text to generate embeddings for.</param>
/// <param name="ct">Optional cancellation token.</param>
/// <returns>Embedding response containing the embedding vector.</returns>
public async Task<EmbeddingCreateResponse> GenerateEmbeddingAsync(string input,
CancellationToken? ct = null)
{
return await Utils.CallWithExceptionHandling(
() => GenerateEmbeddingImplAsync(input, ct),
"Error during embedding generation.", _logger).ConfigureAwait(false);
}
private async Task<EmbeddingCreateResponse> GenerateEmbeddingImplAsync(string input,
CancellationToken? ct)
{
var embeddingRequest = EmbeddingCreateRequestExtended.FromUserInput(_modelId, input, Settings);
var embeddingRequestJson = embeddingRequest.ToJson();
var request = new CoreInteropRequest { Params = new() { { "OpenAICreateRequest", embeddingRequestJson } } };
var response = await _coreInterop.ExecuteCommandAsync("embeddings", request,
ct ?? CancellationToken.None).ConfigureAwait(false);
var embeddingResponse = response.ToEmbeddingResponse(_logger);
return embeddingResponse;
}
}