Skip to content

Commit

Permalink
🛠️ refactor: Model Loading and Custom Endpoint Error Handling (#1849)
Browse files Browse the repository at this point in the history
* fix: handle non-assistant role ChatCompletionMessage error

* refactor(ModelController): decouple res.send from loading/caching models

* fix(custom/initializeClient): only fetch custom endpoint models if models.fetch is true

* refactor(validateModel): load models if modelsConfig is not yet cached

* docs: update on file upload rate limiting
  • Loading branch information
danny-avila committed Feb 20, 2024
1 parent 542494f commit dd8038b
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 10 deletions.
14 changes: 14 additions & 0 deletions api/app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -994,6 +994,7 @@ ${convo}
}

let chatCompletion;
/** @type {OpenAI} */
const openai = new OpenAI({
apiKey: this.apiKey,
...opts,
Expand Down Expand Up @@ -1046,6 +1047,16 @@ ${convo}
.on('error', (err) => {
handleOpenAIErrors(err, errorCallback, 'stream');
})
.on('finalChatCompletion', (finalChatCompletion) => {
const finalMessage = finalChatCompletion?.choices?.[0]?.message;
if (finalMessage && finalMessage?.role !== 'assistant') {
finalChatCompletion.choices[0].message.role = 'assistant';
}

if (finalMessage && !finalMessage?.content?.trim()) {
finalChatCompletion.choices[0].message.content = intermediateReply;
}
})
.on('finalMessage', (message) => {
if (message?.role !== 'assistant') {
stream.messages.push({ role: 'assistant', content: intermediateReply });
Expand Down Expand Up @@ -1117,6 +1128,9 @@ ${convo}
err?.message?.includes(
'OpenAI error: Invalid final message: OpenAI expects final message to include role=assistant',
) ||
err?.message?.includes(
'stream ended without producing a ChatCompletionMessage with role=assistant',
) ||
err?.message?.includes('The server had an error processing your request') ||
err?.message?.includes('missing finish_reason') ||
err?.message?.includes('missing role') ||
Expand Down
17 changes: 13 additions & 4 deletions api/server/controllers/ModelController.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,29 @@ const { CacheKeys } = require('librechat-data-provider');
const { loadDefaultModels, loadConfigModels } = require('~/server/services/Config');
const { getLogStores } = require('~/cache');

async function modelController(req, res) {
/**
* Loads the models from the config.
* @param {Express.Request} req - The Express request object.
* @returns {Promise<TModelsConfig>} The models config.
*/
async function loadModels(req) {
const cache = getLogStores(CacheKeys.CONFIG_STORE);
const cachedModelsConfig = await cache.get(CacheKeys.MODELS_CONFIG);
if (cachedModelsConfig) {
res.send(cachedModelsConfig);
return;
return cachedModelsConfig;
}
const defaultModelsConfig = await loadDefaultModels(req);
const customModelsConfig = await loadConfigModels(req);

const modelConfig = { ...defaultModelsConfig, ...customModelsConfig };

await cache.set(CacheKeys.MODELS_CONFIG, modelConfig);
return modelConfig;
}

async function modelController(req, res) {
const modelConfig = await loadModels(req);
res.send(modelConfig);
}

module.exports = modelController;
module.exports = { modelController, loadModels };
7 changes: 6 additions & 1 deletion api/server/middleware/validateModel.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { EModelEndpoint, CacheKeys, ViolationTypes } = require('librechat-data-provider');
const { loadModels } = require('~/server/controllers/ModelController');
const { logViolation, getLogStores } = require('~/cache');
const { handleError } = require('~/server/utils');

Expand All @@ -17,7 +18,11 @@ const validateModel = async (req, res, next) => {
}

const cache = getLogStores(CacheKeys.CONFIG_STORE);
const modelsConfig = await cache.get(CacheKeys.MODELS_CONFIG);
let modelsConfig = await cache.get(CacheKeys.MODELS_CONFIG);
if (!modelsConfig) {
modelsConfig = await loadModels(req);
}

if (!modelsConfig) {
return handleError(res, { text: 'Models not loaded' });
}
Expand Down
8 changes: 4 additions & 4 deletions api/server/routes/models.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const express = require('express');
const router = express.Router();
const controller = require('../controllers/ModelController');
const { requireJwtAuth } = require('../middleware/');
const { modelController } = require('~/server/controllers/ModelController');
const { requireJwtAuth } = require('~/server/middleware/');

router.get('/', requireJwtAuth, controller);
const router = express.Router();
router.get('/', requireJwtAuth, modelController);

module.exports = router;
2 changes: 1 addition & 1 deletion api/server/services/Endpoints/custom/initializeClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const initializeClient = async ({ req, res, endpointOption }) => {

const cache = getLogStores(CacheKeys.TOKEN_CONFIG);
let endpointTokenConfig = await cache.get(endpoint);
if (!endpointTokenConfig) {
if (endpointConfig && endpointConfig.models.fetch && !endpointTokenConfig) {
await fetchModels({ apiKey: CUSTOM_API_KEY, baseURL: CUSTOM_BASE_URL, name: endpoint });
endpointTokenConfig = await cache.get(endpoint);
}
Expand Down
6 changes: 6 additions & 0 deletions api/typedefs.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@
* @memberof typedefs
*/

/**
* @exports TModelsConfig
* @typedef {import('librechat-data-provider').TModelsConfig} TModelsConfig
* @memberof typedefs
*/

/**
* @exports TPlugin
* @typedef {import('librechat-data-provider').TPlugin} TPlugin
Expand Down
3 changes: 3 additions & 0 deletions docs/features/mod_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ The project's current rate limiters are as follows (see below under setup for de
- Login and registration rate limiting
- [optional] Concurrent Message limiting (only X messages at a time per user)
- [optional] Message limiting (how often a user can send a message, configurable by IP and User)
- [optional] File Upload limiting: configurable through [`librechat.yaml` config file](https://docs.librechat.ai/install/configuration/custom_config.html#rate-limiting).

### Setup

The following are all of the related env variables to make use of and configure the mod system. Note this is also found in the [/.env.example](https://github.com/danny-avila/LibreChat/blob/main/.env.example) file, to be set in your own `.env` file.

**Note:** currently, most of these values are configured through the .env file, but they may soon migrate to be exclusively configured from the [`librechat.yaml` config file](https://docs.librechat.ai/install/configuration/custom_config.html#rate-limiting).

```bash
BAN_VIOLATIONS=true # Whether or not to enable banning users for violations (they will still be logged)
BAN_DURATION=1000 * 60 * 60 * 2 # how long the user and associated IP are banned for
Expand Down

0 comments on commit dd8038b

Please sign in to comment.