I am making a voice chatbot with react native.
So at first, I converted the voice recording file to text using whisper-1 and sent it to gpt-4o, but it took too long.
So I want to send the voice recording file to gpt-4o-audio-preview and get the response as text or voice.
Below is the code I wrote.
‘transcribeAudio’ works fine, but ‘sendAudioToGPT’ gives an error. Please help me.
For reference, the voice recording file is in .m4a format.
const transcribeAudio = async (uri: string) => {
try {
const response = await FileSystem.uploadAsync(
"https://api.openai.com/v1/audio/transcriptions",
uri,
{
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "multipart/form-data",
},
httpMethod: "POST",
uploadType: FileSystem.FileSystemUploadType.MULTIPART,
fieldName: "file",
mimeType: "audio/mpeg",
parameters: {
model: "whisper-1",
},
}
);
const parsedResponse = JSON.parse(response.body);
const transcribedText = parsedResponse.text;
const botMessage = {
role: "bot",
content: transcribedText || "fail",
};
setMessages((prevMessages) => [...prevMessages, botMessage]);
} catch (error) {
console.error("Whisper API Error:", error);
}
};
const sendAudioToGPT = async (uri: string) => {
try {
const response = await FileSystem.uploadAsync(
"https://api.openai.com/v1/chat/completions",
uri,
{
headers: {
Authorization: `Bearer ${API_KEY}`,
"Content-Type": "multipart/form-data",
},
httpMethod: "POST",
uploadType: FileSystem.FileSystemUploadType.MULTIPART,
fieldName: "file",
mimeType: "audio/mpeg",
parameters: {
model: "gpt-4o-audio-preview",
},
}
);
await console.log(response);
} catch (err) {
setError("fail");
console.error("API error:", err);
} finally {
setLoading(false);
}
};```