-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaskhal.js
More file actions
68 lines (60 loc) · 2.57 KB
/
askhal.js
File metadata and controls
68 lines (60 loc) · 2.57 KB
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
import { checkEnvAPIKey, log, processCLIArguments } from './util.js';
import { readFile } from './processContext.js';
import { queryAI } from './queryOpenRouter.js';
/**
* Main function
* @returns {Promise<void>}
* @throws {Error} When file reading or AI query fails
*/
async function main() {
const program = processCLIArguments();
const aiModelName = program.opts().model;
const systemPrompt = program.opts().system;
const contextPath = program.opts().context;
const contextType = (program.opts().type).toLowerCase();
let userPrompt = program.opts().user;
const streamOutput = program.opts().responsive;
const compressPrompt = program.opts().fit;
const apiKey = (program.opts().key) ? program.opts().key : checkEnvAPIKey('OPENROUTER_API_KEY');
const baseURL = program.opts().url;
/**
* @typedef {Object} AIParameters
* @property {number} [TEMPERATURE] - Range: [0.0, 2.0]. Controls the randomness of the generated text.
* @property {number} [TOP_P] - Range: [0.0, 1.0]. Controls the diversity of the generated text.
* @property {number} [TOP_K] - Range: [1, Infinity). Controls the diversity of the generated text.
* @property {number} [FREQUENCY_PENALTY] - Range: [-2.0, 2.0]. Penalizes the frequency of a token in the generated text.
* @property {number} [PRESENCE_PENALTY] - Range: [-2.0, 2.0]. Penalizes the presence of a token in the generated text.
* @property {number} [REPETITION_PENALTY] - Range: [0.0, 2.0]. Penalizes the repetition of a token in the generated text.
*/
let aiParameters = {};
aiParameters.TEMPERATURE = program.opts().temperature;
aiParameters.TOP_K = program.opts().topk;
aiParameters.TOP_P = program.opts().topp;
aiParameters.FREQUENCY_PENALTY = program.opts().frequency;
aiParameters.REPETITION_PENALTY = program.opts().repetition;
aiParameters.PRESENCE_PENALTY = program.opts().presence;
let context;
try {
if (contextPath)
context = await readFile(contextPath, contextType);
if (context)
userPrompt += ` ${context}`;
} catch (err) {
log.error("could not process context file");
throw err;
}
try {
await queryAI(aiModelName, systemPrompt, userPrompt, streamOutput, compressPrompt, baseURL, apiKey, aiParameters);
} catch (err) {
log.error("could not query AI model");
throw err;
}
}
/**
* Program entry point
*/
main()
.catch((err) => {
log.error(`Program terminated with error code '${err.code}'\n${err.message} `);
process.exit(1);
});