From 9485d401808649d905ea6b244cac771819aae201 Mon Sep 17 00:00:00 2001 From: marknguyen1302 Date: Wed, 31 Jul 2024 13:48:08 +0700 Subject: [PATCH] chore: show error from remote engine, lint --- .../src/domain/abstracts/oai.abstract.ts | 1 - cortex-js/src/extensions/cohere.engine.ts | 75 ++++++++++--------- cortex-js/src/extensions/openrouter.engine.ts | 7 +- .../controllers/chat.controller.ts | 52 +++++++++++-- .../controllers/engines.controller.ts | 2 +- 5 files changed, 90 insertions(+), 47 deletions(-) diff --git a/cortex-js/src/domain/abstracts/oai.abstract.ts b/cortex-js/src/domain/abstracts/oai.abstract.ts index 7bed9f844..eef2e1c16 100644 --- a/cortex-js/src/domain/abstracts/oai.abstract.ts +++ b/cortex-js/src/domain/abstracts/oai.abstract.ts @@ -40,7 +40,6 @@ export abstract class OAIEngineExtension extends EngineExtension { }), ); - if (!response) { throw new Error('No response'); } diff --git a/cortex-js/src/extensions/cohere.engine.ts b/cortex-js/src/extensions/cohere.engine.ts index 76c4f2cd3..505fb7a5b 100644 --- a/cortex-js/src/extensions/cohere.engine.ts +++ b/cortex-js/src/extensions/cohere.engine.ts @@ -8,16 +8,16 @@ import { EngineStatus } from '@/domain/abstracts/engine.abstract'; import { ChatCompletionMessage } from '@/infrastructure/dtos/chat/chat-completion-message.dto'; enum RoleType { - user = 'USER', - chatbot = 'CHATBOT', - system = 'SYSTEM', - } - - type CoherePayloadType = { - chat_history?: Array<{ role: RoleType; message: string }> - message?: string - preamble?: string - } + user = 'USER', + chatbot = 'CHATBOT', + system = 'SYSTEM', +} + +type CoherePayloadType = { + chat_history?: Array<{ role: RoleType; message: string }>; + message?: string; + preamble?: string; +}; /** * A class that implements the InferenceExtension interface from the @janhq/core package. @@ -62,9 +62,9 @@ export default class CoHereEngineExtension extends OAIEngineExtension { } transformPayload = (payload: any): CoherePayloadType => { - console.log('payload', payload) + console.log('payload', payload); if (payload.messages.length === 0) { - return {} + return {}; } const { messages, ...params } = payload; @@ -73,31 +73,34 @@ export default class CoHereEngineExtension extends OAIEngineExtension { chat_history: [], message: '', }; - (messages as ChatCompletionMessage[]).forEach((item: ChatCompletionMessage, index: number) => { - // Assign the message of the last item to the `message` property - if (index === messages.length - 1) { - convertedData.message = item.content as string - return - } - if (item.role === 'user') { - convertedData.chat_history!!.push({ - role: 'USER' as RoleType, - message: item.content as string, - }) - } else if (item.role === 'assistant') { - convertedData.chat_history!!.push({ - role: 'CHATBOT' as RoleType, - message: item.content as string, - }) - } else if (item.role === 'system') { - convertedData.preamble = item.content as string - } - }) - return convertedData - } + (messages as ChatCompletionMessage[]).forEach( + (item: ChatCompletionMessage, index: number) => { + // Assign the message of the last item to the `message` property + if (index === messages.length - 1) { + convertedData.message = item.content as string; + return; + } + if (item.role === 'user') { + convertedData.chat_history!.push({ + role: 'USER' as RoleType, + message: item.content as string, + }); + } else if (item.role === 'assistant') { + convertedData.chat_history!.push({ + role: 'CHATBOT' as RoleType, + message: item.content as string, + }); + } else if (item.role === 'system') { + convertedData.preamble = item.content as string; + } + }, + ); + return convertedData; + }; transformResponse = (data: any) => { - const text = typeof data === 'object' ? data.text : JSON.parse(data).text ?? '' + const text = + typeof data === 'object' ? data.text : (JSON.parse(data).text ?? ''); return JSON.stringify({ choices: [ { @@ -107,5 +110,5 @@ export default class CoHereEngineExtension extends OAIEngineExtension { }, ], }); - } + }; } diff --git a/cortex-js/src/extensions/openrouter.engine.ts b/cortex-js/src/extensions/openrouter.engine.ts index 643670edd..b7123963f 100644 --- a/cortex-js/src/extensions/openrouter.engine.ts +++ b/cortex-js/src/extensions/openrouter.engine.ts @@ -50,9 +50,8 @@ export default class OpenRouterEngineExtension extends OAIEngineExtension { transformPayload = (data: any): any => { return { - ...data, - model:"openrouter/auto", - } + ...data, + model: 'openrouter/auto', + }; }; - } diff --git a/cortex-js/src/infrastructure/controllers/chat.controller.ts b/cortex-js/src/infrastructure/controllers/chat.controller.ts index dc1cb7d1a..cce182e4e 100644 --- a/cortex-js/src/infrastructure/controllers/chat.controller.ts +++ b/cortex-js/src/infrastructure/controllers/chat.controller.ts @@ -33,8 +33,9 @@ export class ChatController { @Body() createChatDto: CreateChatCompletionDto, @Res() res: Response, ) { - const { stream } = createChatDto; - + let { stream } = createChatDto; + stream = false; + createChatDto.stream = stream; this.chatService .inference(createChatDto, extractCommonHeaders(headers)) .then((response) => { @@ -46,9 +47,50 @@ export class ChatController { res.json(response); } }) - .catch((error) => - res.status(error.statusCode ?? 400).send(error.message), - ); + .catch((error) => { + const statusCode = error.response?.status ?? 400; + let errorMessage; + if (!stream) { + const data = error.response?.data; + return res + .status(statusCode) + .send( + data.error?.message || + data.message || + error.message || + 'An error occurred', + ); + } + const streamResponse = error.response?.data; + let data = ''; + + streamResponse.on('data', (chunk: any) => { + data += chunk; + }); + + streamResponse.on('end', () => { + try { + const jsonResponse = JSON.parse(data); + errorMessage = + jsonResponse.error?.message || + jsonResponse.message || + error.message || + 'An error occurred'; + } catch (err) { + errorMessage = 'An error occurred while processing the response'; + } + return res + .status(error.statusCode ?? 400) + .send(errorMessage || error.message || 'An error occurred'); + }); + + streamResponse.on('error', (streamError: any) => { + errorMessage = streamError.message ?? 'An error occurred'; + return res + .status(error.statusCode ?? 400) + .send(errorMessage || error.message || 'An error occurred'); + }); + }); this.telemetryUsecases.addEventToQueue({ name: EventName.CHAT, diff --git a/cortex-js/src/infrastructure/controllers/engines.controller.ts b/cortex-js/src/infrastructure/controllers/engines.controller.ts index c91b22aac..9eb7fdcb5 100644 --- a/cortex-js/src/infrastructure/controllers/engines.controller.ts +++ b/cortex-js/src/infrastructure/controllers/engines.controller.ts @@ -112,7 +112,7 @@ export class EnginesController { }) @Patch(':name(*)') update(@Param('name') name: string, @Body() configs?: any | undefined) { - console.log('configs', configs) + console.log('configs', configs); return this.enginesUsecases.updateConfigs( configs.config, configs.value,