-
Notifications
You must be signed in to change notification settings - Fork 0
/
readCode.txt
151 lines (127 loc) · 3.91 KB
/
readCode.txt
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
is there anything wrong in this code?
import { config } from 'dotenv';
import OpenAI from 'openai';
import inquirer from 'inquirer';
import chalk from 'chalk';
import fs from 'fs'; // read file
config();
let filePath = "F:\\chatgpt-ui\\node";
const enterMessage = async () => {
const question = await inquirer.prompt({
name: 'askIfFileOrInput',
type: 'list',
message: 'read from a file or input?',
choices: [
'file',
'input',
]
});
if (question.askIfFileOrInput === 'file') {
try {
const code = fs.readFileSync(filePath, 'utf-8');
return code;
} catch (error) {
console.error(`Error reading file: ${error.message}`);
process.exit(1);
}
} else {
const answer = await inquirer.prompt({
name: 'enterMessage',
type: 'input',
message: `\n${chalk.blueBright('You:')} `,
default() {
return 'Enter message';
},
});
return answer.enterMessage;
}
};
const getMaxTokens = async () => {
const answer = await inquirer.prompt ({
name: 'askIfChangeMaxTokens',
type: 'list',
message: 'change the Max tokens? \ndefault: 80 tokens',
choices: [
'yes',
'no',
],
});
if(answer.askIfChangeMaxTokens == 'no') {
return 80
} else {
const answer = await inquirer.prompt({
name: 'getMaxTokens',
type: 'input',
message: 'Enter max tokens: ',
})
return answer.getMaxTokens
}
}
const getSystemMessage = async () => {
const answer = await inquirer.prompt ({
name: 'askIfChangeSystemMessage',
type: 'list',
message: 'change the model? (to better fine tune the answer to your wants)\ndefault: You are a helpful assistant.',
choices: [
'yes',
'no',
],
});
if (answer.askIfChangeSystemMessage === 'no') {
return 'You are a helpful assistant.'
} else {
const answer = await inquirer.prompt({
name: 'getSytemMessage',
type: 'input',
message: 'Enter system role message/context: ',
})
return answer.getSytemMessage
}
}
const getModel = async () => {
const answer = await inquirer.prompt({
name: 'getModel',
type: 'list',
message: 'Select the model to be used: ',
choices: [
'Chatgpt-4',
'chatgpt-3.5',
],
});
if (answer.getModel === 'chatgpt-3.5') {
return 'gpt-3.5-turbo-0125'; // Corrected to return the selected model
} else {
return 'gpt-4-turbo-preview';
}
};
const client = new OpenAI({
apiKey: process.env.API_KEY,
});
async function main() {
console.clear()
const selectedModel = await getModel();
const temperature = 0.3;
console.clear()
const max_tokens = await getMaxTokens();
console.clear()
const system_message = await getSystemMessage();
console.clear()
while (true) {
const userMessage = await enterMessage(); // Use await to get user input
if (userMessage === 'exit') {
console.log("Exiting program");
break;
}
const completion = await client.chat.completions.create({
model: selectedModel,
messages: [
{ "role": "system", "content": system_message },
{ "role": "user", "content": userMessage },
],
temperature: temperature,
max_tokens: max_tokens,
});
console.log(`\n${chalk.redBright('Assistant: ')}${completion.choices[0].message.content}\n`);
}
}
main();