Skip to content

Commit

Permalink
Merge pull request #580 from betalgo/573-audiocreatetranscriptionrequ…
Browse files Browse the repository at this point in the history
…est-if-specify-a-responseformat-that-isnt-json-then-responsesuccessful-is-not-set-correctly

573 audiocreatetranscriptionrequest if specify a responseformat that isnt json then responsesuccessful is not set correctly
  • Loading branch information
kayhantolga authored Jun 3, 2024
2 parents 21ba3bc + 2b03430 commit 4f903fb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
16 changes: 14 additions & 2 deletions OpenAI.SDK/Extensions/HttpclientExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,22 @@ private static HttpRequestMessage CreatePostEventStreamRequest(string uri, HttpC
return await HandleResponseContent<TResponse>(response, cancellationToken);
}

public static async Task<string> PostFileAndReadAsStringAsync(this HttpClient client, string uri, HttpContent content, CancellationToken cancellationToken = default)
public static async Task<(string? stringResponse, TResponse baseResponse)> PostFileAndReadAsStringAsync<TResponse>(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<TResponse>(response, cancellationToken));
}
}

public static async Task<TResponse> DeleteAndReadAsAsync<TResponse>(this HttpClient client, string uri, CancellationToken cancellationToken = default) where TResponse : BaseResponse, new()
Expand Down
16 changes: 10 additions & 6 deletions OpenAI.SDK/Managers/OpenAIAudioService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ public async Task<AudioCreateTranscriptionResponse> CreateTranslation(AudioCreat

public async Task<AudioCreateSpeechResponse<T>> CreateSpeech<T>(AudioCreateSpeechRequest audioCreateSpeechRequest, CancellationToken cancellationToken = default)
{
return await _httpClient.PostAndReadAsDataAsync<AudioCreateSpeechResponse<T>,T>(_endpointProvider.AudioCreateSpeech(), audioCreateSpeechRequest, cancellationToken);
return await _httpClient.PostAndReadAsDataAsync<AudioCreateSpeechResponse<T>, T>(_endpointProvider.AudioCreateSpeech(), audioCreateSpeechRequest, cancellationToken);
}

private async Task<AudioCreateTranscriptionResponse> 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.");
}
Expand All @@ -55,7 +55,7 @@ private async Task<AudioCreateTranscriptionResponse> Create(AudioCreateTranscrip
}

multipartContent.Add(new StringContent(audioCreateTranscriptionRequest.Model), "model");

if (audioCreateTranscriptionRequest.TimestampGranularities != null)
{
foreach (var granularity in audioCreateTranscriptionRequest.TimestampGranularities)
Expand Down Expand Up @@ -92,9 +92,13 @@ private async Task<AudioCreateTranscriptionResponse> Create(AudioCreateTranscrip
return await _httpClient.PostFileAndReadAsAsync<AudioCreateTranscriptionResponse>(uri, multipartContent, cancellationToken);
}

return new AudioCreateTranscriptionResponse
var response = await _httpClient.PostFileAndReadAsStringAsync<AudioCreateTranscriptionResponse>(uri, multipartContent, cancellationToken);
if (response.stringResponse != null)
{
Text = await _httpClient.PostFileAndReadAsStringAsync(uri, multipartContent, cancellationToken)
};
response.baseResponse.Text = response.stringResponse;
}

return response.baseResponse;

}
}

0 comments on commit 4f903fb

Please sign in to comment.