Skip to content

Commit

Permalink
.Net: Remove Kernel.Builder (#3180)
Browse files Browse the repository at this point in the history
#3169 
Use `new KernelBuilder()` instead of `Kernel.Builder`

---------

Co-authored-by: Dmytro Struk <13853051+dmytrostruk@users.noreply.github.com>
Co-authored-by: Roger Barreto <19890735+RogerBarreto@users.noreply.github.com>
  • Loading branch information
3 people authored Oct 24, 2023
1 parent 3601022 commit ba40209
Show file tree
Hide file tree
Showing 42 changed files with 108 additions and 107 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public static async Task RunAsync()
return;
}

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithOpenAIChatCompletionService(
modelId: openAIModelId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ private static async Task CustomHandlerAsync()

private static KernelBuilder InitializeKernelBuilder()
{
return Kernel.Builder
return new KernelBuilder()
.WithLoggerFactory(InfoLogger.LoggerFactory)
// OpenAI settings - you can set the OpenAI.ApiKey to an invalid value to see the retry policy in play
.WithOpenAIChatCompletionService(TestConfiguration.OpenAI.ChatModelId, "BAD_KEY");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static async Task RunAsync()
{
Console.WriteLine("======== Native function types ========");

var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithOpenAIChatCompletionService(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey)
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static Task RunAsync()
{
Console.WriteLine("======== Describe all plugins and functions ========");

var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithOpenAIChatCompletionService(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static async Task RunAsync()
{
Console.WriteLine("======== WebSearchQueries ========");

IKernel kernel = Kernel.Builder.WithLoggerFactory(ConsoleLogger.LoggerFactory).Build();
IKernel kernel = new KernelBuilder().WithLoggerFactory(ConsoleLogger.LoggerFactory).Build();

// Load native plugins
var plugin = new SearchUrlPlugin();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ private static async Task GetConversationTopicsAsync()

private static IKernel InitializeKernel()
{
IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithAzureChatCompletionService(
TestConfiguration.AzureOpenAI.ChatDeploymentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ private static IMemoryStore CreateSampleKustoMemoryStore()

private static async Task RunWithStoreAsync(IMemoryStore memoryStore, CancellationToken cancellationToken)
{
var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithOpenAIChatCompletionService(TestConfiguration.OpenAI.ChatModelId, TestConfiguration.OpenAI.ApiKey)
.WithOpenAITextEmbeddingGenerationService(TestConfiguration.OpenAI.EmbeddingModelId, TestConfiguration.OpenAI.ApiKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ private static async Task UseKernelInDIPowerAppAsync()
//Registering Kernel
collection.AddTransient<IKernel>((serviceProvider) =>
{
return Kernel.Builder
return new KernelBuilder()
.WithLoggerFactory(serviceProvider.GetRequiredService<ILoggerFactory>())
.WithOpenAITextCompletionService(TestConfiguration.OpenAI.ModelId, TestConfiguration.OpenAI.ApiKey)
.Build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public static Task RunAsync()
/// </summary>
private static void UseDefaultHttpClient()
{
var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithOpenAIChatCompletionService(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey) // If you need to use the default HttpClient from the SK SDK, simply omit the argument for the httpMessageInvoker parameter.
Expand All @@ -47,7 +47,7 @@ private static void UseCustomHttpClient()
using var httpClient = new HttpClient();

// If you need to use a custom HttpClient, simply pass it as an argument for the httpClient parameter.
var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithOpenAIChatCompletionService(
modelId: TestConfiguration.OpenAI.ModelId,
apiKey: TestConfiguration.OpenAI.ApiKey,
Expand All @@ -68,7 +68,7 @@ private static void UseBasicRegistrationWithHttpClientFactory()
{
var factory = sp.GetRequiredService<IHttpClientFactory>();

var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithOpenAIChatCompletionService(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey,
Expand Down Expand Up @@ -99,7 +99,7 @@ private static void UseNamedRegistrationWitHttpClientFactory()
{
var factory = sp.GetRequiredService<IHttpClientFactory>();

var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithOpenAIChatCompletionService(
modelId: TestConfiguration.OpenAI.ChatModelId,
apiKey: TestConfiguration.OpenAI.ApiKey,
Expand Down
22 changes: 11 additions & 11 deletions dotnet/samples/KernelSyntaxExamples/Example42_KernelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

// ==========================================================================================================
// The easier way to instantiate the Semantic Kernel is to use KernelBuilder.
// You can access the builder using either Kernel.Builder or KernelBuilder.
// You can access the builder using new KernelBuilder().

#pragma warning disable CA1852

Expand Down Expand Up @@ -39,20 +39,20 @@ public static Task RunAsync()
string azureOpenAIEmbeddingDeployment = TestConfiguration.AzureOpenAIEmbeddings.DeploymentName;

#pragma warning disable CA1852 // Seal internal types
IKernel kernel1 = Kernel.Builder.Build();
IKernel kernel1 = new KernelBuilder().Build();
#pragma warning restore CA1852 // Seal internal types

IKernel kernel2 = Kernel.Builder.Build();
IKernel kernel2 = new KernelBuilder().Build();

// ==========================================================================================================
// Kernel.Builder returns a new builder instance, in case you want to configure the builder differently.
// new KernelBuilder() returns a new builder instance, in case you want to configure the builder differently.
// The following are 3 distinct builder instances.

var builder1 = new KernelBuilder();

var builder2 = Kernel.Builder;
var builder2 = new KernelBuilder();

var builder3 = Kernel.Builder;
var builder3 = new KernelBuilder();

// ==========================================================================================================
// A builder instance can create multiple kernel instances, e.g. in case you need
Expand Down Expand Up @@ -108,7 +108,7 @@ public static Task RunAsync()
// ==========================================================================================================
// The AI services are defined with the builder

var kernel7 = Kernel.Builder
var kernel7 = new KernelBuilder()
.WithAzureChatCompletionService(
deploymentName: azureOpenAIChatCompletionDeployment,
endpoint: azureOpenAIEndpoint,
Expand All @@ -121,7 +121,7 @@ public static Task RunAsync()
// The default behavior can be configured or a custom retry handler can be injected that will apply to all
// AI requests (when using the kernel).

var kernel8 = Kernel.Builder.WithRetryBasic(
var kernel8 = new KernelBuilder().WithRetryBasic(
new BasicRetryConfig
{
MaxRetryCount = 3,
Expand All @@ -147,11 +147,11 @@ public static Task RunAsync()
(ex, timespan, retryCount, _)
=> logger?.LogWarning(ex, "Error executing action [attempt {RetryCount} of 3], pausing {PausingMilliseconds}ms", retryCount, timespan.TotalMilliseconds));

var kernel9 = Kernel.Builder.WithHttpHandlerFactory(new PollyHttpRetryHandlerFactory(retryThreeTimesPolicy)).Build();
var kernel9 = new KernelBuilder().WithHttpHandlerFactory(new PollyHttpRetryHandlerFactory(retryThreeTimesPolicy)).Build();

var kernel10 = Kernel.Builder.WithHttpHandlerFactory(new PollyRetryThreeTimesFactory()).Build();
var kernel10 = new KernelBuilder().WithHttpHandlerFactory(new PollyRetryThreeTimesFactory()).Build();

var kernel11 = Kernel.Builder.WithHttpHandlerFactory(new MyCustomHandlerFactory()).Build();
var kernel11 = new KernelBuilder().WithHttpHandlerFactory(new MyCustomHandlerFactory()).Build();

return Task.CompletedTask;
}
Expand Down
2 changes: 1 addition & 1 deletion dotnet/samples/KernelSyntaxExamples/Example52_ApimAuth.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static async Task RunAsync()
.AddConsole();
});

var kernel = Kernel.Builder
var kernel = new KernelBuilder()
.WithLoggerFactory(loggerFactory)
.WithAIService<IChatCompletion>(TestConfiguration.AzureOpenAI.ChatDeploymentName, (loggerFactory) =>
new AzureChatCompletion(TestConfiguration.AzureOpenAI.ChatDeploymentName, openAIClient, loggerFactory))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public static async Task RunAsync()
return;
}

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithAzureChatCompletionService(
deploymentName: deploymentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static async Task RunAsync()
return;
}

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLoggerFactory(ConsoleLogger.LoggerFactory)
.WithAzureChatCompletionService(
deploymentName: chatDeploymentName,
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Connectors/Connectors.Memory.Chroma/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const string endpoint = "http://localhost:8000";

ChromaMemoryStore memoryStore = new(endpoint);

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLogger(logger)
.WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", "OPENAI_API_KEY")
.WithMemoryStorage(memoryStore)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Connectors/Connectors.Memory.Kusto/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using Kusto.Data;
var connectionString = new KustoConnectionStringBuilder("https://kvc123.eastus.kusto.windows.net").WithAadUserPromptAuthentication();
KustoMemoryStore memoryStore = new(connectionString, "MyDatabase");

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLogger(ConsoleLogger.Log)
.WithOpenAITextCompletionService(modelId: TestConfiguration.OpenAI.ModelId, apiKey: TestConfiguration.OpenAI.ApiKey)
.WithOpenAITextEmbeddingGenerationService(modelId: TestConfiguration.OpenAI.EmbeddingModelId,apiKey: TestConfiguration.OpenAI.ApiKey)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Connectors/Connectors.Memory.Milvus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ docker-compose up -d
```csharp
using MilvusMemoryStore memoryStore = new("localhost");

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLogger(logger)
.WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", "OPENAI_API_KEY")
.WithMemoryStorage(memoryStore)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Connectors/Connectors.Memory.Postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ NpgsqlDataSource dataSource = dataSourceBuilder.Build();

PostgresMemoryStore memoryStore = new PostgresMemoryStore(dataSource, vectorSize: 1536/*, schema: "public" */);

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLogger(ConsoleLogger.Logger)
.WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", Env.Var("OPENAI_API_KEY"))
.WithMemoryStorage(memoryStore)
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/Connectors/Connectors.Memory.Redis/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ ConnectionMultiplexer connectionMultiplexer = await ConnectionMultiplexer.Connec
IDatabase database = connectionMultiplexer.GetDatabase();
RedisMemoryStore memoryStore = new RedisMemoryStore(database, vectorSize: 1536);

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithLogger(ConsoleLogger.Logger)
.WithOpenAITextEmbeddingGenerationService("text-embedding-ada-002", Env.Var("OPENAI_API_KEY"))
.WithMemoryStorage(memoryStore)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class AIServicesOpenAIExtensionsTests
[Fact]
public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId()
{
KernelBuilder targetBuilder = Kernel.Builder;
KernelBuilder targetBuilder = new();
targetBuilder.WithAzureTextCompletionService("depl", "https://url", "key", "azure");
targetBuilder.WithAzureTextEmbeddingGenerationService("depl2", "https://url", "key", "azure");

Expand All @@ -28,7 +28,7 @@ public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId()
[Fact]
public void ItTellsIfAServiceIsAvailable()
{
KernelBuilder targetBuilder = Kernel.Builder;
KernelBuilder targetBuilder = new();
targetBuilder.WithAzureTextCompletionService("depl", "https://url", "key", serviceId: "azure");
targetBuilder.WithOpenAITextCompletionService("model", "apikey", serviceId: "oai");
targetBuilder.WithAzureTextEmbeddingGenerationService("depl2", "https://url2", "key", serviceId: "azure");
Expand All @@ -46,7 +46,7 @@ public void ItTellsIfAServiceIsAvailable()
public void ItCanOverwriteServices()
{
// Arrange
KernelBuilder targetBuilder = Kernel.Builder;
KernelBuilder targetBuilder = new();

// Act - Assert no exception occurs
targetBuilder.WithAzureTextCompletionService("dep", "https://localhost", "key", serviceId: "one");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public KernelSemanticFunctionExtensionsTests(ITestOutputHelper output)
[Fact]
public async Task ItSupportsFunctionCallsAsync()
{
var builder = Kernel.Builder
var builder = new KernelBuilder()
.WithAIService<ITextCompletion>(null, new RedirectTextCompletion(), true)
.WithLoggerFactory(this._logger);
IKernel target = builder.Build();
Expand All @@ -46,7 +46,7 @@ public async Task ItSupportsFunctionCallsAsync()
[Fact]
public async Task ItSupportsFunctionCallsWithInputAsync()
{
var builder = Kernel.Builder
var builder = new KernelBuilder()
.WithAIService<ITextCompletion>(null, new RedirectTextCompletion(), true)
.WithLoggerFactory(this._logger);
IKernel target = builder.Build();
Expand Down
2 changes: 1 addition & 1 deletion dotnet/src/IntegrationTests/Planners/PlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder
var builder = new KernelBuilder()
.WithLoggerFactory(this._loggerFactory)
.WithRetryBasic();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void CanCallToPlanFromXml()
AzureOpenAIConfiguration? azureOpenAIConfiguration = this._configuration.GetSection("AzureOpenAI").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIConfiguration);

IKernel kernel = Kernel.Builder
IKernel kernel = new KernelBuilder()
.WithRetryBasic()
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder.WithLoggerFactory(this._logger);
var builder = new KernelBuilder().WithLoggerFactory(this._logger);
builder.WithRetryBasic();

if (useChatModel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false, bool useChatModel =
AzureOpenAIConfiguration? azureOpenAIEmbeddingsConfiguration = this._configuration.GetSection("AzureOpenAIEmbeddings").Get<AzureOpenAIConfiguration>();
Assert.NotNull(azureOpenAIEmbeddingsConfiguration);

var builder = Kernel.Builder
var builder = new KernelBuilder()
.WithLoggerFactory(this._loggerFactory)
.WithRetryBasic();

Expand Down
Loading

0 comments on commit ba40209

Please sign in to comment.