我想从firebase bucket获得一个webm文件,并使用openai whisper转录它。
到目前为止,我正在为桶中的文件生成一个公共URL,使用axios发送请求并创建blob。
export const getFile = async(publicUrl: string) => {
const response = await axios.get(publicUrl, { responseType: 'blob' })
return new Blob([response.data], { type: 'video/webm' })
}
在官方openai nodejs包中我想调用的函数是:
createTranscription(file: File, model: string, prompt?: string, responseFormat?: string, temperature?: number, language?: string, options?: AxiosRequestConfig): Promise<import("axios").AxiosResponse<CreateTranscriptionResponse, any>>;
现在根据openai文档:
文件要求。要转录的音频文件,格式为mp3、mp4、mpeg、mpga、m4a、wav或webm。
我有麻烦将此Blob
转换为File
或从firebase存储桶获取File
对象。
What I've try
构造文件
const file = new File([blob], "test.webm", {
type: "video/webm",
});
我会得到一个:ReferenceError: File is not defined
,据我的理解,我不能使用文件javascript对象。
添加所需的属性到blob
一个Blob
和一个File
是接近相同的,所以我尝试添加缺失的属性:
const resp = await openai.createTranscription(
{...blob, lastModified: 0, webkitRelativePath: "", name: "test.webm"},
"whisper-1"
);
哪个打字脚本不喜欢:
Argument of type '{ lastModified: number; webkitRelativePath: string; name: string; size: number; type: string; arrayBuffer(): Promise<ArrayBuffer>; slice(start?: number | undefined, end?: number | undefined, contentType?: string | undefined): Blob; stream(): ReadableStream<...>; text(): Promise<...>; prototype: Blob; }' is not assignable to parameter of type 'File'.
Type '{ lastModified: number; webkitRelativePath: string; name: string; size: number; type: string; arrayBuffer(): Promise<ArrayBuffer>; slice(start?: number | undefined, end?: number | undefined, contentType?: string | undefined): Blob; stream(): ReadableStream<...>; text(): Promise<...>; prototype: Blob; }' provides no match for the signature 'new (blobParts?: BlobPart[] | undefined, options?: BlobPropertyBag | undefined): Blob'.
我有一个类似的问题,输入是使用HTML输入类型文件上传的PDF文件,我想从PDF中提取文本并将其传递给openAI模型。
PDF文本提取器需要一个Buffer对象。在node.js中无法转换/解析传入文件
最后,我尝试了Multer
作为中间件,它工作。
我使用的是nextjs,所以这是我的代码。
import nextConnect from 'next-connect';
import multer from 'multer';
interface MulterRequest extends NextApiRequest {
file: multerFile;
files: Array<multerFile>;
}
const apiRoute = nextConnect({
onError(error, req: MulterRequest, res: NextApiResponse) {
res.status(501).json({
error: `Sorry something Happened! ${error.message}`,
});
},
onNoMatch(req, res) {
res.status(405).json({ error: `Method "${req.method}" Not Allowed` });
},
});
apiRoute.use(multer().any());
apiRoute.post(async (req, res) => {
try {
// console.log(req.body, req.files); // Your form data here
// Any logic with your data here
const configuration = new Configuration({
apiKey: process.env.OPENAI_API_KEY,
});
const openai = new OpenAIApi(configuration);
// Problem was this part 👇 arg1 type was Buffer
const data = await PdfParse(req.files[0].buffer);
const response = await openai....stuff
console.log(response.data)
res.status(200).json({ data: response.data.choices })
// fs.writeFile('./parse.txt', data.text, () => {});
} catch (error) {
console.log(error);
res.status(500).json({ data: 'Error', error });
}
});
Tl;dr使用multer
作为中间件可能会解决您的问题