-
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.
- Loading branch information
Showing
29 changed files
with
298 additions
and
112 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
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
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,32 @@ | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Embeddings; | ||
using Microsoft.SemanticKernel.Memory; | ||
using Microsoft.SemanticKernel.Plugins.Memory; | ||
using SemanticKernel.Connectors.Memory.SqlServer; | ||
|
||
namespace OpenAIExtensions.Chats | ||
{ | ||
public static class KernelPluginsImporter | ||
{ | ||
public static async void ImportTextMemoryPlugin( | ||
this Kernel kernel, | ||
string connectionString, | ||
string schema, | ||
CancellationToken ct = default) | ||
{ | ||
var config = new SqlServerConfig() | ||
{ | ||
ConnectionString = connectionString, | ||
Schema = schema ?? "ai" | ||
}; | ||
|
||
var sqlMemoryStore = await SqlServerMemoryStore | ||
.ConnectAsync(connectionString: connectionString, config, cancellationToken: ct); | ||
|
||
var embeddingGenerator = kernel.GetRequiredService<ITextEmbeddingGenerationService>(); | ||
|
||
var semanticTextMemory = new SemanticTextMemory(sqlMemoryStore, embeddingGenerator); | ||
kernel.ImportPluginFromObject(new TextMemoryPlugin(semanticTextMemory)); | ||
} | ||
} | ||
} |
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,91 @@ | ||
using KernelMemory.MemoryStorage.SqlServer; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.KernelMemory; | ||
using Microsoft.KernelMemory.AI; | ||
using Microsoft.KernelMemory.AI.AzureOpenAI; | ||
using Microsoft.KernelMemory.AI.OpenAI; | ||
using Microsoft.SemanticKernel; | ||
|
||
namespace OpenAIExtensions | ||
{ | ||
public class CreateKernelMemoryRequest | ||
{ | ||
public required string Endpoint { get; set; } | ||
public required string ApiKey { get; set; } | ||
public required string ConnectionString { get; set; } | ||
public string? Schema { get; set; } | ||
} | ||
|
||
public static class KernelMemoryFactory | ||
{ | ||
public static IKernelMemory Create(CreateKernelMemoryRequest request) | ||
{ | ||
if (request is null) | ||
{ | ||
throw new ArgumentNullException(nameof(request)); | ||
} | ||
|
||
var kernelMemoryBuilder = new KernelMemoryBuilder() | ||
.WithAzureOpenAIDefaults( | ||
request.Endpoint, | ||
request.ApiKey); | ||
|
||
var config = new SqlServerConfig() | ||
{ | ||
ConnectionString = request.ConnectionString, | ||
Schema = request.Schema ?? "ai" | ||
}; | ||
|
||
var kernelMemory = kernelMemoryBuilder | ||
.WithSqlServerMemoryDb(config) | ||
.Build<MemoryServerless>(); | ||
|
||
return kernelMemory; | ||
} | ||
|
||
public static IKernelMemoryBuilder WithAzureOpenAIDefaults( | ||
this IKernelMemoryBuilder builder, | ||
string endpoint, | ||
string apiKey, | ||
ITextTokenizer? textGenerationTokenizer = null, | ||
ITextTokenizer? textEmbeddingTokenizer = null, | ||
ILoggerFactory? loggerFactory = null, | ||
bool onlyForRetrieval = false, | ||
HttpClient? httpClient = null) | ||
{ | ||
textGenerationTokenizer ??= new DefaultGPTTokenizer(); | ||
textEmbeddingTokenizer ??= new DefaultGPTTokenizer(); | ||
|
||
var textEmbbedingAIConfig = new AzureOpenAIConfig | ||
{ | ||
APIKey = apiKey, | ||
Endpoint = endpoint, | ||
Deployment = "text-embedding-ada-002", | ||
MaxRetries = 3, | ||
Auth = AzureOpenAIConfig.AuthTypes.APIKey, | ||
MaxTokenTotal = 8191, | ||
}; | ||
|
||
var textGenerationAIConfig = new AzureOpenAIConfig | ||
{ | ||
APIKey = apiKey, | ||
Endpoint = endpoint, | ||
Deployment = "gpt-35-turbo-0613", | ||
MaxRetries = 3, | ||
Auth = AzureOpenAIConfig.AuthTypes.APIKey, | ||
MaxTokenTotal = 16384, | ||
}; | ||
|
||
textEmbbedingAIConfig.Validate(); | ||
textGenerationAIConfig.Validate(); | ||
builder.Services.AddAzureOpenAIEmbeddingGeneration(textEmbbedingAIConfig, textEmbeddingTokenizer, httpClient); | ||
builder.Services.AddAzureOpenAITextGeneration(textGenerationAIConfig, textGenerationTokenizer, httpClient); | ||
if (!onlyForRetrieval) | ||
{ | ||
builder.AddIngestionEmbeddingGenerator(new AzureOpenAITextEmbeddingGenerator(textEmbbedingAIConfig, textEmbeddingTokenizer, loggerFactory, httpClient)); | ||
} | ||
|
||
return builder; | ||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.