-
Notifications
You must be signed in to change notification settings - Fork 4k
.Net: Add AssemblyAI file service Remake from #5964 #6406
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
RogerBarreto
merged 9 commits into
microsoft:feature-connectors-assemblyai
from
RogerBarreto:features/add-assembly-ai-fileclient
Jun 6, 2024
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e466b3f
Adding file assembly ai client
RogerBarreto fcd73d1
Remove AudioStreamContent and add tests for AssemblyAIFileService
Swimburger fec2bea
Add AssemblyAIFileService
Swimburger e11381f
Remove AudioStreamContent and add tests for AssemblyAIFileService
Swimburger b5146b0
Fix Solution File
RogerBarreto 5238234
Fix Solution File
RogerBarreto 26e207f
Adding missing suppressions file
RogerBarreto 9c21a9e
Addressing PR code
RogerBarreto aa4edd5
Fix IT
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
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
121 changes: 121 additions & 0 deletions
121
dotnet/src/Connectors/Connectors.AssemblyAI.UnitTests/Files/AssemblyAIFileServiceTests.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,121 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.AssemblyAI; | ||
using Microsoft.SemanticKernel.Connectors.AssemblyAI.Files; | ||
using SemanticKernel.Connectors.AssemblyAI.UnitTests; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.UnitTests.AssemblyAI; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="AssemblyAIAudioToTextService"/> class. | ||
/// </summary> | ||
public sealed class AssemblyAIFileServiceTests : IDisposable | ||
{ | ||
private const string UploadedFileUrl = "http://localhost/path/to/file.mp3"; | ||
|
||
private const string UploadFileResponseContent = | ||
$$""" | ||
{ | ||
"upload_url": "{{UploadedFileUrl}}" | ||
} | ||
"""; | ||
|
||
private readonly MultipleHttpMessageHandlerStub _messageHandlerStub; | ||
private readonly HttpClient _httpClient; | ||
|
||
public AssemblyAIFileServiceTests() | ||
{ | ||
this._messageHandlerStub = new MultipleHttpMessageHandlerStub(); | ||
this._httpClient = new HttpClient(this._messageHandlerStub, false); | ||
} | ||
|
||
[Fact] | ||
public void ConstructorWithHttpClientWorksCorrectly() | ||
{ | ||
// Arrange & Act | ||
var service = new AssemblyAIAudioToTextService("api-key", httpClient: this._httpClient); | ||
|
||
// Assert | ||
Assert.NotNull(service); | ||
} | ||
|
||
[Fact] | ||
public async Task UploadFileAsync() | ||
{ | ||
// Arrange | ||
var service = new AssemblyAIFileService("api-key", httpClient: this._httpClient); | ||
using var uploadFileResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); | ||
uploadFileResponse.Content = new StringContent(UploadFileResponseContent); | ||
using var transcribeResponse = new HttpResponseMessage(System.Net.HttpStatusCode.OK); | ||
this._messageHandlerStub.ResponsesToReturn = | ||
[ | ||
uploadFileResponse, | ||
]; | ||
using var stream = new BinaryData("data").ToStream(); | ||
|
||
// Act | ||
var result = await service.UploadAsync(stream).ConfigureAwait(true); | ||
|
||
// Assert | ||
Assert.NotNull(result); | ||
Assert.Null(result.Data); | ||
Assert.Equal(new Uri(UploadedFileUrl), result.Uri); | ||
} | ||
|
||
[Fact] | ||
public async Task HttpErrorShouldThrowWithErrorMessageAsync() | ||
{ | ||
// Arrange | ||
var service = new AssemblyAIFileService("api-key", httpClient: this._httpClient); | ||
using var uploadFileResponse = new HttpResponseMessage(System.Net.HttpStatusCode.InternalServerError); | ||
this._messageHandlerStub.ResponsesToReturn = | ||
[ | ||
uploadFileResponse | ||
]; | ||
using var stream = new BinaryData("data").ToStream(); | ||
// Act & Assert | ||
await Assert.ThrowsAsync<HttpOperationException>( | ||
async () => await service.UploadAsync(stream).ConfigureAwait(true) | ||
).ConfigureAwait(true); | ||
} | ||
|
||
[Fact] | ||
public async Task JsonErrorShouldThrowWithErrorMessageAsync() | ||
{ | ||
// Arrange | ||
var service = new AssemblyAIFileService("api-key", httpClient: this._httpClient); | ||
using var uploadFileResponse = new HttpResponseMessage(System.Net.HttpStatusCode.Unauthorized); | ||
const string ErrorMessage = "Bad API key"; | ||
uploadFileResponse.Content = new StringContent( | ||
$$""" | ||
{ | ||
"error": "{{ErrorMessage}}" | ||
} | ||
""", | ||
Encoding.UTF8, | ||
"application/json" | ||
); | ||
this._messageHandlerStub.ResponsesToReturn = | ||
[ | ||
uploadFileResponse | ||
]; | ||
using var stream = new BinaryData("data").ToStream(); | ||
|
||
// Act & Assert | ||
await Assert.ThrowsAsync<HttpOperationException>( | ||
async () => await service.UploadAsync(stream).ConfigureAwait(true) | ||
).ConfigureAwait(true); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
this._httpClient.Dispose(); | ||
this._messageHandlerStub.Dispose(); | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
...et/src/Connectors/Connectors.AssemblyAI.UnitTests/Files/AssemblyAIFilesExtensionsTests.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,62 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.Net.Http; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.SemanticKernel; | ||
using Microsoft.SemanticKernel.Connectors.AssemblyAI.Files; | ||
using Xunit; | ||
|
||
namespace SemanticKernel.Connectors.UnitTests.AssemblyAI; | ||
|
||
/// <summary> | ||
/// Unit tests for <see cref="AssemblyAIServiceCollectionExtensions"/> class. | ||
/// </summary> | ||
public sealed class AssemblyAIFilesExtensionsTests | ||
{ | ||
private const string ApiKey = "Test123"; | ||
private const string Endpoint = "http://localhost:1234/"; | ||
private const string ServiceId = "AssemblyAI"; | ||
|
||
[Fact] | ||
public void AddServiceToKernelBuilder() | ||
{ | ||
// Arrange & Act | ||
using var httpClient = new HttpClient(); | ||
var kernel = Kernel.CreateBuilder() | ||
.AddAssemblyAIFiles( | ||
apiKey: ApiKey, | ||
endpoint: new Uri(Endpoint), | ||
serviceId: ServiceId, | ||
httpClient: httpClient | ||
) | ||
.Build(); | ||
|
||
// Assert | ||
var service = kernel.GetRequiredService<AssemblyAIFileService>(); | ||
Assert.NotNull(service); | ||
Assert.IsType<AssemblyAIFileService>(service); | ||
|
||
service = kernel.GetRequiredService<AssemblyAIFileService>(ServiceId); | ||
Assert.NotNull(service); | ||
Assert.IsType<AssemblyAIFileService>(service); | ||
} | ||
|
||
[Fact] | ||
public void AddServiceToServiceCollection() | ||
{ | ||
// Arrange & Act | ||
var services = new ServiceCollection(); | ||
services.AddAssemblyAIFiles( | ||
apiKey: ApiKey, | ||
endpoint: new Uri(Endpoint), | ||
serviceId: ServiceId | ||
); | ||
using var provider = services.BuildServiceProvider(); | ||
|
||
// Assert | ||
var service = provider.GetRequiredKeyedService<AssemblyAIFileService>(ServiceId); | ||
Assert.NotNull(service); | ||
Assert.IsType<AssemblyAIFileService>(service); | ||
} | ||
} |
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
55 changes: 55 additions & 0 deletions
55
dotnet/src/Connectors/Connectors.AssemblyAI/Files/AssemblyAIFileService.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,55 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.SemanticKernel.Connectors.AssemblyAI.Client; | ||
using Microsoft.SemanticKernel.Http; | ||
|
||
namespace Microsoft.SemanticKernel.Connectors.AssemblyAI.Files; | ||
|
||
/// <summary> | ||
/// Service to upload files to AssemblyAI | ||
/// </summary> | ||
public sealed class AssemblyAIFileService | ||
{ | ||
private readonly AssemblyAIClient _client; | ||
|
||
/// <summary> | ||
/// Creates an instance of the <see cref="AssemblyAIFileService"/> with an AssemblyAI API key. | ||
/// </summary> | ||
/// <param name="apiKey">AssemblyAI API key</param> | ||
/// <param name="endpoint">Optional endpoint uri including the port where AssemblyAI server is hosted</param> | ||
/// <param name="httpClient">Optional HTTP client to be used for communication with the AssemblyAI API.</param> | ||
/// <param name="loggerFactory">Optional logger factory to be used for logging.</param> | ||
public AssemblyAIFileService( | ||
string apiKey, | ||
Uri? endpoint = null, | ||
HttpClient? httpClient = null, | ||
ILoggerFactory? loggerFactory = null | ||
) | ||
{ | ||
Verify.NotNullOrWhiteSpace(apiKey); | ||
this._client = new AssemblyAIClient( | ||
httpClient: HttpClientProvider.GetHttpClient(httpClient), | ||
endpoint: endpoint, | ||
apiKey: apiKey, | ||
logger: loggerFactory?.CreateLogger(this.GetType())); | ||
} | ||
|
||
/// <summary> | ||
/// Upload a file. | ||
/// </summary> | ||
/// <param name="stream">The file stream</param> | ||
/// <param name="cancellationToken">The <see cref="CancellationToken"/> to monitor for cancellation requests. The default is <see cref="CancellationToken.None"/>.</param> | ||
/// <returns>The file metadata.</returns> | ||
public async Task<AudioContent> UploadAsync(Stream stream, CancellationToken cancellationToken = default) | ||
{ | ||
Verify.NotNull(stream); | ||
var file = await this._client.UploadFileAsync(stream, cancellationToken).ConfigureAwait(false); | ||
return new AudioContent(new Uri(file, UriKind.Absolute)); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.