-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgpt.js
26 lines (23 loc) · 784 Bytes
/
gpt.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
const fs = require('fs');
const configPath = `${process.env.HOME}/.node-gpt-config.json`;
let config;
try {
config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
} catch (error) {
console.log(`Unable to read ${configPath}`);
process.exit(1);
}
const go = async function (prompt) {
const { url, ...remainingConfigs } = config;
const headers = {
'Content-Type': 'application/json',
'Authorization': `Bearer ${process.env.OPENAI_API_KEY}`
};
const method = 'POST';
const body = JSON.stringify({ ...remainingConfigs, prompt });
const request = await fetch(url, { headers, method, body });
const response = await request.json();
process.stdout.write(response.choices[0].text.trim());
}
promptWords = process.argv.slice(2)
go(promptWords.join(' '))