mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-18 01:10:14 +01:00
fix(OpenAIClient): use official SDK to identify client and avoid false Rate Limit Error (#1161)
* chore: add eslint ignore unused var pattern * feat: add extractBaseURL helper for valid OpenAI reverse proxies, with tests * feat(OpenAIClient): add new chatCompletion using official OpenAI node SDK * fix(ci): revert change to FORCE_PROMPT condition
This commit is contained in:
parent
ed3d7c9f80
commit
5ab9802aa9
10 changed files with 297 additions and 12 deletions
30
api/app/clients/tools/util/handleOpenAIErrors.js
Normal file
30
api/app/clients/tools/util/handleOpenAIErrors.js
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
const OpenAI = require('openai');
|
||||
|
||||
/**
|
||||
* Handles errors that may occur when making requests to OpenAI's API.
|
||||
* It checks the instance of the error and prints a specific warning message
|
||||
* to the console depending on the type of error encountered.
|
||||
* It then calls an optional error callback function with the error object.
|
||||
*
|
||||
* @param {Error} err - The error object thrown by OpenAI API.
|
||||
* @param {Function} errorCallback - A callback function that is called with the error object.
|
||||
* @param {string} [context='stream'] - A string providing context where the error occurred, defaults to 'stream'.
|
||||
*/
|
||||
async function handleOpenAIErrors(err, errorCallback, context = 'stream') {
|
||||
if (err instanceof OpenAI.APIError && err?.message?.includes('abort')) {
|
||||
console.warn(`[OpenAIClient.chatCompletion][${context}] Aborted Message`);
|
||||
}
|
||||
if (err instanceof OpenAI.OpenAIError && err?.message?.includes('missing finish_reason')) {
|
||||
console.warn(`[OpenAIClient.chatCompletion][${context}] Missing finish_reason`);
|
||||
} else if (err instanceof OpenAI.APIError) {
|
||||
console.warn(`[OpenAIClient.chatCompletion][${context}] API Error`);
|
||||
} else {
|
||||
console.warn(`[OpenAIClient.chatCompletion][${context}] Unhandled error type`);
|
||||
}
|
||||
|
||||
if (errorCallback) {
|
||||
errorCallback(err);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = handleOpenAIErrors;
|
||||
Loading…
Add table
Add a link
Reference in a new issue