我正在使用t3-app和Next.js app router。
我要上传一个音频文件到opanai语音到文本模型使用服务器动作形式。每次我uplaod我的.mp3文件,我无法获取转录。
这是我的代码:
import { api } from "~/trpc/server";
export default function CreateTranscription() {
async function create(formData: FormData) {
"use server";
const audioFile = formData.get("audioFile");
// mutate data
if (audioFile !== null && typeof audioFile !== "string") {
const submit = await api.openai.speechToText.mutate({
audioFile: audioFile,
});
}
// revalidate cache
}
return (
<>
<form action={create}>
<input type="file" name="audioFile" id="audioFile" required />
<button type="submit">Upload</button>
</form>
</>
);
}
字符串
路由器:
import { z } from "zod";
import {
createTRPCRouter,
publicProcedure,
} from "~/server/api/trpc";
import OpenAI from "openai";
import type { Uploadable } from "openai/uploads";
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY,
});
export const openAIRouter = createTRPCRouter({
speechToText: publicProcedure
.input(
z.object({
audioFile: z.custom<Uploadable>(),
}),
)
.mutation(async ({ input }) => {
const transcriptions = await openai.audio.transcriptions.create({
file: input.audioFile,
model: "whisper-1",
});
return transcriptions;
}),
});
型
这就是错误:
input: {
audioFile: File {
size: 379924,
type: 'audio/mpeg',
name: 'Vaundysong.mp3',
lastModified: 1700993541098
}
},
result: TRPCClientError: fetch failed
at TRPCClientError.from (webpack-internal:///(rsc)/./node_modules/@trpc/client/dist/TRPCClientError-0de4d231.mjs:41:16)
at eval (webpack-internal:///(rsc)/./node_modules/@trpc/client/dist/httpBatchLink-204206a5.mjs:207:101) {
meta: undefined,
shape: undefined,
data: undefined,
[cause]: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11) {
cause: [RequestContentLengthMismatchError]
}
},
elapsedMs: 8
}
⨯ node_modules/@trpc/client/dist/TRPCClientError-0de4d231.mjs (37:15) @ TRPCClientError.from
⨯ Internal error: TRPCClientError: fetch failed
at TRPCClientError.from (./node_modules/@trpc/client/dist/TRPCClientError-0de4d231.mjs:41:16)
at eval (./node_modules/@trpc/client/dist/httpBatchLink-204206a5.mjs:207:101)
Cause: TypeError: fetch failed
at Object.fetch (node:internal/deps/undici/undici:11576:11) {
cause: RequestContentLengthMismatchError: Request body length does not match content-length header
at AsyncWriter.end (/Users/hurraychak/Side Projects/muspic/node_modules/next/dist/compiled/undici/index.js:1:82741)
at writeIterable (/Users/hurraychak/Side Projects/muspic/node_modules/next/dist/compiled/undici/index.js:1:81411) {
code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
}
型
我认为我的.mp3文件符合格式,但错误消息似乎在谈论我的请求不符合要求。我不知道问题来自哪里,因为我对全栈开发非常陌生。
也许我的openai_API_key不工作?或者有什么可能的方法可以知道我的文件是否匹配API?
1条答案
按热度按时间eeq64g8w1#
尝试使用NVM降级节点版本。