-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for massive embedding model for azure openai.
- Loading branch information
1 parent
d5fa29f
commit af5574e
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/KernelMemory.Extensions.Interfaces/IBulkTextEmbeddingGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
using Microsoft.KernelMemory; | ||
using Microsoft.KernelMemory.AI; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace KernelMemory.Extensions.Interfaces | ||
{ | ||
/// <summary> | ||
/// For better performances we can support embedding generators that | ||
/// can process multiple texts at once. | ||
/// </summary> | ||
public interface IBulkTextEmbeddingGenerator : ITextEmbeddingGenerator | ||
{ | ||
Task<Embedding[]> GenerateEmbeddingsAsync( | ||
string[] text, | ||
CancellationToken cancellationToken = default); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/KernelMemory.Extensions/Helper/AzureOpenaiEmbeddingGenerator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using Azure; | ||
using Azure.AI.OpenAI; | ||
using KernelMemory.Extensions.Interfaces; | ||
using Microsoft.KernelMemory; | ||
using System; | ||
using System.Linq; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace KernelMemory.Extensions.Helper | ||
{ | ||
/// <summary> | ||
/// Used to create massive embedding vectors for a given text. | ||
/// </summary> | ||
public class AzureOpenaiEmbeddingGenerator : IBulkTextEmbeddingGenerator | ||
{ | ||
private readonly OpenAIClient _client; | ||
private readonly MicrosoftMlTiktokenTokenizer _microsoftMlTiktokenTokenizer; | ||
private readonly string _deployment; | ||
private readonly int? _dimensions; | ||
|
||
public AzureOpenaiEmbeddingGenerator( | ||
AzureOpenAIConfig azureOpenAIConfig, | ||
MicrosoftMlTiktokenTokenizer microsoftMlTiktokenTokenizer) | ||
{ | ||
_client = new OpenAIClient(new Uri(azureOpenAIConfig.Endpoint), new AzureKeyCredential(azureOpenAIConfig.APIKey)); | ||
MaxTokens = azureOpenAIConfig.MaxTokenTotal; | ||
_microsoftMlTiktokenTokenizer = microsoftMlTiktokenTokenizer; | ||
_deployment = azureOpenAIConfig.Deployment; | ||
_dimensions = azureOpenAIConfig.EmbeddingDimensions; | ||
} | ||
|
||
public AzureOpenaiEmbeddingGenerator( | ||
AzureOpenAIConfig azureOpenAIConfig, | ||
string modelName) : this (azureOpenAIConfig, new MicrosoftMlTiktokenTokenizer(modelName)) | ||
{ | ||
} | ||
|
||
public int MaxTokens { get; } | ||
|
||
public int CountTokens(string text) | ||
{ | ||
return _microsoftMlTiktokenTokenizer.CountTokens(text); | ||
} | ||
|
||
public async Task<Embedding> GenerateEmbeddingAsync(string text, CancellationToken cancellationToken = default) | ||
{ | ||
var result = await GenerateEmbeddingsAsync([text], cancellationToken); | ||
return result[0]; | ||
} | ||
|
||
public async Task<Embedding[]> GenerateEmbeddingsAsync(string[] text, CancellationToken cancellationToken = default) | ||
{ | ||
var options = new EmbeddingsOptions(_deployment, text); | ||
if (_dimensions.HasValue) | ||
{ | ||
options.Dimensions = _dimensions.Value; | ||
} | ||
var result = await _client.GetEmbeddingsAsync(options, cancellationToken); | ||
return result.Value.Data.Select(ei => new Embedding(ei.Embedding)).ToArray(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters