-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathOpenAIEmbeddingsGenerator.cs
More file actions
48 lines (40 loc) · 1.69 KB
/
OpenAIEmbeddingsGenerator.cs
File metadata and controls
48 lines (40 loc) · 1.69 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
using Build5Nines.SharpVector.Embeddings;
using OpenAI.Embeddings;
using System.Collections.Generic;
using System.Linq;
namespace Build5Nines.SharpVector.OpenAI.Embeddings;
public class OpenAIEmbeddingsGenerator : IBatchEmbeddingsGenerator
{
protected EmbeddingClient EmbeddingClient { get; private set; }
public OpenAIEmbeddingsGenerator(EmbeddingClient embeddingClient)
{
EmbeddingClient = embeddingClient;
}
public async Task<float[]> GenerateEmbeddingsAsync(string text)
{
var result = await EmbeddingClient.GenerateEmbeddingAsync(text);
var embedding = result.Value;
var vector = embedding.ToFloats();
return vector.ToArray();
}
/// <summary>
/// Generates embeddings for a batch of input texts using the OpenAI embeddings client.
/// This leverages the API's multi-input batching for improved throughput and reduced overhead.
/// </summary>
/// <param name="texts">Collection of non-empty texts to embed.</param>
/// <returns>A list of float vectors aligned to the input order.</returns>
public async Task<IReadOnlyList<float[]>> GenerateEmbeddingsAsync(IEnumerable<string> texts)
{
if (texts is null) throw new ArgumentNullException(nameof(texts));
var inputs = texts.ToList();
if (inputs.Count == 0)
{
return Array.Empty<float[]>();
}
// Call the batch embeddings API once for all inputs.
var batchResult = await EmbeddingClient.GenerateEmbeddingsAsync(inputs);
// Map the embeddings to float arrays while preserving order.
var vectors = batchResult.Value.Select(e => e.ToFloats().ToArray()).ToList();
return vectors;
}
}