-
-
Notifications
You must be signed in to change notification settings - Fork 538
/
Copy pathSemanticKernelTextEmbeddingProvider.cs
67 lines (58 loc) · 2.92 KB
/
SemanticKernelTextEmbeddingProvider.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
using BotSharp.Abstraction.MLTasks;
using BotSharp.Abstraction.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel.Embeddings;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace BotSharp.Plugin.SemanticKernel
{
/// <summary>
/// Use Semantic Kernel Memory as text embedding provider
/// </summary>
public class SemanticKernelTextEmbeddingProvider : ITextEmbedding
{
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private readonly ITextEmbeddingGenerationService _embedding;
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
private readonly IConfiguration _configuration;
/// <summary>
/// Constructor of <see cref="SemanticKernelTextEmbeddingProvider"/>
/// </summary>
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
public SemanticKernelTextEmbeddingProvider(ITextEmbeddingGenerationService embedding, IConfiguration configuration)
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
{
_embedding = embedding;
_configuration = configuration;
_dimension = configuration.GetValue<int>("SemanticKernel:Dimension");
}
/// <inheritdoc/>
protected int _dimension;
public string Provider => "semantic-kernel";
public string Model => string.Empty;
/// <inheritdoc/>
public async Task<float[]> GetVectorAsync(string text)
{
#pragma warning disable SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
return (await this._embedding.GenerateEmbeddingAsync(text)).ToArray();
#pragma warning restore SKEXP0001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
}
/// <inheritdoc/>
public async Task<List<float[]>> GetVectorsAsync(List<string> texts)
{
var embeddings = await this._embedding.GenerateEmbeddingsAsync(texts);
return embeddings.Select(_ => _.ToArray())
.ToList();
}
public void SetModelName(string model) { }
public void SetDimension(int dimension)
{
_dimension = dimension > 0 ? dimension : _configuration.GetValue<int>("SemanticKernel:Dimension");
}
public int GetDimension()
{
return _dimension;
}
}
}