-
Notifications
You must be signed in to change notification settings - Fork 177
Expand file tree
/
Copy pathSampleEmbeddingGenerator.cs
More file actions
47 lines (38 loc) · 1.44 KB
/
SampleEmbeddingGenerator.cs
File metadata and controls
47 lines (38 loc) · 1.44 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
using Microsoft.Extensions.AI;
public class SampleEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
{
private readonly Random _random = new Random();
private readonly Uri _serviceEndpoint;
private readonly string _modelId;
public EmbeddingGeneratorMetadata Metadata { get; }
public SampleEmbeddingGenerator(Uri endpoint, string modelId)
{
_serviceEndpoint = endpoint;
_modelId = modelId;
Metadata = new EmbeddingGeneratorMetadata("SampleEmbeddingGenerator", endpoint, modelId);
}
public async Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(
IEnumerable<string> values,
EmbeddingGenerationOptions? options = null,
CancellationToken cancellationToken = default)
{
GeneratedEmbeddings<Embedding<float>> embeddings = [];
foreach (var value in values)
{
// Simulate some async operation
await Task.Delay(100, cancellationToken);
// Generate a sample embedding
embeddings.Add(new(new[] {
_random.NextSingle(),
_random.NextSingle(),
_random.NextSingle()}));
}
return embeddings;
}
public object? GetService(Type serviceType, object? key = null) =>
key is null && serviceType?.IsInstanceOfType(this) is true ? this : null;
public void Dispose()
{
// Clean up resources if necessary
}
}