forked from RageAgainstThePixel/OpenAI-DotNet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
OpenAI-DotNet 5.1.0 (RageAgainstThePixel#37)
- Closes RageAgainstThePixel#34 adds Audio Endpoint to support - audio speech to text - audio translation - updated unit tests
- Loading branch information
1 parent
3bf166f
commit ee6cac0
Showing
17 changed files
with
540 additions
and
13 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
This file contains 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 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 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 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,35 @@ | ||
using NUnit.Framework; | ||
using OpenAI.Audio; | ||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI.Tests | ||
{ | ||
internal class TestFixture_07_Audio | ||
{ | ||
[Test] | ||
public async Task Test_1_Transcription() | ||
{ | ||
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv()); | ||
Assert.IsNotNull(api.AudioEndpoint); | ||
var transcriptionAudio = Path.GetFullPath("..\\..\\..\\Assets\\T3mt39YrlyLoq8laHSdf.mp3"); | ||
var request = new AudioTranscriptionRequest(transcriptionAudio, language: "en"); | ||
var result = await api.AudioEndpoint.CreateTranscriptionAsync(request); | ||
Assert.IsNotNull(result); | ||
Console.WriteLine(result); | ||
} | ||
|
||
[Test] | ||
public async Task Test_2_Translation() | ||
{ | ||
var api = new OpenAIClient(OpenAIAuthentication.LoadFromEnv()); | ||
Assert.IsNotNull(api.AudioEndpoint); | ||
var translationAudio = Path.GetFullPath("..\\..\\..\\Assets\\Ja-botchan_1-1_1-2.mp3"); | ||
var request = new AudioTranslationRequest(Path.GetFullPath(translationAudio)); | ||
var result = await api.AudioEndpoint.CreateTranslationAsync(request); | ||
Assert.IsNotNull(result); | ||
Console.WriteLine(result); | ||
} | ||
} | ||
} |
This file contains 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 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 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 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,112 @@ | ||
using System.IO; | ||
using System.Net.Http; | ||
using System.Text.Json; | ||
using System.Text.Json.Serialization; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace OpenAI.Audio | ||
{ | ||
/// <summary> | ||
/// Speech to text. | ||
/// </summary> | ||
public sealed class AudioEndpoint : BaseEndPoint | ||
{ | ||
private class AudioResponse | ||
{ | ||
public AudioResponse(string text) | ||
{ | ||
Text = text; | ||
} | ||
|
||
[JsonPropertyName("text")] | ||
public string Text { get; } | ||
} | ||
|
||
/// <inheritdoc /> | ||
public AudioEndpoint(OpenAIClient api) : base(api) { } | ||
|
||
/// <inheritdoc /> | ||
protected override string GetEndpoint() | ||
=> $"{Api.BaseUrl}audio"; | ||
|
||
/// <summary> | ||
/// Transcribes audio into the input language. | ||
/// </summary> | ||
/// <param name="request"><see cref="AudioTranscriptionRequest"/>.</param> | ||
/// <param name="cancellationToken">Optional, <see cref="CancellationToken"/>.</param> | ||
/// <returns>The transcribed text.</returns> | ||
public async Task<string> CreateTranscriptionAsync(AudioTranscriptionRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
using var content = new MultipartFormDataContent(); | ||
using var audioData = new MemoryStream(); | ||
await request.Audio.CopyToAsync(audioData, cancellationToken); | ||
content.Add(new ByteArrayContent(audioData.ToArray()), "file", request.AudioName); | ||
content.Add(new StringContent(request.Model), "model"); | ||
|
||
if (!string.IsNullOrWhiteSpace(request.Prompt)) | ||
{ | ||
content.Add(new StringContent(request.Prompt), "prompt"); | ||
} | ||
|
||
var responseFormat = request.ResponseFormat; | ||
content.Add(new StringContent(responseFormat.ToString().ToLower()), "response_format"); | ||
|
||
if (request.Temperature.HasValue) | ||
{ | ||
content.Add(new StringContent(request.Temperature.ToString()), "temperature"); | ||
} | ||
|
||
if (!string.IsNullOrWhiteSpace(request.Language)) | ||
{ | ||
content.Add(new StringContent(request.Language), "language"); | ||
} | ||
|
||
request.Dispose(); | ||
|
||
var response = await Api.Client.PostAsync($"{GetEndpoint()}/transcriptions", content, cancellationToken); | ||
var responseAsString = await response.ReadAsStringAsync(cancellationToken); | ||
|
||
return responseFormat == AudioResponseFormat.Json | ||
? JsonSerializer.Deserialize<AudioResponse>(responseAsString)?.Text | ||
: responseAsString; | ||
} | ||
|
||
/// <summary> | ||
/// Translates audio into into English. | ||
/// </summary> | ||
/// <param name="request"></param> | ||
/// <param name="cancellationToken"></param> | ||
/// <returns>The translated text.</returns> | ||
public async Task<string> CreateTranslationAsync(AudioTranslationRequest request, CancellationToken cancellationToken = default) | ||
{ | ||
using var content = new MultipartFormDataContent(); | ||
using var audioData = new MemoryStream(); | ||
await request.Audio.CopyToAsync(audioData, cancellationToken); | ||
content.Add(new ByteArrayContent(audioData.ToArray()), "file", request.AudioName); | ||
content.Add(new StringContent(request.Model), "model"); | ||
|
||
if (!string.IsNullOrWhiteSpace(request.Prompt)) | ||
{ | ||
content.Add(new StringContent(request.Prompt), "prompt"); | ||
} | ||
|
||
var responseFormat = request.ResponseFormat; | ||
content.Add(new StringContent(responseFormat.ToString().ToLower()), "response_format"); | ||
|
||
if (request.Temperature.HasValue) | ||
{ | ||
content.Add(new StringContent(request.Temperature.ToString()), "temperature"); | ||
} | ||
|
||
request.Dispose(); | ||
|
||
var response = await Api.Client.PostAsync($"{GetEndpoint()}/translations", content, cancellationToken); | ||
var responseAsString = await response.ReadAsStringAsync(cancellationToken); | ||
|
||
return responseFormat == AudioResponseFormat.Json | ||
? JsonSerializer.Deserialize<AudioResponse>(responseAsString)?.Text | ||
: responseAsString; | ||
} | ||
} | ||
} |
This file contains 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,11 @@ | ||
namespace OpenAI.Audio | ||
{ | ||
public enum AudioResponseFormat | ||
{ | ||
Json = 0, | ||
Verbose_Json, | ||
Text, | ||
Srt, | ||
Vtt | ||
} | ||
} |
Oops, something went wrong.