-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenAI-API-Calls.js
37 lines (32 loc) · 973 Bytes
/
OpenAI-API-Calls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
const { config } = require('dotenv');
const OpenAI = require('openai');
const fs = require('fs');
config();
const openai = new OpenAI({
apiKey: process.env.OPENAI_API_KEY
});
async function generateChatResponse(messages) {
try {
const completion = await openai.chat.completions.create({
model: "gpt-3.5-turbo",
messages: messages,
});
return completion.choices[0].message.content;
} catch (error) {
console.error('Error generating chat response:', error);
throw error;
}
}
async function transcribeAudio(audioPath) {
try {
const response = await openai.audio.transcriptions.create({
model: "whisper-1",
file: fs.createReadStream(audioPath),
});
return response.text;
} catch (error) {
console.error('Error transcribing audio:', error);
throw error;
}
}
module.exports = { generateChatResponse, transcribeAudio };