-
Notifications
You must be signed in to change notification settings - Fork 4.7k
.Net: OpenAI V2 - Migrate Audio Services Phase 04 #7029
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
Merged
Roger Barreto (rogerbarreto)
merged 18 commits into
microsoft:feature-connectors-openai
from
rogerbarreto:features/openai-v2-phase04
Jul 2, 2024
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
40400f2
WIP
rogerbarreto 1109159
WIP
rogerbarreto 487e1a4
Updating tests
rogerbarreto 7e85f14
Add missing UTs
rogerbarreto 6d12918
Complete UT + Move Utility to SK.UT
rogerbarreto c34024a
Audio to Text, Text to Audio Integration Tests
rogerbarreto 0fa58c5
Remove extra space in filename
rogerbarreto 2ab5d59
Removing nonused image test data
rogerbarreto 4ed0eb3
Address PR comments
rogerbarreto 4eddfdd
Address warnings and feedback
rogerbarreto 2d06071
Granularities is single
rogerbarreto 3b09b69
Fix granularities as a bitwise operation
rogerbarreto 1fd1660
Add missing UT
rogerbarreto dca02ad
Typo fix
rogerbarreto f731941
Fix warnings
rogerbarreto 710255e
Exclude utility from code coverage
rogerbarreto 98612b8
Merge branch 'feature-connectors-openai' of https://github.com/micros…
rogerbarreto d39bda8
Remove extra UT for shared from AzureOpenAI Connector
rogerbarreto File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 0 additions & 53 deletions
53
.../Connectors/Connectors.AzureOpenAI.UnitTests/Core/ClientResultExceptionExtensionsTests.cs
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
...et/src/Connectors/Connectors.OpenAIV2.UnitTests/Services/OpenAIAudioToTextServiceTests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.