-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (57 loc) · 2.08 KB
/
index.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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { createInterface } from 'node:readline/promises';
import { env as environment, exit, stdin as input, stdout as output } from 'node:process';
import OpenAI from 'openai';
const EXIT_COMMAND = 'exit';
const SYSTEM_ROLE = 'system';
const USER_ROLE = 'user';
const apiKey = environment.CHATGPT_CLI_SECRET_KEY;
if (!apiKey?.length) {
console.error(`You must specify "CHATGPT_CLI_SECRET_KEY" via the environment`);
exit(1);
}
const openAI = new OpenAI({ apiKey });
const fitOutputToScreen = (environment.CHATGPT_CLI_FIT_OUTPUT_TO_SCREEN ?? 'true') === 'true';
const model = environment.CHATGPT_CLI_MODEL ?? 'gpt-3.5-turbo';
const prompt = environment.CHATGPT_CLI_PROMPT ?? '> ';
const readline = createInterface({
input,
output,
terminal: true,
historySize: 0
});
const formatOutput = content => {
if (!fitOutputToScreen) return content;
const maxLineLength = process.stdout.columns;
if (content.length <= maxLineLength) return content;
return content.replace(
new RegExp(`(?![^\\n]{1,${maxLineLength}}$)([^\\n]{1,${maxLineLength}})\\s`, 'g'),
'$1\n'
);
};
const chatbotType = await readline.question(
`What type of chatbot would you like to create?\n\n${prompt}`
);
if (chatbotType === EXIT_COMMAND) {
exit(0);
}
const messages = [{ role: SYSTEM_ROLE, content: chatbotType }];
let userInput = await readline.question(`\nSay hello to your new assistant.\n\n${prompt}`);
while (userInput !== EXIT_COMMAND) {
messages.push({ role: USER_ROLE, content: userInput });
try {
const response = await openAI.chat.completions.create({ messages, model });
const responseMessage = response?.data?.choices?.[0]?.message;
if (responseMessage) {
messages.push(responseMessage);
userInput = await readline.question(
`\n${formatOutput(responseMessage.content)}\n\n${prompt}`
);
} else {
userInput = await readline.question(`\nNo response, try asking again\n\n${prompt}`);
}
} catch (error) {
console.log(error.message);
userInput = await readline.question(`\nSomething went wrong, try asking again\n\n${prompt}`);
}
}
readline.close();