Search code examples
c#openai-apiasp.net-core-6.0

C# Calling OpenAI to process audio file doesn't work. With HttpClient or OpenAI NuGet


Getting really frustrated with this. Really wish OpenAI would have some sort of technical help, but whatever.

I am trying to test to see if we can upload a WAV file and have it translate to text. Their API Documentation indicates they can with the following endpoint: POST https://api.openai.com/v1/audio/transcriptions

Of course there are no code samples using C#, but trying to modify the node one to work with either HttpClient or OpenAI NuGet package, it keeps throwing errors saying bad request. I even went so far to see if ChatGPT and render the code, which that too fails lol lots of help that is.

For the most part, I've tried this variation:

static async Task<string> ConvertAudioToText(string filePath, string apiKey)
{
    using var httpClient = new HttpClient();
    httpClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {apiKey}");
    httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

    var audioBytes = await File.ReadAllBytesAsync(filePath);

    using var content = new MultipartFormDataContent();
    using var audioContent = new ByteArrayContent(audioBytes);
    audioContent.Headers.ContentType = new MediaTypeHeaderValue("audio/wav");
    content.Add(audioContent, "model", "whisper-1");

    var response = await httpClient.PostAsync("https://api.openai.com/v1/engines/whisper-1/transcriptions", content);
    var responseContent = await response.Content.ReadAsStringAsync();

    return responseContent;
}

Which keeps returning this error message:

"error": {
    "message": "you must provide a model parameter",
    "type": "invalid_request_error",
    "param": null,
    "code": null
}

Any help trying to figure this out, would be great. Any idea why this gives me an error, I provided the model but it keeps asking for it. How do I provide a file and model types?

Thanks!


Solution

  • This Should help you. This is an example of call to get the transcript as SRT file, from an audio (or video) file.

    HttpClient client = new HttpClient();
    string filePath = "path_to_your_file";
    
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "YOUR_API_KEY");
    
    using (var memoryStream = new MemoryStream())
    {
        using (var fs = File.OpenRead(filePath))
        {
            fs.CopyTo(memoryStream);
            var content = new MultipartFormDataContent
            {
                { new StringContent("whisper-1"), "model" },
                { new StringContent("srt"), "response_format" },
                { new ByteArrayContent(memoryStream.ToArray()), "file", filePath }
            };
            var response = await client.PostAsync("https://api.openai.com/v1/audio/transcriptions", content);
            var transcript = await response.Content.ReadAsStringAsync();
        }
    }