The code imports Node.js modules, defines environment configurations, and exports a function launchChats
that launches chat services using Node.js. The function loops through the environment configurations and uses child_process
to run a new Node.js process for each configuration.
npm run import -- "start a bunch of llm rpc services"
const {spawn, spawnSync} = require("child_process");
const path = require('path')
const ENVIRONMENTS = [
void 0,
{
CHAT_PORT: 8181,
DEFAULT_MODEL: 'Default',
},
{
CHAT_PORT: 8282,
DEFAULT_MODEL: 'Meta',
},
{
CHAT_PORT: 8383,
DEFAULT_MODEL: 'DeepSeek',
},
{
CHAT_PORT: 8484,
DEFAULT_MODEL: 'Qwen',
},
{
CHAT_PORT: 8585,
DEFAULT_MODEL: 'Code',
},
{
CHAT_PORT: 8686,
DEFAULT_MODEL: 'Mistral',
},
]
function launchChats(botIndex = 0) {
if(parseInt(botIndex).toString() != botIndex.toString()) {
return
} else {
botIndex = parseInt(botIndex)
}
if(typeof botIndex != 'number' && !botIndex) {
return
}
for(let i = botIndex; i < ENVIRONMENTS.length; i++) {
if(!ENVIRONMENTS[i]) {
continue
}
spawn('node', ['--experimental-vm-modules', '-e', 'var result = require(\'./Core\').run()', '--', 'resume express chat service'], {
env: Object.assign({}, process.env, ENVIRONMENTS[i] ? ENVIRONMENTS[i] : {}),
stdio: [0, 1, 2],
//detached: true,
//shell: true,
cwd: path.dirname(__dirname)
})
if(botIndex) {
break
}
}
}
module.exports = launchChats
const { spawn, spawnSync } = require('child_process');
const path = require('path');
// Define environments as an array of configuration objects
const environments = [
{ chatPort: 8181, defaultModel: 'Default' },
{ chatPort: 8282, defaultModel: 'Meta' },
{ chatPort: 8383, defaultModel: 'DeepSeek' },
{ chatPort: 8484, defaultModel: 'Qwen' },
{ chatPort: 8585, defaultModel: 'Code' },
{ chatPort: 8686, defaultModel: 'Mistral' },
];
// Define environment defaults
const environmentDefaults = { defaultModel: 'Default' };
// Function to launch chat services
function launchChats(botIndex = 0) {
// Check if botIndex is a non-negative integer
if (typeof botIndex!== 'number' || botIndex < 0 || botIndex % 1!== 0) {
// TODO: handle invalid botIndex
return;
}
// Iterate over environments starting from the specified index
for (let i = botIndex; i < environments.length; i++) {
const environment = environments[i] || {};
const environmentConfig = Object.assign({}, environmentDefaults, environment);
// Spawn a new process for the chat service
spawn('node', ['--experimental-vm-modules', '-e', 'var result = require(\'./Core\').run()', '--','resume express chat service'], {
env: process.env,
...environmentConfig,
stdio: 'inherit',
cwd: path.dirname(__dirname),
});
// Break the loop if botIndex is specified
if (botIndex) {
break;
}
}
}
module.exports = launchChats;
Code Breakdown
const {spawn, spawnSync} = require("child_process");
const path = require('path')
This code imports two modules:
child_process
: a built-in Node.js module that allows running external commands and scripts.path
: a built-in Node.js module that provides utilities for working with file paths.const ENVIRONMENTS = [
//... list of environment configurations...
]
This code defines an array of environment configurations, where each configuration is an object containing key-value pairs that will be set as environment variables.
launchChats
Functionfunction launchChats(botIndex = 0) {
//...
}
This code defines a function named launchChats
that takes an optional botIndex
parameter with a default value of 0. The function is designed to launch chat services using Node.js.
for (let i = botIndex; i < ENVIRONMENTS.length; i++) {
//...
}
The function loops through the ENVIRONMENTS
array, starting from the botIndex
parameter. If botIndex
is not a valid number, the function returns immediately.
spawn('node', ['--experimental-vm-modules', '-e', 'var result = require(\'./Core\').run()', '--','resume express chat service'], {
//...
})
Inside the loop, the function uses the spawn
method from the child_process
module to launch a new process. The process runs the Node.js executable with the following arguments:
--experimental-vm-modules
: enables experimental VM modules-e
: executes the following code as an expressionvar result = require('./Core').run()
: imports the Core
module and calls its run()
method--
: separates the Node.js executable from the command-line argumentsresume express chat service
: the actual command-line argument that launches the chat serviceThe spawn
method is called with an options object that sets the environment variables, standard input/output streams, and current working directory.
if (botIndex) {
break
}
If botIndex
is a valid number greater than 0, the loop breaks after launching the first chat service.
module.exports = launchChats
The function is exported as a module, making it available for use in other parts of the application.