Skip to content
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

573 audiocreatetranscriptionrequest if specify a responseformat that isnt json then responsesuccessful is not set correctly #580

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;

}
}