Skip to content

Add configprovider to openai text generation and model config #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion extensions/Chunkers/Chunkers/PlainTextChunker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal enum SeparatorTypes
".", "?", "!", "⁉", "⁈", "⁇", "…",
// Chinese punctuation
"。", "?", "!", ";", ":"
]);
]);

// Prioritized list of characters to split inside a sentence.
private static readonly SeparatorTrie s_potentialSeparators = new([
Expand Down
25 changes: 13 additions & 12 deletions extensions/OpenAI/OpenAI/DependencyInjection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI;
using Microsoft.KernelMemory.AI.OpenAI;
using Microsoft.KernelMemory.Context;
using OpenAI;

#pragma warning disable IDE0130 // reduce number of "using" statements
Expand Down Expand Up @@ -226,12 +227,11 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration(
{
config.Validate();
return services
.AddSingleton<ITextEmbeddingGenerator>(
serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>(),
httpClient));
.AddSingleton<ITextEmbeddingGenerator>(serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>(),
httpClient));
}

public static IServiceCollection AddOpenAITextEmbeddingGeneration(
Expand All @@ -242,12 +242,11 @@ public static IServiceCollection AddOpenAITextEmbeddingGeneration(
{
config.Validate();
return services
.AddSingleton<ITextEmbeddingGenerator>(
serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
openAIClient: openAIClient,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>()));
.AddSingleton<ITextEmbeddingGenerator>(serviceProvider => new OpenAITextEmbeddingGenerator(
config: config,
openAIClient: openAIClient,
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>()));
}

public static IServiceCollection AddOpenAITextGeneration(
Expand All @@ -261,6 +260,7 @@ public static IServiceCollection AddOpenAITextGeneration(
.AddSingleton<ITextGenerator, OpenAITextGenerator>(serviceProvider => new OpenAITextGenerator(
config: config,
textTokenizer: textTokenizer,
contextProvider: serviceProvider.GetService<IContextProvider>(),
loggerFactory: serviceProvider.GetService<ILoggerFactory>(),
httpClient));
}
Expand All @@ -276,6 +276,7 @@ public static IServiceCollection AddOpenAITextGeneration(
.AddSingleton<ITextGenerator, OpenAITextGenerator>(serviceProvider => new OpenAITextGenerator(
config: config,
openAIClient: openAIClient,
contextProvider: serviceProvider.GetService<IContextProvider>(),
textTokenizer: textTokenizer,
loggerFactory: serviceProvider.GetService<ILoggerFactory>()));
}
Expand Down
21 changes: 17 additions & 4 deletions extensions/OpenAI/OpenAI/OpenAITextGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.KernelMemory.AI.OpenAI.Internals;
using Microsoft.KernelMemory.Context;
using Microsoft.KernelMemory.Diagnostics;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
Expand All @@ -30,8 +31,9 @@ public sealed class OpenAITextGenerator : ITextGenerator
private readonly OpenAIChatCompletionService _client;
private readonly ITextTokenizer _textTokenizer;
private readonly ILogger<OpenAITextGenerator> _log;
private readonly IContextProvider _contextProvider;

private readonly string _textModel;
private readonly string _modelName;

/// <inheritdoc/>
public int MaxTokenTotal { get; }
Expand All @@ -41,17 +43,20 @@ public sealed class OpenAITextGenerator : ITextGenerator
/// </summary>
/// <param name="config">Client and model configuration</param>
/// <param name="textTokenizer">Text tokenizer, possibly matching the model used</param>
/// <param name="contextProvider">Request context provider with runtime configuration overrides</param>
/// <param name="loggerFactory">App logger factory</param>
/// <param name="httpClient">Optional HTTP client with custom settings</param>
public OpenAITextGenerator(
OpenAIConfig config,
ITextTokenizer? textTokenizer = null,
IContextProvider? contextProvider = null,
ILoggerFactory? loggerFactory = null,
HttpClient? httpClient = null)
: this(
config,
OpenAIClientBuilder.Build(config, httpClient, loggerFactory),
textTokenizer,
contextProvider,
loggerFactory)
{
}
Expand All @@ -62,16 +67,19 @@ public OpenAITextGenerator(
/// <param name="config">Model configuration</param>
/// <param name="openAIClient">Custom OpenAI client, already configured</param>
/// <param name="textTokenizer">Text tokenizer, possibly matching the model used</param>
/// <param name="contextProvider">Request context provider with runtime configuration overrides</param>
/// <param name="loggerFactory">App logger factory</param>
public OpenAITextGenerator(
OpenAIConfig config,
OpenAIClient openAIClient,
ITextTokenizer? textTokenizer = null,
IContextProvider? contextProvider = null,
ILoggerFactory? loggerFactory = null)
: this(
config,
SkClientBuilder.BuildChatClient(config.TextModel, openAIClient, loggerFactory),
textTokenizer,
contextProvider,
loggerFactory)
{
}
Expand All @@ -81,17 +89,20 @@ public OpenAITextGenerator(
/// </summary>
/// <param name="config">Model configuration</param>
/// <param name="skClient">Custom Semantic Kernel client, already configured</param>
/// <param name="contextProvider">Request context provider with runtime configuration overrides</param>
/// <param name="textTokenizer">Text tokenizer, possibly matching the model used</param>
/// <param name="loggerFactory">App logger factory</param>
public OpenAITextGenerator(
OpenAIConfig config,
OpenAIChatCompletionService skClient,
ITextTokenizer? textTokenizer = null,
IContextProvider? contextProvider = null,
ILoggerFactory? loggerFactory = null)
{
this._client = skClient;
this._contextProvider = contextProvider ?? new RequestContextProvider();
this._log = (loggerFactory ?? DefaultLogger.Factory).CreateLogger<OpenAITextGenerator>();
this._textModel = config.TextModel;
this._modelName = config.TextModel;
this.MaxTokenTotal = config.TextModelMaxTokenTotal;

if (textTokenizer == null && !string.IsNullOrEmpty(config.TextModelTokenizer))
Expand Down Expand Up @@ -129,13 +140,15 @@ public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
TextGenerationOptions options,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
var modelName = this._contextProvider.GetContext().GetCustomTextGenerationModelNameOrDefault(this._modelName);
var skOptions = new OpenAIPromptExecutionSettings
{
ModelId = modelName,
MaxTokens = options.MaxTokens,
Temperature = options.Temperature,
FrequencyPenalty = options.FrequencyPenalty,
PresencePenalty = options.PresencePenalty,
TopP = options.NucleusSampling
TopP = options.NucleusSampling,
};

if (options.StopSequences is { Count: > 0 })
Expand Down Expand Up @@ -178,7 +191,7 @@ public async IAsyncEnumerable<GeneratedTextContent> GenerateTextAsync(
Timestamp = (DateTimeOffset?)x.Metadata["CreatedAt"] ?? DateTimeOffset.UtcNow,
ServiceType = "OpenAI",
ModelType = Constants.ModelType.TextGeneration,
ModelName = this._textModel,
ModelName = modelName,
ServiceTokensIn = usage!.InputTokenCount,
ServiceTokensOut = usage.OutputTokenCount,
ServiceReasoningTokens = usage.OutputTokenDetails?.ReasoningTokenCount
Expand Down
2 changes: 1 addition & 1 deletion service/Abstractions/Context/IContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ public static int GetCustomEmbeddingGenerationBatchSizeOrDefault(this IContext?
/// Extensions supported:
/// - Ollama
/// - Anthropic
/// - OpenAI
/// Extensions not supported:
/// - Azure OpenAI
/// - ONNX
/// - OpenAI
/// </summary>
public static string GetCustomTextGenerationModelNameOrDefault(this IContext? context, string defaultValue)
{
Expand Down