From 2582ed91f859ac06b02385d263be0ce367531c89 Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Mon, 3 Jun 2024 20:26:31 +0100 Subject: [PATCH 1/2] Fixed Audio string response Error handling --- OpenAI.SDK/Extensions/HttpclientExtensions.cs | 16 ++++++++++++++-- OpenAI.SDK/Managers/OpenAIAudioService.cs | 16 ++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/OpenAI.SDK/Extensions/HttpclientExtensions.cs b/OpenAI.SDK/Extensions/HttpclientExtensions.cs index c8ce4535..93075a1c 100644 --- a/OpenAI.SDK/Extensions/HttpclientExtensions.cs +++ b/OpenAI.SDK/Extensions/HttpclientExtensions.cs @@ -111,10 +111,22 @@ private static HttpRequestMessage CreatePostEventStreamRequest(string uri, HttpC return await HandleResponseContent(response, cancellationToken); } - public static async Task PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) + public static async Task<(string? stringResponse, TResponse baseResponse)> PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default) where TResponse : BaseResponse, new() { var response = await client.PostAsync(uri, content, cancellationToken); - return await response.Content.ReadAsStringAsync(cancellationToken) ?? throw new InvalidOperationException(); + if (response.IsSuccessStatusCode) + { + var tResponse = new TResponse + { + HttpStatusCode = response.StatusCode, + HeaderValues = response.ParseHeaders() + }; + return (await response.Content.ReadAsStringAsync(cancellationToken),tResponse ); + } + else + { + return (null, await HandleResponseContent(response, cancellationToken)); + } } public static async Task DeleteAndReadAsAsync(this HttpClient client, string uri, CancellationToken cancellationToken = default) where TResponse : BaseResponse, new() diff --git a/OpenAI.SDK/Managers/OpenAIAudioService.cs b/OpenAI.SDK/Managers/OpenAIAudioService.cs index 360ca2d6..860dd854 100644 --- a/OpenAI.SDK/Managers/OpenAIAudioService.cs +++ b/OpenAI.SDK/Managers/OpenAIAudioService.cs @@ -26,14 +26,14 @@ public async Task CreateTranslation(AudioCreat public async Task> CreateSpeech(AudioCreateSpeechRequest audioCreateSpeechRequest, CancellationToken cancellationToken = default) { - return await _httpClient.PostAndReadAsDataAsync,T>(_endpointProvider.AudioCreateSpeech(), audioCreateSpeechRequest, cancellationToken); + return await _httpClient.PostAndReadAsDataAsync, T>(_endpointProvider.AudioCreateSpeech(), audioCreateSpeechRequest, cancellationToken); } private async Task Create(AudioCreateTranscriptionRequest audioCreateTranscriptionRequest, string uri, CancellationToken cancellationToken = default) { var multipartContent = new MultipartFormDataContent(); - if (audioCreateTranscriptionRequest is {File: not null, FileStream: not null}) + if (audioCreateTranscriptionRequest is { File: not null, FileStream: not null }) { throw new ArgumentException("Either File or FileStream must be set, but not both."); } @@ -56,7 +56,7 @@ private async Task Create(AudioCreateTranscrip } multipartContent.Add(new StringContent(audioCreateTranscriptionRequest.Model), "model"); - + if (audioCreateTranscriptionRequest.TimestampGranularities != null) { foreach (var granularity in audioCreateTranscriptionRequest.TimestampGranularities) @@ -93,9 +93,13 @@ private async Task Create(AudioCreateTranscrip return await _httpClient.PostFileAndReadAsAsync(uri, multipartContent, cancellationToken); } - return new AudioCreateTranscriptionResponse + var response = await _httpClient.PostFileAndReadAsStringAsync(uri, multipartContent, cancellationToken); + if (response.stringResponse != null) { - Text = await _httpClient.PostFileAndReadAsStringAsync(uri, multipartContent, cancellationToken) - }; + response.baseResponse.Text = response.stringResponse; + } + + return response.baseResponse; + } } \ No newline at end of file From bc4649cbc31858bdcbecbc38476b63bd597f9d03 Mon Sep 17 00:00:00 2001 From: Tolga Kayhan Date: Mon, 3 Jun 2024 20:29:54 +0100 Subject: [PATCH 2/2] code Cleanup --- OpenAI.SDK/Managers/OpenAIAudioService.cs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/OpenAI.SDK/Managers/OpenAIAudioService.cs b/OpenAI.SDK/Managers/OpenAIAudioService.cs index 860dd854..c6855b7f 100644 --- a/OpenAI.SDK/Managers/OpenAIAudioService.cs +++ b/OpenAI.SDK/Managers/OpenAIAudioService.cs @@ -1,6 +1,4 @@ -using System.Text; -using System.Text.Json; -using OpenAI.Extensions; +using OpenAI.Extensions; using OpenAI.Interfaces; using OpenAI.ObjectModels; using OpenAI.ObjectModels.RequestModels;