|
1 | 1 | import * as fs from 'fs';
|
2 | 2 | import * as _ from 'lodash';
|
3 | 3 | import { getConfigPath } from './common-path';
|
4 |
| -export interface ChatConfig { |
| 4 | +import { ConfigType } from 'src/downloader/universal-utils'; |
| 5 | +import { Logger } from '@nestjs/common'; |
| 6 | + |
| 7 | +export interface ModelConfig { |
5 | 8 | model: string;
|
6 | 9 | endpoint?: string;
|
7 | 10 | token?: string;
|
8 | 11 | default?: boolean;
|
9 | 12 | task?: string;
|
10 | 13 | }
|
11 | 14 |
|
12 |
| -export class ConfigLoader { |
13 |
| - private chatsConfig: ChatConfig[]; |
| 15 | +export interface EmbeddingConfig { |
| 16 | + model: string; |
| 17 | + endpoint?: string; |
| 18 | + default?: boolean; |
| 19 | + token?: string; |
| 20 | +} |
14 | 21 |
|
| 22 | +export interface AppConfig { |
| 23 | + models?: ModelConfig[]; |
| 24 | + embeddings?: EmbeddingConfig[]; |
| 25 | +} |
| 26 | + |
| 27 | +export const exampleConfigContent = `{ |
| 28 | + // Chat models configuration |
| 29 | + // You can configure multiple chat models |
| 30 | + "models": [ |
| 31 | + // Example of OpenAI GPT configuration |
| 32 | + { |
| 33 | + "model": "gpt-3.5-turbo", |
| 34 | + "endpoint": "https://api.openai.com/v1", |
| 35 | + "token": "your-openai-token", // Replace with your OpenAI token |
| 36 | + "default": true // Set as default chat model |
| 37 | + }, |
| 38 | + |
| 39 | + // Example of local model configuration |
| 40 | + { |
| 41 | + "model": "llama2", |
| 42 | + "endpoint": "http://localhost:11434/v1" |
| 43 | + } |
| 44 | + ], |
| 45 | +
|
| 46 | + // Embedding model configuration (optional) |
| 47 | + "embeddings": [{ |
| 48 | + "model": "text-embedding-ada-002", |
| 49 | + "endpoint": "https://api.openai.com/v1", |
| 50 | + "token": "your-openai-token", // Replace with your OpenAI token |
| 51 | + "default": true // Set as default embedding |
| 52 | + }] |
| 53 | +}`; |
| 54 | + |
| 55 | +export class ConfigLoader { |
| 56 | + readonly logger = new Logger(ConfigLoader.name); |
| 57 | + private type: string; |
| 58 | + private static instances: Map<ConfigType, ConfigLoader> = new Map(); |
| 59 | + private static config: AppConfig; |
15 | 60 | private readonly configPath: string;
|
16 | 61 |
|
17 |
| - constructor() { |
18 |
| - this.configPath = getConfigPath('config'); |
| 62 | + private constructor(type: ConfigType) { |
| 63 | + this.type = type; |
| 64 | + this.configPath = getConfigPath(); |
| 65 | + this.initConfigFile(); |
| 66 | + this.loadConfig(); |
| 67 | + } |
| 68 | + |
| 69 | + public static getInstance(type: ConfigType): ConfigLoader { |
| 70 | + if (!ConfigLoader.instances.has(type)) { |
| 71 | + ConfigLoader.instances.set(type, new ConfigLoader(type)); |
| 72 | + } |
| 73 | + return ConfigLoader.instances.get(type)!; |
| 74 | + } |
| 75 | + |
| 76 | + public initConfigFile(): void { |
| 77 | + Logger.log('Creating example config file', 'ConfigLoader'); |
| 78 | + |
| 79 | + const config = getConfigPath(); |
| 80 | + if (fs.existsSync(config)) { |
| 81 | + return; |
| 82 | + } |
| 83 | + |
| 84 | + if (!fs.existsSync(config)) { |
| 85 | + //make file |
| 86 | + fs.writeFileSync(config, exampleConfigContent, 'utf-8'); |
| 87 | + } |
| 88 | + Logger.log('Creating example config file', 'ConfigLoader'); |
| 89 | + } |
| 90 | + |
| 91 | + public reload(): void { |
19 | 92 | this.loadConfig();
|
20 | 93 | }
|
21 | 94 |
|
22 | 95 | private loadConfig() {
|
23 |
| - const file = fs.readFileSync(this.configPath, 'utf-8'); |
| 96 | + try { |
| 97 | + Logger.log( |
| 98 | + `Loading configuration from ${this.configPath}`, |
| 99 | + 'ConfigLoader', |
| 100 | + ); |
| 101 | + const file = fs.readFileSync(this.configPath, 'utf-8'); |
| 102 | + const jsonContent = file.replace( |
| 103 | + /\\"|"(?:\\"|[^"])*"|(\/\/.*|\/\*[\s\S]*?\*\/)/g, |
| 104 | + (m, g) => (g ? '' : m), |
| 105 | + ); |
| 106 | + ConfigLoader.config = JSON.parse(jsonContent); |
| 107 | + this.validateConfig(); |
| 108 | + } catch (error) { |
| 109 | + if ( |
| 110 | + error.code === 'ENOENT' || |
| 111 | + error.message.includes('Unexpected end of JSON input') |
| 112 | + ) { |
| 113 | + ConfigLoader.config = {}; |
| 114 | + this.saveConfig(); |
| 115 | + } else { |
| 116 | + throw error; |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + this.logger.log(ConfigLoader.config); |
24 | 121 |
|
25 |
| - this.chatsConfig = JSON.parse(file); |
26 | 122 | }
|
27 | 123 |
|
28 |
| - get<T>(path: string) { |
| 124 | + get<T>(path?: string): T { |
29 | 125 | if (!path) {
|
30 |
| - return this.chatsConfig as unknown as T; |
| 126 | + return ConfigLoader.config as unknown as T; |
31 | 127 | }
|
32 |
| - return _.get(this.chatsConfig, path) as T; |
| 128 | + return _.get(ConfigLoader.config, path) as T; |
33 | 129 | }
|
34 | 130 |
|
35 | 131 | set(path: string, value: any) {
|
36 |
| - _.set(this.chatsConfig, path, value); |
| 132 | + _.set(ConfigLoader.config, path, value); |
37 | 133 | this.saveConfig();
|
38 | 134 | }
|
39 | 135 |
|
40 | 136 | private saveConfig() {
|
| 137 | + const configDir = path.dirname(this.configPath); |
| 138 | + if (!fs.existsSync(configDir)) { |
| 139 | + fs.mkdirSync(configDir, { recursive: true }); |
| 140 | + } |
41 | 141 | fs.writeFileSync(
|
42 | 142 | this.configPath,
|
43 |
| - JSON.stringify(this.chatsConfig, null, 4), |
| 143 | + JSON.stringify(ConfigLoader.config, null, 2), |
44 | 144 | 'utf-8',
|
45 | 145 | );
|
46 | 146 | }
|
47 | 147 |
|
| 148 | + addConfig(config: ModelConfig | EmbeddingConfig) { |
| 149 | + if (!ConfigLoader.config[this.type]) { |
| 150 | + ConfigLoader.config[this.type] = []; |
| 151 | + } |
| 152 | + this.logger.log(ConfigLoader.config); |
| 153 | + const index = ConfigLoader.config[this.type].findIndex( |
| 154 | + (chat) => chat.model === config.model, |
| 155 | + ); |
| 156 | + if (index !== -1) { |
| 157 | + ConfigLoader.config[this.type].splice(index, 1); |
| 158 | + } |
| 159 | + |
| 160 | + if (config.default) { |
| 161 | + ConfigLoader.config.models.forEach((chat) => { |
| 162 | + chat.default = false; |
| 163 | + }); |
| 164 | + } |
| 165 | + |
| 166 | + ConfigLoader.config[this.type].push(config); |
| 167 | + this.saveConfig(); |
| 168 | + } |
| 169 | + |
| 170 | + removeConfig(modelName: string): boolean { |
| 171 | + if (!ConfigLoader.config[this.type]) { |
| 172 | + return false; |
| 173 | + } |
| 174 | + |
| 175 | + const initialLength = ConfigLoader.config[this.type].length; |
| 176 | + ConfigLoader.config.models = ConfigLoader.config[this.type].filter( |
| 177 | + (chat) => chat.model !== modelName, |
| 178 | + ); |
| 179 | + |
| 180 | + if (ConfigLoader.config[this.type].length !== initialLength) { |
| 181 | + this.saveConfig(); |
| 182 | + return true; |
| 183 | + } |
| 184 | + |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + getAllConfigs(): EmbeddingConfig[] | ModelConfig[] | null { |
| 189 | + const res = ConfigLoader.config[this.type]; |
| 190 | + return Array.isArray(res) ? res : null; |
| 191 | + } |
| 192 | + |
48 | 193 | validateConfig() {
|
49 |
| - if (!this.chatsConfig) { |
50 |
| - throw new Error("Invalid configuration: 'chats' section is missing."); |
| 194 | + if (!ConfigLoader.config) { |
| 195 | + ConfigLoader.config = {}; |
| 196 | + } |
| 197 | + |
| 198 | + if (typeof ConfigLoader.config !== 'object') { |
| 199 | + throw new Error('Invalid configuration: Must be an object'); |
| 200 | + } |
| 201 | + |
| 202 | + if (ConfigLoader.config.models) { |
| 203 | + if (!Array.isArray(ConfigLoader.config.models)) { |
| 204 | + throw new Error("Invalid configuration: 'chats' must be an array"); |
| 205 | + } |
| 206 | + |
| 207 | + ConfigLoader.config.models.forEach((chat, index) => { |
| 208 | + if (!chat.model) { |
| 209 | + throw new Error( |
| 210 | + `Invalid chat configuration at index ${index}: 'model' is required`, |
| 211 | + ); |
| 212 | + } |
| 213 | + }); |
| 214 | + |
| 215 | + const defaultChats = ConfigLoader.config.models.filter( |
| 216 | + (chat) => chat.default, |
| 217 | + ); |
| 218 | + if (defaultChats.length > 1) { |
| 219 | + throw new Error( |
| 220 | + 'Invalid configuration: Multiple default chat configurations found', |
| 221 | + ); |
| 222 | + } |
51 | 223 | }
|
| 224 | + |
| 225 | + if (ConfigLoader.config[ConfigType.EMBEDDINGS]) { |
| 226 | + this.logger.log(ConfigLoader.config[ConfigType.EMBEDDINGS]); |
| 227 | + if (!Array.isArray(ConfigLoader.config[ConfigType.EMBEDDINGS])) { |
| 228 | + throw new Error("Invalid configuration: 'embeddings' must be an array"); |
| 229 | + } |
| 230 | + |
| 231 | + ConfigLoader.config.models.forEach((emb, index) => { |
| 232 | + if (!emb.model) { |
| 233 | + throw new Error( |
| 234 | + `Invalid chat configuration at index ${index}: 'model' is required`, |
| 235 | + ); |
| 236 | + } |
| 237 | + }); |
| 238 | + |
| 239 | + const defaultChats = ConfigLoader.config[ConfigType.EMBEDDINGS].filter( |
| 240 | + (chat) => chat.default, |
| 241 | + ); |
| 242 | + if (defaultChats.length > 1) { |
| 243 | + throw new Error( |
| 244 | + 'Invalid configuration: Multiple default emb configurations found', |
| 245 | + ); |
| 246 | + } |
| 247 | + } |
| 248 | + } |
| 249 | + |
| 250 | + getConfig(): AppConfig { |
| 251 | + return ConfigLoader.config; |
52 | 252 | }
|
53 | 253 | }
|
0 commit comments