-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
114 lines (101 loc) · 3.43 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
#!/usr/bin/env node
import 'dotenv/config';
import { Command, Option } from 'commander';
import GroqInstance from './util/GroqInstance.js';
import BuildFilePrompt from './util/BuildFilePrompt.js';
import ProcessFileWithProvider from './util/ProcessFileWithProvider.js';
import FilePathResolver from './util/FilePathResolver.js';
import TomlChecker from './util/TomlChecker.js';
import ExtractOptions from './util/ExtractOptions.js';
import ResponsePresenter from './util/ResponsePresenter.js';
import { fileURLToPath } from 'url';
import { dirname, resolve } from 'path';
import { readFileSync } from 'fs';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const packageJSONPath = resolve(__dirname, './package.json');
const packageJSON = JSON.parse(readFileSync(packageJSONPath, 'utf-8'));
const program = new Command();
program
.version(packageJSON.version, '-v, --version', 'output the current version')
.description(packageJSON.description);
program
.addOption(
new Option(
'-a, --api-key <your-key>',
'define API key to use for processing defined in .env file'
).env('API_KEY')
)
.addOption(
new Option(
'-b, --baseURL <url>',
'define the base URL to use for processing defined in .env file'
)
.default('https://api.groq.com/')
.env('BASE_URL')
)
.addOption(
new Option(
'-m, --model <model-name>',
'define the model to use for processing'
)
.default('llama-3.1-70b-versatile')
.env('MODEL_NAME')
)
.addOption(
new Option(
'-o, --output <file>',
'define an output file with valid extension to be able access the output'
)
)
.addOption(
new Option(
'-t, --temperature <number>',
'define temperature of chat completion between 0 to 2'
)
.default(1)
.argParser(parseFloat)
)
.addOption(
new Option(
'-u, --token-usage',
'specify if you want to see tokens that were sent in the prompt and the number of tokens that were returned'
)
);
program
.argument('<files...>', 'path of the files or directories to process')
.action(async (files, options) => {
try {
const {
apiKey,
baseURL,
temperature,
model,
outputFile,
tokenUsage
} = ExtractOptions(options, TomlChecker());
const resolvedFiles = FilePathResolver(files);
const Groq = GroqInstance(apiKey, baseURL);
console.log('Processing request with provider...');
const responses = await Promise.all(
resolvedFiles.map(async (file) => {
return await ProcessFileWithProvider(
Groq,
BuildFilePrompt(file),
model,
temperature
);
})
);
ResponsePresenter(responses, outputFile, tokenUsage);
process.exit(0);
} catch (error) {
console.error(`Error: ${error.message}`);
process.exit(1);
}
});
program.showHelpAfterError();
if (process.env.NODE_ENV !== 'test') {
program.parse(process.argv);
}
export default program;