diff --git a/.gitignore b/.gitignore index ecb875a..84b96dd 100644 --- a/.gitignore +++ b/.gitignore @@ -160,3 +160,5 @@ OpenAPI_key.txt image_edit_mask_.png image_edit_original_.png instructions.txt +/Playgrounds/Transcription/Male voices +/Playgrounds/Transcription/Male voices.zip diff --git a/Forge.OpenAI/Forge.OpenAI.csproj b/Forge.OpenAI/Forge.OpenAI.csproj index 65bf2d0..f73a903 100644 --- a/Forge.OpenAI/Forge.OpenAI.csproj +++ b/Forge.OpenAI/Forge.OpenAI.csproj @@ -9,9 +9,9 @@ - net461;netstandard2.0;netcoreapp3.1;net6.0;net7.0 + netstandard2.0;net6.0;net7.0 true - ..\..\.Documents\Forge.pfx + {D1DDE02E-1865-4173-8F3C-153D99AB394F} Forge.OpenAI.xml diff --git a/Forge.OpenAI/Infrastructure/ApiHttpService.cs b/Forge.OpenAI/Infrastructure/ApiHttpService.cs index 0417d57..86631e2 100644 --- a/Forge.OpenAI/Infrastructure/ApiHttpService.cs +++ b/Forge.OpenAI/Infrastructure/ApiHttpService.cs @@ -6,6 +6,7 @@ using Microsoft.Extensions.Options; using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Net.Http; @@ -411,16 +412,20 @@ private async Task> ApiCall(HttpMet { if (typeof(string).IsAssignableFrom(typeof(TResult))) { + //var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); result = new HttpOperationResult(jsonResult as TResult); } else { - result = new HttpOperationResult(JsonSerializer.Deserialize(jsonResult, _options.JsonSerializerOptions)); + var contentAsBytes = UTF8Encoding.UTF8.GetBytes(jsonResult); + var jsonObj = GetUTF8Object(contentAsBytes); + result = new HttpOperationResult(jsonObj); SetResponseData(response, result.Result); } } else { + // var jsonResult = await response.Content.ReadAsStringAsync().ConfigureAwait(false); _logger?.LogDebug($"ApiCall, response indicates an unsuccessful operation from {httpClient.BaseAddress}{uri}, method: {httpMethod.Method}"); result = new HttpOperationResult(new Exception(response.StatusCode.ToString(), new Exception(jsonResult)), response.StatusCode, jsonResult); @@ -434,7 +439,11 @@ private async Task> ApiCall(HttpMet return result; }); } - + private TResult GetUTF8Object(byte[] responseBytes) + { + Utf8JsonReader reader = new Utf8JsonReader(responseBytes); + return JsonSerializer.Deserialize(ref reader, _options.JsonSerializerOptions); + } /// Perform the API call in streaming mode the asynchronously. /// The type of the data. /// The type of the result. diff --git a/Forge.OpenAI/Interfaces/Services/ITranscriptionService.cs b/Forge.OpenAI/Interfaces/Services/ITranscriptionService.cs index 14f6552..4713e47 100644 --- a/Forge.OpenAI/Interfaces/Services/ITranscriptionService.cs +++ b/Forge.OpenAI/Interfaces/Services/ITranscriptionService.cs @@ -17,6 +17,13 @@ public interface ITranscriptionService /// TranscriptionResponse /// Task> GetAsync(TranscriptionRequest request, CancellationToken cancellationToken); + /// + /// This method is useful + /// + /// + /// + /// + //Task> GetResponseAsStringAsync(TranscriptionRequest request, CancellationToken cancellationToken); } diff --git a/Forge.OpenAI/Services/TranscriptionService.cs b/Forge.OpenAI/Services/TranscriptionService.cs index 393d539..a942367 100644 --- a/Forge.OpenAI/Services/TranscriptionService.cs +++ b/Forge.OpenAI/Services/TranscriptionService.cs @@ -92,7 +92,7 @@ private async Task TranscriptHttpContentFactoryAsync(TranscriptionR if (!string.IsNullOrWhiteSpace(request.Prompt)) content.Add(new StringContent(request.Prompt), "prompt"); if (!string.IsNullOrWhiteSpace(request.ResponseFormat)) content.Add(new StringContent(request.ResponseFormat), "response_format"); - if (!string.IsNullOrWhiteSpace(request.Language)) content.Add(new StringContent(request.ResponseFormat), "language"); + if (!string.IsNullOrWhiteSpace(request.Language)) content.Add(new StringContent(request.Language), "language"); if (request.Temperature.HasValue) content.Add(new StringContent(request.Temperature.Value.ToString()), "temperature"); return content; diff --git a/Forge.OpenAI/Settings/OpenAIDefaultOptions.cs b/Forge.OpenAI/Settings/OpenAIDefaultOptions.cs index b1ddb74..82e4947 100644 --- a/Forge.OpenAI/Settings/OpenAIDefaultOptions.cs +++ b/Forge.OpenAI/Settings/OpenAIDefaultOptions.cs @@ -160,7 +160,9 @@ public static class OpenAIDefaultOptions /// The default json serializer options public static JsonSerializerOptions DefaultJsonSerializerOptions { get; set; } = new JsonSerializerOptions { - DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull + DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull, + Encoder = System.Text.Encodings.Web.JavaScriptEncoder.UnsafeRelaxedJsonEscaping + }; } diff --git a/Playgrounds/Transcription/Program.cs b/Playgrounds/Transcription/Program.cs index e518d62..c76e19b 100644 --- a/Playgrounds/Transcription/Program.cs +++ b/Playgrounds/Transcription/Program.cs @@ -5,12 +5,22 @@ using Forge.OpenAI.Models.Common; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; +using System.Text; namespace Transcription { internal class Program { + public static string DecodeFromUtf16ToUtf8(string utf16String) + { + // copy the string as UTF-8 bytes. + byte[] utf8Bytes = new byte[utf16String.Length]; + for (int i = 0; i < utf16String.Length; ++i) + utf8Bytes[i] = (byte)utf16String[i]; + + return Encoding.UTF8.GetString(utf8Bytes, 0, utf8Bytes.Length); + } static async Task Main(string[] args) { @@ -27,10 +37,11 @@ static async Task Main(string[] args) TranscriptionRequest request = new TranscriptionRequest(); request.AudioFile = new BinaryContentData() { ContentName = "audio.mp3", SourceStream = File.OpenRead("audio.mp3") }; - + //request.Language = "ta"; HttpOperationResult response = await openAi.TranscriptionService.GetAsync(request, CancellationToken.None).ConfigureAwait(false); if (response.IsSuccess) { + Console.OutputEncoding = Encoding.UTF8; Console.WriteLine(response.Result?.Text); } else diff --git a/Playgrounds/Transcription/Sample.mp3 b/Playgrounds/Transcription/Sample.mp3 new file mode 100644 index 0000000..907f90c Binary files /dev/null and b/Playgrounds/Transcription/Sample.mp3 differ diff --git a/Playgrounds/Transcription/Transcription.csproj b/Playgrounds/Transcription/Transcription.csproj index fc45c3e..f39d06d 100644 --- a/Playgrounds/Transcription/Transcription.csproj +++ b/Playgrounds/Transcription/Transcription.csproj @@ -15,6 +15,7 @@ + @@ -24,9 +25,11 @@ - + + + PreserveNewest @@ -34,6 +37,9 @@ PreserveNewest + + Always +