-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathGeminiEmbeddingGenerator.cs
50 lines (42 loc) · 1.7 KB
/
GeminiEmbeddingGenerator.cs
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
#if NET472_OR_GREATER || NETSTANDARD2_0
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
#endif
using Microsoft.Extensions.AI;
namespace Mscc.GenerativeAI.Microsoft
{
public class GeminiEmbeddingGenerator : IEmbeddingGenerator<string, Embedding<float>>
{
private const string providerName = "gemini";
/// <summary>
/// Gets the Gemini model that is used to communicate with.
/// </summary>
private readonly GenerativeModel _client;
/// <inheritdoc/>
public EmbeddingGeneratorMetadata Metadata { get; }
public GeminiEmbeddingGenerator(string apiKey, string model = "")
{
var genAi = new GoogleAI(apiKey);
_client = genAi.GenerativeModel(model);
Metadata = new(providerName, null, model);
}
/// <inheritdoc/>
public async Task<GeneratedEmbeddings<Embedding<float>>> GenerateAsync(
IEnumerable<string> values,
EmbeddingGenerationOptions? options = null,
CancellationToken cancellationToken = default)
{
if (values == null) throw new ArgumentNullException(nameof(values));
var request = MicrosoftAi.AbstractionMapper.ToGeminiEmbedContentRequest(values, options);
var response = await _client.EmbedContent(request);
return MicrosoftAi.AbstractionMapper.ToGeneratedEmbeddings(request, response);
}
/// <inheritdoc/>
public object? GetService(Type serviceType, object? key)
=> key is null && serviceType?.IsInstanceOfType(this) is true ? this : null;
/// <inheritdoc/>
public void Dispose() { }
}
}