题意:使用 HttpClient 或 OpenAI NuGet 调用 OpenAI 处理音频文件时不起作用 (C#)。
问题背景:
Getting really frustrated with this. Really wish OpenAI would have some sort of technical help, but whatever.
对此感到非常沮丧。真希望 OpenAI 能提供某种技术支持,但算了。
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
我正在尝试测试是否可以上传 WAV 文件并将其转换为文本。他们的 API 文档表明可以使用以下端点来实现: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.
当然,文档中没有使用 C# 的代码示例,但我尝试修改 Node.js 的代码来使用 HttpClient 或 OpenAI NuGet 包,总是抛出错误,提示“Bad Request”。我甚至还试图让 ChatGPT 生成代码,但那也失败了,真是太“有帮助”了
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?
如果能帮助我解决这个问题,那就太好了。有谁知道为什么会出现错误,我已经提供了模型,但它还是一直在要求。我该如何提供文件和模型类型?
问题解决:
This Should help you. This is an example of call to get the transcript as SRT file, from an audio (or video) file.
这应该对你有帮助。这是一个调用示例,用于从音频(或视频)文件获取 SRT 格式的转录。
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();
}
}