-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
89 lines (79 loc) · 2.16 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import Groq from 'groq-sdk';
import dotenv from 'dotenv';
import readline from 'readline';
import chalk from 'chalk';
dotenv.config();
const groq = new Groq({
apiKey: process.env.GROQ_API_KEY,
});
const getGroqChatCompletion = async (message) => {
try {
const response = await groq.chat.completions.create({
messages: [
{
role: 'system',
content: 'You are a helpful assistant.',
},
{
role: 'user',
content: message,
},
],
model: 'llama3-8b-8192',
temperature: 0.5,
max_tokens: 1024,
top_p: 1,
stop: null,
stream: false,
});
return response.choices[0]?.message?.content || 'No response from AI.';
} catch (error) {
console.error('Error fetching chat completion:', error);
return 'An error occurred while fetching the response.';
}
};
const typeEffect = (text, chunkSize = 2, delay = 50) => {
// Displays text in chunks (words or phrases) with a delay between each chunk
return new Promise((resolve) => {
let i = 0;
const words = text.split(' ');
const interval = setInterval(() => {
const chunk = words.slice(i, i + chunkSize).join(' ') + ' ';
process.stdout.write(chunk);
i += chunkSize;
if (i >= words.length) {
clearInterval(interval);
resolve();
}
}, delay);
});
};
const main = async () => {
console.log(
chalk.bgGreen.bold(' WELCOME TO AI-TERM-TALK (CLI AI CHATBOT)! ')
);
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: chalk.cyan.bold("Ask your question (or type 'exit' to quit): "),
});
rl.prompt();
rl.on('line', async (input) => {
if (input.toLowerCase() === 'exit') {
rl.close();
} else {
console.log(chalk.green.bold('Thinking...'));
const response = await getGroqChatCompletion(input);
await typeEffect(response);
console.log('\n');
rl.setPrompt(
chalk.blue.bold("Ask another question (or type 'exit' to quit): ")
);
rl.prompt();
}
});
rl.on('close', () => {
console.log(chalk.yellow.bold('\nGOODBYE!'));
});
};
main();