-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
41 lines (36 loc) · 1.15 KB
/
Copy pathstart.js
File metadata and controls
41 lines (36 loc) · 1.15 KB
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
38
39
40
41
const fs = require('fs');
const readline = require('readline');
const { organization, apiKey } = require('./openai_key');
const { Configuration, OpenAIApi } = require("openai");
const bot = process.argv[2];
const configuration = new Configuration({ apiKey });
const openai = new OpenAIApi(configuration);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
});
function generatePrompt(question) {
return `${fs.readFileSync(`${bot}/prompt.txt`, 'utf8')}Q: ${question}`;
}
async function answer(question) {
const completion = await openai.createCompletion({
model: "text-davinci-003",
prompt: generatePrompt(question),
temperature: 0.9,
max_tokens: 400
});
return {
result: completion.data.choices[0].text
};
}
const bot_repl = function () {
rl.question('>>> ', async (prompt) => {
await answer(prompt)
.then(response => response.result.length > 0 ? response.result : '🤷🏽')
.then(result => result.replace(/A:[\n]?/, '\n').trim())
.then(console.log);
bot_repl();
});
};
bot_repl();