-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
129 lines (116 loc) · 4.17 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
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
import inquirer from 'inquirer';
import axios from 'axios';
import { ArgumentParser } from 'argparse';
import fs from 'fs/promises';
import os from 'os';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const packageJsonPath = path.join(__dirname, 'package.json');
const GPT_CLI_CONFIG_PATH = path.join(os.homedir(), '.gpt_cli.config');
const packageJsonContent = JSON.parse(await fs.readFile(packageJsonPath));
const parser = new ArgumentParser({
description: 'Chat GPT CLI ',
});
const resetConfig = async () => {
try {
await fs.rm(GPT_CLI_CONFIG_PATH);
} catch (e) {}
};
const readConfig = async () => {
let config = {};
try {
const content = await fs.readFile(GPT_CLI_CONFIG_PATH);
config = JSON.parse(content);
} catch (e) {}
return config;
};
const writeConfig = async (data) => {
await fs.writeFile(GPT_CLI_CONFIG_PATH, JSON.stringify(data, 4), { flag: 'w+' });
};
const checkAndUpdateConfig = async (config) => {
if (!config.OPEN_AI_API_KEY || !config.OPEN_AI_TEMPERATURE) {
const res = await inquirer.prompt([
{
type: 'text',
message: 'Enter your OpenAI api key',
name: 'OPEN_AI_API_KEY',
require: true,
validate: (v) => v && v.length === 51
},
{
type: 'number',
message: 'Enter your OpenAI temperature option',
name: 'OPEN_AI_TEMPERATURE',
require: false,
default: 0.7,
},
]);
config.OPEN_AI_API_KEY = res.OPEN_AI_API_KEY;
config.OPEN_AI_TEMPERATURE = res.OPEN_AI_TEMPERATURE;
await writeConfig(config);
}
return config;
};
const sendRequest = async (config) => {
const { prompt } = await inquirer.prompt([
{
type: 'text',
message: 'Ask your question to the ChatGPT',
name: 'prompt',
require: true,
validate: (v) => !!v
},
]);
try {
const result = await axios.post(
'https://api.openai.com/v1/chat/completions',
{
model: 'gpt-3.5-turbo',
messages: [{ role: 'user', content: prompt }],
temperature: config.OPEN_AI_TEMPERATURE,
},
{ headers: { Authorization: `Bearer ${config.OPEN_AI_API_KEY}`, 'Content-Type': 'application/json' } }
);
const data = result.data;
console.log(`Prompt tokens: ${data.usage.prompt_tokens}, Completion tokens: ${data.usage.completion_tokens}`);
console.log(`-----------------------------------------`);
console.log(`GPT: ${data.choices[0].message.content}`);
} catch (e) {
console.log(e.message);
}
};
parser.add_argument('-v', '--version', { action: 'version', version: packageJsonContent.version });
parser.add_argument('-r', '--reset-config', { action: 'store_true', help: 'reset the config file' });
parser.add_argument('-a', '--replace-api-key', { type: 'string', dest: 'newApiKey', help: 'replace the api key' });
parser.add_argument('-t', '--replace-temperature', { type: 'int', dest: 'newTemperature', help: 'replace the temperature' });
const argsCheck = async (args) => {
if (args) {
if (args.reset_config) {
await resetConfig();
}
if (args.newApiKey) {
const config = await readConfig();
await writeConfig({ ...config, OPEN_AI_API_KEY: args.newApiKey });
}
if (args.newTemperature) {
const config = await readConfig();
await writeConfig({ ...config, OPEN_AI_TEMPERATURE: args.newTemperature });
}
}
};
const baseFlow = async () => {
let GPT_CLI_CONFIG = await readConfig();
GPT_CLI_CONFIG = await checkAndUpdateConfig(GPT_CLI_CONFIG);
await sendRequest(GPT_CLI_CONFIG);
};
const main = async () => {
const args = parser.parse_args();
if (args && (args.reset_config || args.newApiKey || args.newTemperature)) {
await argsCheck(args);
return;
}
await baseFlow();
};
main();