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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SergeyMenshykh , moved this to be an Utility and the test was moved to SemanticKernel.UnitTests, removed the test from both connectors.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please consider doing it in a separate PR next time to minimize PR sizes and to have them reviewed faster.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
</ItemGroup>

<ItemGroup>
<Using Include="SemanticKernel.Connectors.OpenAI.UnitTests"/>
<Using Include="SemanticKernel.Connectors.OpenAI.UnitTests" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft. All rights reserved.

using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AudioToText;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextToAudio;
using Microsoft.SemanticKernel.TextToImage;
using OpenAI;
using Xunit;
Expand Down Expand Up @@ -70,4 +72,64 @@ public void ItCanAddTextToImageServiceWithOpenAIClient()
// Assert
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
}

[Fact]
public void ItCanAddTextToAudioService()
{
// Arrange
var sut = Kernel.CreateBuilder();

// Act
var service = sut.AddOpenAITextToAudio("model", "key")
.Build()
.GetRequiredService<ITextToAudioService>();

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

[Fact]
public void ItCanAddTextToAudioServiceWithOpenAIClient()
{
// Arrange
var sut = Kernel.CreateBuilder();

// Act
var service = sut.AddOpenAITextToAudio("model", new OpenAIClient("key"))
.Build()
.GetRequiredService<ITextToAudioService>();

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

[Fact]
public void ItCanAddAudioToTextService()
{
// Arrange
var sut = Kernel.CreateBuilder();

// Act
var service = sut.AddOpenAIAudioToText("model", "key")
.Build()
.GetRequiredService<IAudioToTextService>();

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

[Fact]
public void ItCanAddAudioToTextServiceWithOpenAIClient()
{
// Arrange
var sut = Kernel.CreateBuilder();

// Act
var service = sut.AddOpenAIAudioToText("model", new OpenAIClient("key"))
.Build()
.GetRequiredService<IAudioToTextService>();

// Assert
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

using Microsoft.Extensions.DependencyInjection;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.AudioToText;
using Microsoft.SemanticKernel.Embeddings;
using Microsoft.SemanticKernel.Services;
using Microsoft.SemanticKernel.TextToAudio;
using Microsoft.SemanticKernel.TextToImage;
using OpenAI;
using Xunit;
Expand Down Expand Up @@ -71,4 +73,64 @@ public void ItCanAddImageToTextServiceWithOpenAIClient()
// Assert
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
}

[Fact]
public void ItCanAddTextToAudioService()
{
// Arrange
var sut = new ServiceCollection();

// Act
var service = sut.AddOpenAITextToAudio("model", "key")
.BuildServiceProvider()
.GetRequiredService<ITextToAudioService>();

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

[Fact]
public void ItCanAddTextToAudioServiceWithOpenAIClient()
{
// Arrange
var sut = new ServiceCollection();

// Act
var service = sut.AddOpenAITextToAudio("model", new OpenAIClient("key"))
.BuildServiceProvider()
.GetRequiredService<ITextToAudioService>();

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

[Fact]
public void ItCanAddAudioToTextService()
{
// Arrange
var sut = new ServiceCollection();

// Act
var service = sut.AddOpenAIAudioToText("model", "key")
.BuildServiceProvider()
.GetRequiredService<IAudioToTextService>();

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

[Fact]
public void ItCanAddAudioToTextServiceWithOpenAIClient()
{
// Arrange
var sut = new ServiceCollection();

// Act
var service = sut.AddOpenAIAudioToText("model", new OpenAIClient("key"))
.BuildServiceProvider()
.GetRequiredService<IAudioToTextService>();

// Assert
Assert.Equal("model", service.Attributes[AIServiceExtensions.ModelIdKey]);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// Copyright (c) Microsoft. All rights reserved.

using System;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Moq;
using OpenAI;
using Xunit;
using static Microsoft.SemanticKernel.Connectors.OpenAI.OpenAIAudioToTextExecutionSettings;

namespace SemanticKernel.Connectors.OpenAI.UnitTests.Services;

/// <summary>
/// Unit tests for <see cref="OpenAIAudioToTextService"/> class.
/// </summary>
public sealed class OpenAIAudioToTextServiceTests : IDisposable
{
private readonly HttpMessageHandlerStub _messageHandlerStub;
private readonly HttpClient _httpClient;
private readonly Mock<ILoggerFactory> _mockLoggerFactory;

public OpenAIAudioToTextServiceTests()
{
this._messageHandlerStub = new HttpMessageHandlerStub();
this._httpClient = new HttpClient(this._messageHandlerStub, false);
this._mockLoggerFactory = new Mock<ILoggerFactory>();
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConstructorWithApiKeyWorksCorrectly(bool includeLoggerFactory)
{
// Arrange & Act
var service = includeLoggerFactory ?
new OpenAIAudioToTextService("model-id", "api-key", "organization", loggerFactory: this._mockLoggerFactory.Object) :
new OpenAIAudioToTextService("model-id", "api-key", "organization");

// Assert
Assert.NotNull(service);
Assert.Equal("model-id", service.Attributes["ModelId"]);
}

[Theory]
[InlineData(true)]
[InlineData(false)]
public void ConstructorWithOpenAIClientWorksCorrectly(bool includeLoggerFactory)
{
// Arrange & Act
var client = new OpenAIClient("key");
var service = includeLoggerFactory ?
new OpenAIAudioToTextService("model-id", client, loggerFactory: this._mockLoggerFactory.Object) :
new OpenAIAudioToTextService("model-id", client);

// Assert
Assert.NotNull(service);
Assert.Equal("model-id", service.Attributes["ModelId"]);
}

[Theory]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Default }, "0")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Word }, "word")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Segment }, "segment")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Segment, TimeStampGranularities.Word }, "word", "segment")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Word, TimeStampGranularities.Segment }, "word", "segment")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Default, TimeStampGranularities.Word }, "word", "0")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Word, TimeStampGranularities.Default }, "word", "0")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Default, TimeStampGranularities.Segment }, "segment", "0")]
[InlineData(new TimeStampGranularities[] { TimeStampGranularities.Segment, TimeStampGranularities.Default }, "segment", "0")]
public async Task GetTextContentGranularitiesWorksAsync(TimeStampGranularities[] granularities, params string[] expectedGranularities)
{
// Arrange
var service = new OpenAIAudioToTextService("model-id", "api-key", httpClient: this._httpClient);
this._messageHandlerStub.ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("Test audio-to-text response")
};

// Act
var settings = new OpenAIAudioToTextExecutionSettings("file.mp3") { Granularities = granularities };
var result = await service.GetTextContentsAsync(new AudioContent(new BinaryData("data"), mimeType: null), settings);

// Assert
Assert.NotNull(this._messageHandlerStub.RequestContent);
Assert.NotNull(result);

var multiPartData = Encoding.UTF8.GetString(this._messageHandlerStub.RequestContent!);
var multiPartBreak = multiPartData.Substring(0, multiPartData.IndexOf("\r\n", StringComparison.OrdinalIgnoreCase));

foreach (var granularity in expectedGranularities)
{
var expectedMultipart = $"{granularity}\r\n{multiPartBreak}";
Assert.Contains(expectedMultipart, multiPartData);
}
}

[Fact]
public async Task GetTextContentByDefaultWorksCorrectlyAsync()
{
// Arrange
var service = new OpenAIAudioToTextService("model-id", "api-key", "organization", null, this._httpClient);
this._messageHandlerStub.ResponseToReturn = new HttpResponseMessage(System.Net.HttpStatusCode.OK)
{
Content = new StringContent("Test audio-to-text response")
};

// Act
var result = await service.GetTextContentsAsync(new AudioContent(new BinaryData("data"), mimeType: null), new OpenAIAudioToTextExecutionSettings("file.mp3"));

// Assert
Assert.NotNull(result);
Assert.Equal("Test audio-to-text response", result[0].Text);
}

[Fact]
public async Task GetTextContentsDoesLogActionAsync()
{
// Assert
var modelId = "whisper-1";
var logger = new Mock<ILogger<OpenAITextToAudioService>>();
logger.Setup(l => l.IsEnabled(It.IsAny<LogLevel>())).Returns(true);

this._mockLoggerFactory.Setup(x => x.CreateLogger(It.IsAny<string>())).Returns(logger.Object);

// Arrange
var sut = new OpenAIAudioToTextService(modelId, "apiKey", httpClient: this._httpClient, loggerFactory: this._mockLoggerFactory.Object);

// Act
await sut.GetTextContentsAsync(new(new byte[] { 0x01, 0x02 }, "text/plain"));

// Assert
logger.VerifyLog(LogLevel.Information, $"Action: {nameof(OpenAIAudioToTextService.GetTextContentsAsync)}. OpenAI Model ID: {modelId}.", Times.Once());
}

public void Dispose()
{
this._httpClient.Dispose();
this._messageHandlerStub.Dispose();
}
}
Loading