Skip to content
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

Feature serviceregistry #993

Merged
merged 37 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
01279de
AI Service Provider
May 8, 2023
0897181
bug and UT fixes
May 8, 2023
e9162a9
Improvements/fixes for service registry (#857)
SergeyMenshykh May 8, 2023
e5d7bd7
Merge main
May 8, 2023
c8a9a70
Improvements + fixes (#880)
SergeyMenshykh May 9, 2023
1a28455
A few examples of using SK SDK in DI/IoC container (#964)
SergeyMenshykh May 13, 2023
f0f76b9
Merge
SergeyMenshykh May 15, 2023
0b3bd35
Merge branch 'main' into feature-serviceregistry
lemillermicrosoft May 15, 2023
6425311
Merge branch 'main' into feature-serviceregistry
SergeyMenshykh May 15, 2023
ea7db57
Copilot chat changes are rolled back.
SergeyMenshykh May 15, 2023
194b926
Merge branch 'main' into feature-serviceregistry
SergeyMenshykh May 15, 2023
ad8c89f
Rolling back the change if change helps to fix failing test
SergeyMenshykh May 15, 2023
ab03c6b
Code doing unnecessary attempts to resolve the AI service during the …
SergeyMenshykh May 15, 2023
e1c7c6c
Merge with latetest bits from main branch
SergeyMenshykh May 15, 2023
b37388c
Addressing comments
SergeyMenshykh May 16, 2023
57da25c
Merge with latest bits from main
SergeyMenshykh May 19, 2023
f5fe11c
Merge branch 'main' into feature-serviceregistry
SergeyMenshykh May 19, 2023
e4a5bf2
rollback accidently committed file
SergeyMenshykh May 19, 2023
41e89ec
remove unnessesary changes
SergeyMenshykh May 19, 2023
16d555a
Merge branch 'main' into feature-serviceregistry
SergeyMenshykh May 19, 2023
77adc28
Merge branch 'main' into feature-serviceregistry
shawncal May 19, 2023
fe652bd
Merge with latest bits from main
SergeyMenshykh May 19, 2023
fc62128
Merge branch 'feature-serviceregistry' of https://github.com/microsof…
SergeyMenshykh May 19, 2023
2b06204
Merge branch 'main' into feature-serviceregistry
lemillermicrosoft May 19, 2023
e755af8
Merge branch 'main' into feature-serviceregistry
lemillermicrosoft May 19, 2023
cca83e2
Merge branch 'main' into feature-serviceregistry
shawncal May 19, 2023
9e9a6a1
fixing tests
May 19, 2023
a48a8fc
Merge remote-tracking branch 'origin/main' into feature-serviceregistry
May 19, 2023
fbc7821
integration test fixes
May 19, 2023
ef2fb0a
Merge remote-tracking branch 'origin/main' into feature-serviceregistry
May 19, 2023
20a9557
Merge remote-tracking branch 'origin/main' into feature-serviceregistry
May 20, 2023
b198356
Merge branch 'main' into feature-serviceregistry
shawncal May 20, 2023
a94bce7
Merge branch 'feature-serviceregistry' of https://github.com/microsof…
May 20, 2023
0f3dad6
format
May 20, 2023
2aed64d
Marking the old functionality for registering connectors as obsolete.
SergeyMenshykh May 22, 2023
01552c9
Merge branch 'main' into feature-serviceregistry
SergeyMenshykh May 22, 2023
9aab3a0
Merge remote-tracking branch 'origin/main' into feature-serviceregistry
May 22, 2023
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
Prev Previous commit
Next Next commit
Improvements/fixes for service registry (#857)
### Description
Series of improvements to align service registration extension methods
with the other kernel builder ones and remove the "default" suffix from
service provider Get*ServiceOrDefault method name to not expose
unnecessary details with code that may use it.
  • Loading branch information
SergeyMenshykh committed May 8, 2023
commit e9162a96f5bf7097fd5f99328dff15b9830e6677
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
namespace Microsoft.SemanticKernel;
#pragma warning restore IDE0130

public static class OpenAKernelBuilderExtensions
public static class OpenAIKernelBuilderExtensions
{
#region Text Completion

Expand All @@ -33,7 +33,7 @@ public static class OpenAKernelBuilderExtensions
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureTextCompletionService(this KernelBuilder builder,
public static KernelBuilder WithAzureTextCompletionService(this KernelBuilder builder,
string deploymentName,
string endpoint,
string apiKey,
Expand Down Expand Up @@ -67,7 +67,7 @@ public static KernelBuilder AddAzureTextCompletionService(this KernelBuilder bui
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureTextCompletionService(this KernelBuilder builder,
public static KernelBuilder WithAzureTextCompletionService(this KernelBuilder builder,
string deploymentName,
string endpoint,
TokenCredential credentials,
Expand Down Expand Up @@ -101,7 +101,7 @@ public static KernelBuilder AddAzureTextCompletionService(this KernelBuilder bui
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddOpenAITextCompletionService(this KernelBuilder builder,
public static KernelBuilder WithOpenAITextCompletionService(this KernelBuilder builder,
string modelId,
string apiKey,
string? orgId = null,
Expand Down Expand Up @@ -138,7 +138,7 @@ public static KernelBuilder AddOpenAITextCompletionService(this KernelBuilder bu
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureTextEmbeddingGenerationService(this KernelBuilder builder,
public static KernelBuilder WithAzureTextEmbeddingGenerationService(this KernelBuilder builder,
string deploymentName,
string endpoint,
string apiKey,
Expand Down Expand Up @@ -171,7 +171,7 @@ public static KernelBuilder AddAzureTextEmbeddingGenerationService(this KernelBu
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureTextEmbeddingGenerationService(this KernelBuilder builder,
public static KernelBuilder WithAzureTextEmbeddingGenerationService(this KernelBuilder builder,
string deploymentName,
string endpoint,
TokenCredential credential,
Expand Down Expand Up @@ -204,7 +204,7 @@ public static KernelBuilder AddAzureTextEmbeddingGenerationService(this KernelBu
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddOpenAITextEmbeddingGenerationService(this KernelBuilder builder,
public static KernelBuilder WithOpenAITextEmbeddingGenerationService(this KernelBuilder builder,
string modelId,
string apiKey,
string? orgId = null,
Expand Down Expand Up @@ -242,7 +242,7 @@ public static KernelBuilder AddOpenAITextEmbeddingGenerationService(this KernelB
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureChatCompletionService(this KernelBuilder builder,
public static KernelBuilder WithAzureChatCompletionService(this KernelBuilder builder,
string deploymentName,
string endpoint,
string apiKey,
Expand Down Expand Up @@ -284,7 +284,7 @@ public static KernelBuilder AddAzureChatCompletionService(this KernelBuilder bui
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddAzureChatCompletionService(this KernelBuilder builder,
public static KernelBuilder WithAzureChatCompletionService(this KernelBuilder builder,
string deploymentName,
string endpoint,
TokenCredential credentials,
Expand Down Expand Up @@ -326,7 +326,7 @@ public static KernelBuilder AddAzureChatCompletionService(this KernelBuilder bui
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddOpenAIChatCompletionService(this KernelBuilder builder,
public static KernelBuilder WithOpenAIChatCompletionService(this KernelBuilder builder,
string modelId,
string apiKey,
string? orgId = null,
Expand Down Expand Up @@ -369,7 +369,7 @@ public static KernelBuilder AddOpenAIChatCompletionService(this KernelBuilder bu
/// <param name="httpClient">Custom <see cref="HttpClient"/> for HTTP requests.</param>
/// <param name="logger">Application logger</param>
/// <returns>Self instance</returns>
public static KernelBuilder AddOpenAIImageGenerationService(this KernelBuilder builder,
public static KernelBuilder WithOpenAIImageGenerationService(this KernelBuilder builder,
string apiKey,
string? orgId = null,
string? serviceId = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
namespace SemanticKernel.Connectors.UnitTests.OpenAI;

/// <summary>
/// Unit tests of <see cref="OpenAKernelBuilderExtensions"/>.
/// Unit tests of <see cref="OpenAIKernelBuilderExtensions"/>.
/// </summary>
public class AIServicesOpenAIExtensionsTests
{
[Fact]
public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId()
{
KernelBuilder targetBuilder = Kernel.Builder;
targetBuilder.AddAzureTextCompletionService("depl", "https://url", "key", "azure");
targetBuilder.AddAzureTextEmbeddingGenerationService("depl2", "https://url", "key", "azure");
targetBuilder.WithAzureTextCompletionService("depl", "https://url", "key", "azure");
targetBuilder.WithAzureTextEmbeddingGenerationService("depl2", "https://url", "key", "azure");

IKernel targetKernel = targetBuilder.Build();
Assert.NotNull(targetKernel.GetService<ITextCompletion>("azure"));
Expand All @@ -28,10 +28,10 @@ public void ItSucceedsWhenAddingDifferentServiceTypeWithSameId()
public void ItTellsIfAServiceIsAvailable()
{
KernelBuilder targetBuilder = Kernel.Builder;
targetBuilder.AddAzureTextCompletionService("depl", "https://url", "key", serviceId: "azure");
targetBuilder.AddOpenAITextCompletionService("model", "apikey", serviceId: "oai");
targetBuilder.AddAzureTextEmbeddingGenerationService("depl2", "https://url2", "key", serviceId: "azure");
targetBuilder.AddOpenAITextEmbeddingGenerationService("model2", "apikey2", serviceId: "oai2");
targetBuilder.WithAzureTextCompletionService("depl", "https://url", "key", serviceId: "azure");
targetBuilder.WithOpenAITextCompletionService("model", "apikey", serviceId: "oai");
targetBuilder.WithAzureTextEmbeddingGenerationService("depl2", "https://url2", "key", serviceId: "azure");
targetBuilder.WithOpenAITextEmbeddingGenerationService("model2", "apikey2", serviceId: "oai2");

// Assert
IKernel targetKernel = targetBuilder.Build();
Expand All @@ -48,13 +48,13 @@ public void ItCanOverwriteServices()
KernelBuilder targetBuilder = Kernel.Builder;

// Act - Assert no exception occurs
targetBuilder.AddAzureTextCompletionService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.AddAzureTextCompletionService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.AddOpenAITextCompletionService("model", "key, serviceId: \"one\"");
targetBuilder.AddOpenAITextCompletionService("model", "key", serviceId: "one");
targetBuilder.AddAzureTextEmbeddingGenerationService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.AddAzureTextEmbeddingGenerationService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.AddOpenAITextEmbeddingGenerationService("model", "key", serviceId: "one");
targetBuilder.AddOpenAITextEmbeddingGenerationService("model", "key", serviceId: "one");
targetBuilder.WithAzureTextCompletionService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.WithAzureTextCompletionService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.WithOpenAITextCompletionService("model", "key, serviceId: \"one\"");
targetBuilder.WithOpenAITextCompletionService("model", "key", serviceId: "one");
targetBuilder.WithAzureTextEmbeddingGenerationService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.WithAzureTextEmbeddingGenerationService("dep", "https://localhost", "key", serviceId: "one");
targetBuilder.WithOpenAITextEmbeddingGenerationService("model", "key", serviceId: "one");
targetBuilder.WithOpenAITextEmbeddingGenerationService("model", "key", serviceId: "one");
SergeyMenshykh marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public async Task OpenAITestAsync(string prompt, string expectedAnswerContains)

IKernel target = Kernel.Builder
.WithLogger(this._logger)
.AddOpenAITextCompletionService(
.WithOpenAITextCompletionService(
serviceId: openAIConfiguration.ServiceId,
modelId: openAIConfiguration.ModelId,
apiKey: openAIConfiguration.ApiKey,
Expand All @@ -70,7 +70,7 @@ public async Task AzureOpenAITestAsync(string prompt, string expectedAnswerConta

IKernel target = Kernel.Builder
.WithLogger(this._logger)
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
serviceId: azureOpenAIConfiguration.ServiceId,
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
Expand Down Expand Up @@ -99,7 +99,7 @@ public async Task OpenAIHttpRetryPolicyTestAsync(string prompt, string expectedO
Assert.NotNull(openAIConfiguration);

IKernel target = Kernel.Builder
.AddOpenAITextCompletionService(
.WithOpenAITextCompletionService(
serviceId: openAIConfiguration.ServiceId,
modelId: openAIConfiguration.ModelId,
apiKey: "INVALID_KEY") // Use an invalid API key to force a 401 Unauthorized response
Expand All @@ -123,7 +123,7 @@ public async Task OpenAIHttpInvalidKeyShouldReturnErrorDetailAsync()

// Use an invalid API key to force a 401 Unauthorized response
IKernel target = Kernel.Builder
.AddOpenAITextCompletionService(
.WithOpenAITextCompletionService(
modelId: openAIConfiguration.ModelId,
apiKey: "INVALID_KEY",
serviceId: openAIConfiguration.ServiceId)
Expand All @@ -149,7 +149,7 @@ public async Task AzureOpenAIHttpInvalidKeyShouldReturnErrorDetailAsync()

IKernel target = Kernel.Builder
.WithLogger(this._testOutputHelper)
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
apiKey: "INVALID_KEY",
Expand All @@ -176,7 +176,7 @@ public async Task AzureOpenAIHttpExceededMaxTokensShouldReturnErrorDetailAsync()
// Arrange
IKernel target = Kernel.Builder
.WithLogger(this._testOutputHelper)
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
apiKey: azureOpenAIConfiguration.ApiKey,
Expand Down
4 changes: 2 additions & 2 deletions dotnet/src/IntegrationTests/Planning/PlanTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false)

var builder = Kernel.Builder
.WithLogger(this._logger)
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
apiKey: azureOpenAIConfiguration.ApiKey,
Expand All @@ -468,7 +468,7 @@ private IKernel InitializeKernel(bool useEmbeddings = false)
if (useEmbeddings)
{
builder
.AddAzureTextEmbeddingGenerationService(
.WithAzureTextEmbeddingGenerationService(
deploymentName: azureOpenAIEmbeddingsConfiguration.DeploymentName,
endpoint: azureOpenAIEmbeddingsConfiguration.Endpoint,
apiKey: azureOpenAIEmbeddingsConfiguration.ApiKey)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public void CanCallToPlanFromXml()
Assert.NotNull(azureOpenAIConfiguration);

IKernel kernel = Kernel.Builder
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
apiKey: azureOpenAIConfiguration.ApiKey,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ private IKernel InitializeKernel(bool useEmbeddings = false)

var builder = Kernel.Builder
.WithLogger(this._logger)
.AddAzureTextCompletionService(
.WithAzureTextCompletionService(
deploymentName: azureOpenAIConfiguration.DeploymentName,
endpoint: azureOpenAIConfiguration.Endpoint,
apiKey: azureOpenAIConfiguration.ApiKey,
setAsDefault: true);

if (useEmbeddings)
{
builder.AddAzureTextEmbeddingGenerationService(
builder.WithAzureTextEmbeddingGenerationService(
deploymentName: azureOpenAIEmbeddingsConfiguration.DeploymentName,
endpoint: azureOpenAIEmbeddingsConfiguration.Endpoint,
apiKey: azureOpenAIEmbeddingsConfiguration.ApiKey);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static class ChatCompletionServiceExtensions
/// <param name="serviceId">Optional identifier of the desired service.</param>
/// <returns>The completion service id matching the given id or the default.</returns>
/// <exception cref="KernelException">Thrown when no suitable service is found.</exception>
public static IChatCompletion GetChatCompletionServiceOrDefault(
public static IChatCompletion GetChatCompletionService(
this IAIServiceProvider services,
string? serviceId = null) => services.GetNamedServiceOrDefault<IChatCompletion>(serviceId)
string? serviceId = null) => services.GetService<IChatCompletion>(serviceId)
?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Chat completion service not found");

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public static class TextEmbeddingServiceExtensions
/// <param name="serviceId">Optional identifier of the desired service.</param>
/// <returns>The embedding service matching the given id or the default service.</returns>
/// <exception cref="KernelException">Thrown when no suitable service is found.</exception>
public static ITextEmbeddingGeneration GetTextEmbeddingServiceOrDefault(
public static ITextEmbeddingGeneration GetTextEmbeddingService(
this IAIServiceProvider services,
string? serviceId = null)
=> services.GetNamedServiceOrDefault<ITextEmbeddingGeneration>(serviceId)
=> services.GetService<ITextEmbeddingGeneration>(serviceId)
?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Text embedding service not available");

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public static class ImageGenerationServiceExtensions
/// <param name="serviceId">Optional identifier of the desired service.</param>
/// <returns>The <see cref="IImageGeneration"/> id matching the given id or the default.</returns>
/// <exception cref="KernelException">Thrown when no suitable service is found.</exception>
public static IImageGeneration GetImageGenerationServiceOrDefault(
public static IImageGeneration GetImageGenerationService(
this IAIServiceProvider services,
string? serviceId = null) => services.GetNamedServiceOrDefault<IImageGeneration>(serviceId)
string? serviceId = null) => services.GetService<IImageGeneration>(serviceId)
?? throw new KernelException(KernelException.ErrorCodes.ServiceNotFound, "Image generation service not found");

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,6 @@
namespace Microsoft.SemanticKernel.Services;
internal static class AIServiceProviderExtensions
{
/// <summary>
/// Get the service matching the given id or the default if an id is not provided or not found.
/// </summary>
/// <typeparam name="T">The type of the service.</typeparam>
/// <param name="serviceProvider">The service provider.</param>
/// <param name="serviceId">Optional identifier of the desired service.</param>
/// <returns>The service matching the given id, the default service, or null.</returns>
public static T? GetNamedServiceOrDefault<T>(
this IAIServiceProvider serviceProvider,
string? serviceId = null) where T : IAIService
{
if ((serviceId != null)
&& serviceProvider.TryGetService<T>(serviceId, out var namedService))
{
return namedService;
}

return serviceProvider.GetService<T>();
}


/// <summary>
/// Tries to get the service of the specified type and name, and returns a value indicating whether the operation succeeded.
/// </summary>
Expand Down
10 changes: 2 additions & 8 deletions dotnet/src/SemanticKernel/KernelBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,7 @@ public KernelBuilder Configure(Action<KernelConfig> configure)
/// Adds a <typeparamref name="TService"/> instance to the services collection
/// </summary>
/// <param name="instance">The <typeparamref name="TService"/> instance.</param>
/// <param name="setAsDefault">Optional: set as the default AI service for type <typeparamref name="TService"/></param>
public KernelBuilder WithDefaultAIService<TService>(
TService instance,
bool setAsDefault = false) where TService : IAIService
public KernelBuilder WithDefaultAIService<TService>(TService instance) where TService : IAIService
{
this._aiServices.SetService<TService>(instance);
return this;
Expand All @@ -174,10 +171,7 @@ public KernelBuilder WithAIService<TService>(
/// Adds a <typeparamref name="TService"/> factory method to the services collection
/// </summary>
/// <param name="factory">The factory method that creates the AI service instances of type <typeparamref name="TService"/>.</param>
/// <param name="setAsDefault">Optional: set as the default AI service for type <typeparamref name="TService"/></param>
public KernelBuilder WithDefaultAIService<TService>(
Func<TService> factory,
bool setAsDefault = false) where TService : IAIService
public KernelBuilder WithDefaultAIService<TService>(Func<TService> factory) where TService : IAIService
{
this._aiServices.SetService<TService>(factory);
return this;
Expand Down
Loading