forked from VictorTaelin/ChatSH
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.js
49 lines (44 loc) · 1.09 KB
/
utils.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
// read from config file
import fs from "fs";
import path from "path";
class ChatSHConfig {
constructor(configFile) {
if (configFile.backend !== "openai" && configFile.backend !== "ollama") {
throw new Error(
`Invalid backend: ${configFile.backend} must be either 'openai' or 'ollama'`
);
}
this.backend = configFile.backend;
this.config = configFile[configFile.backend] || {};
}
}
const getConfig = () => {
const configFile = path.join(process.env.HOME, ".config", "chatsh");
let config = {};
if (fs.existsSync(configFile)) {
config = JSON.parse(fs.readFileSync(configFile, "utf8"));
} else {
console.log(
`
Config file not found: ${configFile}
Please create a config file in the following format:
{
"backend": "openai | ollama",
"openai": { "apiKey", "model" }
"ollama": { "model", "host" }
}
Example:
echo '{
"backend": "ollama",
"openai": {
"model": "gemma:7b",
"host": "http://localhost:11434"
}
}' > ~/.config/chatsh
`.trim()
);
process.exit(1);
}
return new ChatSHConfig(config);
};
export { getConfig };