Skip to content

Commit

Permalink
Merge pull request #91 from matthewbolanos/main
Browse files Browse the repository at this point in the history
Update to RC3
  • Loading branch information
matthewbolanos authored Dec 6, 2023
2 parents 429f610 + c57c344 commit d1c629e
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 57 deletions.
2 changes: 1 addition & 1 deletion sk-csharp-console-chat/ConsoleChat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Task StartAsync(CancellationToken cancellationToken)
private async Task ExecuteAsync(CancellationToken cancellationToken)
{
ChatHistory chatMessages = [];
IChatCompletionService chatCompletionService = this._kernel.GetService<IChatCompletionService>();
IChatCompletionService chatCompletionService = this._kernel.GetRequiredService<IChatCompletionService>();

// Loop till we are cancelled
while (!cancellationToken.IsCancellationRequested)
Expand Down
18 changes: 8 additions & 10 deletions sk-csharp-console-chat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,14 @@
// Add kernel settings to the host builder
services
.AddSingleton<KernelSettings>(kernelSettings)
.AddTransient<Kernel>(serviceProvider => new KernelBuilder()
.WithServices(serviceCollection =>
{
serviceCollection
.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Information))
.AddChatCompletionService(kernelSettings);
})
.WithPlugins(plugins => plugins.AddPluginFromObject<LightPlugin>())
.Build()
)
.AddTransient<Kernel>(serviceProvider => {
KernelBuilder builder = new();
builder.Services.AddLogging(c => c.AddDebug().SetMinimumLevel(LogLevel.Information));
builder.Services.AddChatCompletionService(kernelSettings);
builder.Plugins.AddFromType<LightPlugin>();

return builder.Build();
})
.AddHostedService<ConsoleChat>();
});

Expand Down
2 changes: 1 addition & 1 deletion sk-csharp-console-chat/sk-csharp-console-chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Resilience" Version="8.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc3" />
</ItemGroup>

<ItemGroup>
Expand Down
20 changes: 7 additions & 13 deletions sk-csharp-hello-world/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Reflection;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AI.ChatCompletion;
Expand All @@ -8,19 +9,12 @@

var kernelSettings = KernelSettings.LoadSettings();

using var loggerFactory = LoggerFactory.Create(b =>
{
b.SetMinimumLevel(kernelSettings.LogLevel ?? LogLevel.Warning)
.AddConsole()
.AddDebug();
});

Kernel kernel = new KernelBuilder()
.WithLoggerFactory(loggerFactory)
.WithChatCompletionService(kernelSettings)
.WithPlugins(plugins => plugins.AddPluginFromObject<LightPlugin>())
.Build();
KernelBuilder builder = new();
builder.Services.AddLogging(c => c.SetMinimumLevel(LogLevel.Information).AddDebug());
builder.Services.AddChatCompletionService(kernelSettings);
builder.Plugins.AddFromType<LightPlugin>();

Kernel kernel = builder.Build();

// Load prompt from resource
using StreamReader reader = new(Assembly.GetExecutingAssembly().GetManifestResourceStream("prompts.Chat.yaml")!);
Expand All @@ -29,8 +23,8 @@
promptTemplateFactory: new HandlebarsPromptTemplateFactory()
);

// Create the chat history
ChatHistory chatMessages = [];
IChatCompletionService chatCompletionService = kernel.GetService<IChatCompletionService>();

// Loop till we are cancelled
while (true)
Expand Down
29 changes: 0 additions & 29 deletions sk-csharp-hello-world/config/KernelBuilderExtensions.cs

This file was deleted.

30 changes: 30 additions & 0 deletions sk-csharp-hello-world/config/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;

internal static class ServiceCollectionExtensions
{
/// <summary>
/// Adds a chat completion service to the list. It can be either an OpenAI or Azure OpenAI backend service.
/// </summary>
/// <param name="kernelBuilder"></param>
/// <param name="kernelSettings"></param>
/// <exception cref="ArgumentException"></exception>
internal static IServiceCollection AddChatCompletionService(this IServiceCollection serviceCollection, KernelSettings kernelSettings)
{
switch (kernelSettings.ServiceType.ToUpperInvariant())
{
case ServiceTypes.AzureOpenAI:
serviceCollection = serviceCollection.AddAzureOpenAIChatCompletion(kernelSettings.DeploymentId, kernelSettings.ModelId, endpoint: kernelSettings.Endpoint, apiKey: kernelSettings.ApiKey, serviceId: kernelSettings.ServiceId);
break;

case ServiceTypes.OpenAI:
serviceCollection = serviceCollection.AddOpenAIChatCompletion(modelId: kernelSettings.ModelId, apiKey: kernelSettings.ApiKey, orgId: kernelSettings.OrgId, serviceId: kernelSettings.ServiceId);
break;

default:
throw new ArgumentException($"Invalid service type value: {kernelSettings.ServiceType}");
}

return serviceCollection;
}
}
6 changes: 3 additions & 3 deletions sk-csharp-hello-world/sk-csharp-hello-world.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="6.0.1" />
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="6.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.SemanticKernel.PromptTemplate.Handlebars" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.SemanticKernel.Yaml" Version="1.0.0-rc2" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.PromptTemplate.Handlebars" Version="1.0.0-rc3" />
<PackageReference Include="Microsoft.SemanticKernel.Yaml" Version="1.0.0-rc3" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d1c629e

Please sign in to comment.