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

题意:使用 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();
    }
}

 

  • 15
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
OpenAI是一个人工智能研究机构,提供了一系列的API和工具,以帮助开发人员构建智能系统。要接入OpenAI,您需要按照以下步骤操作: 1. 注册OpenAI账户并创建API密钥 在OpenAI官网注册账户并创建API密钥,以便您可以使用OpenAI提供的API。 2. 安装OpenAI API客户端库 您需要安装OpenAI API客户端库以便调用OpenAI API。您可以使用以下命令在Java项目中安装OpenAI API客户端库: ``` // 使用 Maven 安装 OpenAI API 客户端库 <dependency> <groupId>com.openai</groupId> <artifactId>openai-api</artifactId> <version>0.0.1</version> </dependency> ``` 3. 编写Java代码 在Java项目中编写代码以调用OpenAI API。以下是一个简单的示例: ```java import com.openai.api.ApiException; import com.openai.api.api.CompletionsApi; import com.openai.api.model.CompletionRequest; import com.openai.api.model.CompletionResponseData; public class OpenAIDemo { public static void main(String[] args) { CompletionsApi apiInstance = new CompletionsApi(); String prompt = "Hello, my name is"; CompletionRequest completionRequest = new CompletionRequest(); completionRequest.setPrompt(prompt); completionRequest.setMaxTokens(5); completionRequest.setTemperature(0.5); completionRequest.setN(1); String apiKey = "YOUR_API_KEY"; try { CompletionResponseData result = apiInstance.createCompletion(completionRequest, apiKey); System.out.println(result.getChoices().get(0).getText()); } catch (ApiException e) { System.err.println("Exception when calling CompletionsApi#createCompletion"); e.printStackTrace(); } } } ``` 在这个示例中,我们使用OpenAI API的Completions API来生成文本。我们提供了一个简单的提示,然后设置了一些参数(例如生成的最大标记数和生成的温度)。然后我们调用了createCompletion方法,并传递了我们的API密钥。最后,我们打印出了生成的文本。 以上是一个简单的示例,您可以根据自己的需求来调用OpenAI提供的不同API
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

营赢盈英

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值