mirror of
https://github.com/danny-avila/LibreChat.git
synced 2026-01-06 10:38:50 +01:00
🛠️ fix: Completion Edge Cases & Improve Error Handling UX (#3968)
* fix: edge cases concerning completion response as an array * refactor: improve invalid request error UX
This commit is contained in:
parent
0148b9b097
commit
341e086d70
6 changed files with 21 additions and 7 deletions
|
|
@ -3,6 +3,7 @@ const fetch = require('node-fetch');
|
|||
const {
|
||||
supportsBalanceCheck,
|
||||
isAgentsEndpoint,
|
||||
paramEndpoints,
|
||||
ErrorTypes,
|
||||
Constants,
|
||||
CacheKeys,
|
||||
|
|
@ -561,6 +562,7 @@ class BaseClient {
|
|||
});
|
||||
}
|
||||
|
||||
/** @type {string|string[]|undefined} */
|
||||
const completion = await this.sendCompletion(payload, opts);
|
||||
this.abortController.requestCompleted = true;
|
||||
|
||||
|
|
@ -580,9 +582,11 @@ class BaseClient {
|
|||
|
||||
if (typeof completion === 'string') {
|
||||
responseMessage.text = addSpaceIfNeeded(generation) + completion;
|
||||
} else if (completion) {
|
||||
} else if (Array.isArray(completion) && paramEndpoints.has(this.options.endpoint)) {
|
||||
responseMessage.text = '';
|
||||
responseMessage.content = completion;
|
||||
} else if (Array.isArray(completion)) {
|
||||
responseMessage.text = addSpaceIfNeeded(generation) + completion.join('');
|
||||
}
|
||||
|
||||
if (
|
||||
|
|
|
|||
|
|
@ -1271,7 +1271,7 @@ ${convo}
|
|||
const { choices } = chatCompletion;
|
||||
if (!Array.isArray(choices) || choices.length === 0) {
|
||||
logger.warn('[OpenAIClient] Chat completion response has no choices');
|
||||
return intermediateReply;
|
||||
return intermediateReply.join('');
|
||||
}
|
||||
|
||||
const { message, finish_reason } = choices[0] ?? {};
|
||||
|
|
@ -1281,7 +1281,7 @@ ${convo}
|
|||
|
||||
if (!message) {
|
||||
logger.warn('[OpenAIClient] Message is undefined in chatCompletion response');
|
||||
return intermediateReply;
|
||||
return intermediateReply.join('');
|
||||
}
|
||||
|
||||
if (typeof message.content !== 'string' || message.content.trim() === '') {
|
||||
|
|
@ -1316,7 +1316,7 @@ ${convo}
|
|||
logger.error('[OpenAIClient] Known OpenAI error:', err);
|
||||
return intermediateReply.join('');
|
||||
} else if (err instanceof OpenAI.APIError) {
|
||||
if (intermediateReply) {
|
||||
if (intermediateReply.length > 0) {
|
||||
return intermediateReply.join('');
|
||||
} else {
|
||||
throw err;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue