-
Notifications
You must be signed in to change notification settings - Fork 0
/
openai_Grok_resp.js
36 lines (31 loc) · 996 Bytes
/
openai_Grok_resp.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
import axios from 'axios';
import cliSpinners from 'cli-spinners';
import ora from 'ora';
import dotenv from 'dotenv';
import fs from 'fs';
import path from 'path';
import os from 'os';
const homeDir = os.homedir();
const homeEnvPath = path.join(homeDir, '.terminalbot', '.env');
// Load environment variables from the homeEnvPath
dotenv.config({ path: homeEnvPath });
async function getGptResponse(data) {
const loading = ora({
spinner: cliSpinners.clock,
}).start();
try {
const response = await axios.post("https://api.groq.com/openai/v1/chat/completions", data, {
headers: {
'Authorization': `Bearer ${process.env.API_KEY}`,
'Content-Type': 'application/json'
}
});
loading.stop();
return response.data.choices[0].message.content;
} catch (error) {
loading.fail();
// console.error("Error:", error);
return null;
}
}
export default getGptResponse;