-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.js
556 lines (518 loc) · 19 KB
/
config.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
import path from 'path';
import convict from 'convict';
import HandleBars from './lib/handleBars.js';
import fs from 'fs';
import { fileURLToPath, pathToFileURL } from 'url';
import GcpAuthTokenHelper from './lib/gcpAuthTokenHelper.js';
import logger from './lib/logger.js';
import PathwayManager from './lib/pathwayManager.js';
import { readdir } from 'fs/promises';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
convict.addFormat({
name: 'string-array',
validate: function (val) {
if (!Array.isArray(val)) {
throw new Error('must be of type Array');
}
},
coerce: function (val) {
return val.split(',');
},
});
// Schema for config
var config = convict({
env: {
format: String,
default: 'development',
env: 'NODE_ENV'
},
cortexId: {
format: String,
default: 'local',
env: 'CORTEX_ID'
},
basePathwayPath: {
format: String,
default: path.join(__dirname, 'pathways', 'basePathway.js'),
env: 'CORTEX_BASE_PATHWAY_PATH'
},
corePathwaysPath: {
format: String,
default: path.join(__dirname, 'pathways'),
env: 'CORTEX_CORE_PATHWAYS_PATH'
},
cortexApiKeys: {
format: 'string-array',
default: null,
env: 'CORTEX_API_KEY',
sensitive: true
},
cortexConfigFile: {
format: String,
default: null,
env: 'CORTEX_CONFIG_FILE'
},
defaultModelName: {
format: String,
default: null,
env: 'DEFAULT_MODEL_NAME'
},
enableCache: {
format: Boolean,
default: true,
env: 'CORTEX_ENABLE_CACHE'
},
enableDuplicateRequests: {
format: Boolean,
default: true,
env: 'CORTEX_ENABLE_DUPLICATE_REQUESTS'
},
enableGraphqlCache: {
format: Boolean,
default: false,
env: 'CORTEX_ENABLE_GRAPHQL_CACHE'
},
enableRestEndpoints: {
format: Boolean,
default: false,
env: 'CORTEX_ENABLE_REST'
},
gcpServiceAccountKey: {
format: String,
default: null,
env: 'GCP_SERVICE_ACCOUNT_KEY',
sensitive: true
},
models: {
format: Object,
default: {
"oai-gpturbo": {
"type": "OPENAI-CHAT",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "gpt-3.5-turbo"
},
"requestsPerSecond": 10,
"maxTokenLength": 8192,
"supportsStreaming": true,
},
"oai-whisper": {
"type": "OPENAI-WHISPER",
"url": "https://api.openai.com/v1/audio/transcriptions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}"
},
"params": {
"model": "whisper-1"
},
},
"neuralspace": {
"type": "NEURALSPACE",
"url": "https://voice.neuralspace.ai/api/v2/jobs",
"headers": {
"Authorization": "{{NEURALSPACE_API_KEY}}",
},
},
"azure-cognitive": {
"type": "AZURE-COGNITIVE",
"url": "{{{AZURE_COGNITIVE_API_URL}}}",
"headers": {
"api-key": "{{AZURE_COGNITIVE_API_KEY}}",
"Content-Type": "application/json"
},
"requestsPerSecond": 10
},
"oai-embeddings": {
"type": "OPENAI-EMBEDDINGS",
"url": "https://api.openai.com/v1/embeddings",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "text-embedding-ada-002"
},
"maxTokenLength": 8192,
},
"oai-gpt4o": {
"type": "OPENAI-VISION",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "gpt-4o"
},
"requestsPerSecond": 50,
"maxTokenLength": 131072,
"maxReturnTokens": 4096,
"supportsStreaming": true
},
"oai-gpt4o-mini": {
"type": "OPENAI-VISION",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "gpt-4o-mini"
},
"requestsPerSecond": 50,
"maxTokenLength": 131072,
"maxReturnTokens": 4096,
"supportsStreaming": true
},
"oai-o1-mini": {
"type": "OPENAI-REASONING",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "o1-mini"
},
"requestsPerSecond": 10,
"maxTokenLength": 128000,
"maxReturnTokens": 65536,
"supportsStreaming": false
},
"oai-o1-preview": {
"type": "OPENAI-REASONING",
"url": "https://api.openai.com/v1/chat/completions",
"headers": {
"Authorization": "Bearer {{OPENAI_API_KEY}}",
"Content-Type": "application/json"
},
"params": {
"model": "o1-preview"
},
"requestsPerSecond": 10,
"maxTokenLength": 128000,
"maxReturnTokens": 32768,
"supportsStreaming": false
},
"azure-bing": {
"type": "AZURE-BING",
"url": "https://api.bing.microsoft.com/v7.0/search",
"headers": {
"Ocp-Apim-Subscription-Key": "{{AZURE_BING_KEY}}",
"Content-Type": "application/json"
},
"requestsPerSecond": 10,
"maxTokenLength": 200000
},
"runware-flux-schnell": {
"type": "RUNWARE-AI",
"url": "https://api.runware.ai/v1",
"headers": {
"Content-Type": "application/json"
},
},
"replicate-flux-11-pro": {
"type": "REPLICATE-API",
"url": "https://api.replicate.com/v1/models/black-forest-labs/flux-1.1-pro/predictions",
"headers": {
"Prefer": "wait=60",
"Authorization": "Token {{REPLICATE_API_KEY}}",
"Content-Type": "application/json"
},
},
"replicate-flux-1-schnell": {
"type": "REPLICATE-API",
"url": "https://api.replicate.com/v1/models/black-forest-labs/flux-schnell/predictions",
"headers": {
"Prefer": "wait=10",
"Authorization": "Token {{REPLICATE_API_KEY}}",
"Content-Type": "application/json"
},
},
"replicate-flux-1-dev": {
"type": "REPLICATE-API",
"url": "https://api.replicate.com/v1/models/black-forest-labs/flux-dev/predictions",
"headers": {
"Prefer": "wait",
"Authorization": "Token {{REPLICATE_API_KEY}}",
"Content-Type": "application/json"
},
},
"replicate-recraft-v3": {
"type": "REPLICATE-API",
"url": "https://api.replicate.com/v1/models/recraft-ai/recraft-v3/predictions",
"headers": {
"Prefer": "wait",
"Authorization": "Token {{REPLICATE_API_KEY}}",
"Content-Type": "application/json"
},
},
"azure-video-translate": {
"type": "AZURE-VIDEO-TRANSLATE",
"headers": {
"Content-Type": "application/json"
},
"supportsStreaming": true,
}
},
env: 'CORTEX_MODELS'
},
azureVideoTranslationApiUrl: {
format: String,
default: 'http://127.0.0.1:5005',
env: 'AZURE_VIDEO_TRANSLATION_API_URL'
},
openaiApiKey: {
format: String,
default: null,
env: 'OPENAI_API_KEY',
sensitive: true
},
openaiApiUrl: {
format: String,
default: 'https://api.openai.com/v1/completions',
env: 'OPENAI_API_URL'
},
openaiDefaultModel: {
format: String,
default: 'gpt-3.5-turbo',
env: 'OPENAI_DEFAULT_MODEL'
},
pathways: {
format: Object,
default: {}
},
pathwaysPath: {
format: String,
default: path.join(process.cwd(), '/pathways'),
env: 'CORTEX_PATHWAYS_PATH'
},
PORT: {
format: 'port',
default: 4000,
env: 'CORTEX_PORT'
},
storageConnectionString: {
doc: 'Connection string used for access to Storage',
format: '*',
default: '',
sensitive: true,
env: 'STORAGE_CONNECTION_STRING'
},
redisEncryptionKey: {
format: String,
default: null,
env: 'REDIS_ENCRYPTION_KEY',
sensitive: true
},
replicateApiKey: {
format: String,
default: null,
env: 'REPLICATE_API_KEY',
sensitive: true
},
runwareAiApiKey: {
format: String,
default: null,
env: 'RUNWARE_API_KEY',
sensitive: true
},
dalleImageApiUrl: {
format: String,
default: 'null',
env: 'DALLE_IMAGE_API_URL'
},
whisperMediaApiUrl: {
format: String,
default: 'null',
env: 'WHISPER_MEDIA_API_URL'
},
whisperTSApiUrl: {
format: String,
default: null,
env: 'WHISPER_TS_API_URL'
},
subscriptionKeepAlive: {
format: Number,
default: 0,
env: 'SUBSCRIPTION_KEEP_ALIVE'
},
neuralSpaceApiKey: {
format: String,
default: null,
env: 'NEURALSPACE_API_KEY'
},
});
// Read in environment variables and set up service configuration
const configFile = config.get('cortexConfigFile');
// Load config file
if (configFile && fs.existsSync(configFile)) {
logger.info(`Loading config from ${configFile}`);
config.loadFile(configFile);
} else {
const openaiApiKey = config.get('openaiApiKey');
if (!openaiApiKey) {
const errorString = 'No config file or api key specified. Please set the OPENAI_API_KEY to use OAI or use CORTEX_CONFIG_FILE environment variable to point at the Cortex configuration for your project.';
logger.error(errorString);
throw new Error(errorString);
} else {
logger.info(`Using default model with OPENAI_API_KEY environment variable`)
}
}
if (config.get('gcpServiceAccountKey')) {
const gcpAuthTokenHelper = new GcpAuthTokenHelper(config.getProperties());
config.set('gcpAuthTokenHelper', gcpAuthTokenHelper);
}
// Load dynamic pathways from JSON file or cloud storage
const createDynamicPathwayManager = async (config, basePathway) => {
const { dynamicPathwayConfig } = config.getProperties();
if (!dynamicPathwayConfig) {
return null;
}
const storageConfig = {
storageType: dynamicPathwayConfig.storageType || 'local',
filePath: dynamicPathwayConfig.filePath || "./dynamic/pathways.json",
azureStorageConnectionString: dynamicPathwayConfig.azureStorageConnectionString,
azureContainerName: dynamicPathwayConfig.azureContainerName || 'cortexdynamicpathways',
awsAccessKeyId: dynamicPathwayConfig.awsAccessKeyId,
awsSecretAccessKey: dynamicPathwayConfig.awsSecretAccessKey,
awsRegion: dynamicPathwayConfig.awsRegion,
awsBucketName: dynamicPathwayConfig.awsBucketName || 'cortexdynamicpathways',
publishKey: dynamicPathwayConfig.publishKey,
};
const pathwayManager = new PathwayManager(storageConfig, basePathway);
try {
const dynamicPathways = await pathwayManager.initialize();
logger.info(`Dynamic pathways loaded successfully`);
logger.info(`Loaded dynamic pathways for users: [${Object.keys(dynamicPathways).join(", ")}]`);
return pathwayManager;
} catch (error) {
logger.error(`Error loading dynamic pathways: ${error.message}`);
return pathwayManager;
}
};
// Build and load pathways to config
const buildPathways = async (config) => {
const { pathwaysPath, corePathwaysPath, basePathwayPath } = config.getProperties();
const basePathwayURL = pathToFileURL(basePathwayPath).toString();
// Load cortex base pathway
const basePathway = await import(basePathwayURL).then(module => module.default);
// Helper function to recursively load pathway files
const loadPathwaysFromDir = async (dirPath) => {
const pathways = {};
try {
const files = await readdir(dirPath, { withFileTypes: true });
for (const file of files) {
const fullPath = path.join(dirPath, file.name);
if (file.isDirectory()) {
// Skip the shared directory
if (file.name === 'shared') continue;
// Recursively load pathways from other subdirectories
const subPathways = await loadPathwaysFromDir(fullPath);
Object.assign(pathways, subPathways);
} else if (file.name.endsWith('.js')) {
// Load individual pathway file
const pathwayURL = pathToFileURL(fullPath).toString();
const pathway = await import(pathwayURL).then(module => module.default || module);
const pathwayName = path.basename(file.name, '.js');
pathways[pathwayName] = pathway;
}
}
} catch (error) {
logger.error(`Error loading pathways from ${dirPath}: ${error.message}`);
}
return pathways;
};
// Load core pathways
logger.info(`Loading core pathways from ${corePathwaysPath}`);
let loadedPathways = await loadPathwaysFromDir(corePathwaysPath);
// Load custom pathways and override core pathways if same
if (pathwaysPath && fs.existsSync(pathwaysPath)) {
logger.info(`Loading custom pathways from ${pathwaysPath}`);
const customPathways = await loadPathwaysFromDir(pathwaysPath);
loadedPathways = { ...loadedPathways, ...customPathways };
}
const { DYNAMIC_PATHWAYS_CONFIG_FILE, DYNAMIC_PATHWAYS_CONFIG_JSON } = process.env;
let dynamicPathwayConfig;
// Load dynamic pathways
let pathwayManager;
try {
if (DYNAMIC_PATHWAYS_CONFIG_FILE) {
logger.info(`Reading dynamic pathway config from ${DYNAMIC_PATHWAYS_CONFIG_FILE}`);
dynamicPathwayConfig = JSON.parse(fs.readFileSync(DYNAMIC_PATHWAYS_CONFIG_FILE, 'utf8'));
} else if (DYNAMIC_PATHWAYS_CONFIG_JSON) {
logger.info(`Reading dynamic pathway config from DYNAMIC_PATHWAYS_CONFIG_JSON variable`);
dynamicPathwayConfig = JSON.parse(DYNAMIC_PATHWAYS_CONFIG_JSON);
}
else {
logger.warn('Dynamic pathways are not enabled. Please set the DYNAMIC_PATHWAYS_CONFIG_FILE or DYNAMIC_PATHWAYS_CONFIG_JSON environment variable to enable dynamic pathways.');
}
config.load({ dynamicPathwayConfig });
pathwayManager = await createDynamicPathwayManager(config, basePathway);
} catch (error) {
logger.error(`Error loading dynamic pathways: ${error.message}`);
process.exit(1);
}
// This is where we integrate pathway overrides from the config
// file. This can run into a partial definition issue if the
// config file contains pathways that no longer exist.
const pathways = config.get('pathways');
for (const [key, def] of Object.entries(loadedPathways)) {
const pathway = { ...basePathway, name: key, objName: key.charAt(0).toUpperCase() + key.slice(1), ...def, ...pathways[key] };
pathways[def.name || key] = pathways[key] = pathway;
}
// Add pathways to config
config.load({ pathways });
return { pathwayManager, pathways };
}
// Build and load models to config
const buildModels = (config) => {
const { models } = config.getProperties();
// iterate over each model
for (let [key, model] of Object.entries(models)) {
if (!model.name) {
model.name = key;
}
// if model is in old format, convert it to new format
if (!model.endpoints) {
model = {
...model,
endpoints: [
{
name: "default",
url: model.url,
headers: model.headers,
params: model.params,
requestsPerSecond: model.requestsPerSecond
}
]
};
}
// compile handlebars templates for each endpoint
model.endpoints = model.endpoints.map(endpoint =>
JSON.parse(HandleBars.compile(JSON.stringify(endpoint))({ ...model, ...config.getEnv(), ...config.getProperties() }))
);
models[key] = model;
}
// Add constructed models to config
config.load({ models });
// Check that models are specified, Cortex cannot run without a model
if (Object.keys(config.get('models')).length <= 0) {
const errorString = 'No models specified! Please set the models in your config file or via CORTEX_MODELS environment variable to point at the models for your project.';
logger.error(errorString);
throw new Error(errorString);
}
// Set default model name to the first model in the config in case no default is specified
if (!config.get('defaultModelName')) {
logger.warn('No default model specified, using first model as default.');
config.load({ defaultModelName: Object.keys(config.get('models'))[0] });
}
return models;
}
// TODO: Perform validation
// config.validate({ allowed: 'strict' });
export { config, buildPathways, buildModels };