Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<IsTestProject>true</IsTestProject>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;CA2007,CA1806,CA1869,CA1861,IDE0300,VSTHRD111</NoWarn>
<NoWarn>$(NoWarn);SKEXP0001;SKEXP0010;CA2007,CA1806,CA1869,CA1861,IDE0300,VSTHRD111,IDE1006</NoWarn>

</PropertyGroup>

Expand All @@ -27,7 +27,7 @@
</ItemGroup>

<ItemGroup>
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/test/HttpMessageHandlerStub.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
<Compile Include="$(RepoRoot)/dotnet/src/InternalUtilities/test/*.cs" Link="%(RecursiveDir)%(Filename)%(Extension)" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;

namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Extensions;
Expand All @@ -21,8 +22,8 @@ public sealed class AzureOpenAIServiceCollectionExtensionsTests
[Theory]
[InlineData(InitializationType.ApiKey)]
[InlineData(InitializationType.TokenCredential)]
[InlineData(InitializationType.OpenAIClientInline)]
[InlineData(InitializationType.OpenAIClientInServiceProvider)]
[InlineData(InitializationType.ClientInline)]
[InlineData(InitializationType.ClientInServiceProvider)]
public void ServiceCollectionAddAzureOpenAIChatCompletionAddsValidService(InitializationType type)
{
// Arrange
Expand All @@ -37,8 +38,8 @@ public void ServiceCollectionAddAzureOpenAIChatCompletionAddsValidService(Initia
{
InitializationType.ApiKey => builder.Services.AddAzureOpenAIChatCompletion("deployment-name", "https://endpoint", "api-key"),
InitializationType.TokenCredential => builder.Services.AddAzureOpenAIChatCompletion("deployment-name", "https://endpoint", credentials),
InitializationType.OpenAIClientInline => builder.Services.AddAzureOpenAIChatCompletion("deployment-name", client),
InitializationType.OpenAIClientInServiceProvider => builder.Services.AddAzureOpenAIChatCompletion("deployment-name"),
InitializationType.ClientInline => builder.Services.AddAzureOpenAIChatCompletion("deployment-name", client),
InitializationType.ClientInServiceProvider => builder.Services.AddAzureOpenAIChatCompletion("deployment-name"),
_ => builder.Services
};

Expand All @@ -52,12 +53,47 @@ public void ServiceCollectionAddAzureOpenAIChatCompletionAddsValidService(Initia

#endregion

#region Text embeddings

[Theory]
[InlineData(InitializationType.ApiKey)]
[InlineData(InitializationType.TokenCredential)]
[InlineData(InitializationType.ClientInline)]
[InlineData(InitializationType.ClientInServiceProvider)]
public void ServiceCollectionAddAzureOpenAITextEmbeddingGenerationAddsValidService(InitializationType type)
{
// Arrange
var credentials = DelegatedTokenCredential.Create((_, _) => new AccessToken());
var client = new AzureOpenAIClient(new Uri("http://localhost"), "key");
var builder = Kernel.CreateBuilder();

builder.Services.AddSingleton<AzureOpenAIClient>(client);

// Act
IServiceCollection collection = type switch
{
InitializationType.ApiKey => builder.Services.AddAzureOpenAITextEmbeddingGeneration("deployment-name", "https://endpoint", "api-key"),
InitializationType.TokenCredential => builder.Services.AddAzureOpenAITextEmbeddingGeneration("deployment-name", "https://endpoint", credentials),
InitializationType.ClientInline => builder.Services.AddAzureOpenAITextEmbeddingGeneration("deployment-name", client),
InitializationType.ClientInServiceProvider => builder.Services.AddAzureOpenAITextEmbeddingGeneration("deployment-name"),
_ => builder.Services
};

// Assert
var service = builder.Build().GetRequiredService<ITextEmbeddingGenerationService>();

Assert.NotNull(service);
Assert.True(service is AzureOpenAITextEmbeddingGenerationService);
}

#endregion

public enum InitializationType
{
ApiKey,
TokenCredential,
OpenAIClientInline,
OpenAIClientInServiceProvider,
OpenAIClientEndpoint,
ClientInline,
ClientInServiceProvider,
ClientEndpoint,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.TextGeneration;

namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Extensions;
Expand Down Expand Up @@ -52,6 +53,41 @@ public void KernelBuilderAddAzureOpenAIChatCompletionAddsValidService(Initializa

#endregion

#region Text embeddings

[Theory]
[InlineData(InitializationType.ApiKey)]
[InlineData(InitializationType.TokenCredential)]
[InlineData(InitializationType.OpenAIClientInline)]
[InlineData(InitializationType.OpenAIClientInServiceProvider)]
public void KernelBuilderAddAzureOpenAITextEmbeddingGenerationAddsValidService(InitializationType type)
{
// Arrange
var credentials = DelegatedTokenCredential.Create((_, _) => new AccessToken());
var client = new AzureOpenAIClient(new Uri("http://localhost"), "key");
var builder = Kernel.CreateBuilder();

builder.Services.AddSingleton<AzureOpenAIClient>(client);

// Act
builder = type switch
{
InitializationType.ApiKey => builder.AddAzureOpenAITextEmbeddingGeneration("deployment-name", "https://endpoint", "api-key"),
InitializationType.TokenCredential => builder.AddAzureOpenAITextEmbeddingGeneration("deployment-name", "https://endpoint", credentials),
InitializationType.OpenAIClientInline => builder.AddAzureOpenAITextEmbeddingGeneration("deployment-name", client),
InitializationType.OpenAIClientInServiceProvider => builder.AddAzureOpenAITextEmbeddingGeneration("deployment-name"),
_ => builder
};

// Assert
var service = builder.Build().GetRequiredService<ITextEmbeddingGenerationService>();

Assert.NotNull(service);
Assert.True(service is AzureOpenAITextEmbeddingGenerationService);
}

#endregion

public enum InitializationType
{
ApiKey,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.ClientModel;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Azure.AI.OpenAI;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.AzureOpenAI;
using Microsoft.SemanticKernel.Services;

namespace SemanticKernel.Connectors.AzureOpenAI.UnitTests.Services;

/// <summary>
/// Unit tests for <see cref="AzureOpenAITextEmbeddingGenerationService"/> class.
/// </summary>
public class AzureOpenAITextEmbeddingGenerationServiceTests
{
[Fact]
public void ItCanBeInstantiatedAndPropertiesSetAsExpected()
{
// Arrange
var sut = new AzureOpenAITextEmbeddingGenerationService("deployment-name", "https://endpoint", "api-key", modelId: "model", dimensions: 2);
var sutWithAzureOpenAIClient = new AzureOpenAITextEmbeddingGenerationService("deployment-name", new AzureOpenAIClient(new Uri("https://endpoint"), new ApiKeyCredential("apiKey")), modelId: "model", dimensions: 2);

// Assert
Assert.NotNull(sut);
Assert.NotNull(sutWithAzureOpenAIClient);
Assert.Equal("model", sut.Attributes[AIServiceExtensions.ModelIdKey]);
Assert.Equal("model", sutWithAzureOpenAIClient.Attributes[AIServiceExtensions.ModelIdKey]);
}

[Fact]
public async Task ItGetEmbeddingsAsyncReturnsEmptyWhenProvidedDataIsEmpty()
{
// Arrange
var sut = new AzureOpenAITextEmbeddingGenerationService("deployment-name", "https://endpoint", "api-key");

// Act
var result = await sut.GenerateEmbeddingsAsync([], null, CancellationToken.None);

// Assert
Assert.Empty(result);
}

[Fact]
public async Task GetEmbeddingsAsyncReturnsEmptyWhenProvidedDataIsWhitespace()
{
// Arrange
using HttpMessageHandlerStub handler = new()
{
ResponseToReturn = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(File.ReadAllText("./TestData/text-embeddings-response.txt"))
}
};
using HttpClient client = new(handler);

var sut = new AzureOpenAITextEmbeddingGenerationService("deployment-name", "https://endpoint", "api-key", httpClient: client);

// Act
var result = await sut.GenerateEmbeddingsAsync(["test"], null, CancellationToken.None);

// Assert
Assert.Single(result);
Assert.Equal(4, result[0].Length);
}

[Fact]
public async Task ItThrowsIfNumberOfResultsDiffersFromInputsAsync()
{
// Arrange
using HttpMessageHandlerStub handler = new()
{
ResponseToReturn = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new StringContent(File.ReadAllText("./TestData/text-embeddings-multiple-response.txt"))
}
};
using HttpClient client = new(handler);

var sut = new AzureOpenAITextEmbeddingGenerationService("deployment-name", "https://endpoint", "api-key", httpClient: client);

// Act & Assert
await Assert.ThrowsAsync<KernelException>(async () => await sut.GenerateEmbeddingsAsync(["test"], null, CancellationToken.None));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": "zcyMP83MDEAzM1NAzcyMQA=="
},
{
"object": "embedding",
"index": 1,
"embedding": "zcyMP83MDEAzM1NAzcyMQA=="
}
],
"model": "text-embedding-ada-002",
"usage": {
"prompt_tokens": 7,
"total_tokens": 7
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"object": "list",
"data": [
{
"object": "embedding",
"index": 0,
"embedding": "zcyMP83MDEAzM1NAzcyMQA=="
}
],
"model": "text-embedding-ada-002",
"usage": {
"prompt_tokens": 7,
"total_tokens": 7
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,10 @@
<Description>Semantic Kernel connectors for Azure OpenAI. Contains clients for text generation, chat completion, embedding and DALL-E text to image.</Description>
</PropertyGroup>

<ItemGroup>
<Compile Remove="Services\AzureOpenAITextEmbeddingGenerationService.cs" />
</ItemGroup>

<ItemGroup>
<InternalsVisibleTo Include="SemanticKernel.Connectors.AzureOpenAI.UnitTests" />
</ItemGroup>

<ItemGroup>
<None Include="Services\AzureOpenAITextEmbeddingGenerationService.cs" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Azure.AI.OpenAI" VersionOverride="2.0.0-beta.2" />
</ItemGroup>
Expand Down
Loading