Skip to content

Commit 3c96644

Browse files
authored
🛠️ fix: Error Message Parsing and ChatOpenAI credentials (danny-avila#1482)
* refactor(createLLM): ensure ChatOpenAI class always uses client-defined openAIApiKey; move typedefs to main def file * refactor(useSSE): improve error message parsing in error handler
1 parent 84ad092 commit 3c96644

File tree

3 files changed

+47
-35
lines changed

3 files changed

+47
-35
lines changed

api/app/clients/llm/createLLM.js

Lines changed: 1 addition & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,6 @@ const { ChatOpenAI } = require('langchain/chat_models/openai');
22
const { sanitizeModelName } = require('../../../utils');
33
const { isEnabled } = require('../../../server/utils');
44

5-
/**
6-
* @typedef {Object} ModelOptions
7-
* @property {string} modelName - The name of the model.
8-
* @property {number} [temperature] - The temperature setting for the model.
9-
* @property {number} [presence_penalty] - The presence penalty setting.
10-
* @property {number} [frequency_penalty] - The frequency penalty setting.
11-
* @property {number} [max_tokens] - The maximum number of tokens to generate.
12-
*/
13-
14-
/**
15-
* @typedef {Object} ConfigOptions
16-
* @property {string} [basePath] - The base path for the API requests.
17-
* @property {Object} [baseOptions] - Base options for the API requests, including headers.
18-
* @property {Object} [httpAgent] - The HTTP agent for the request.
19-
* @property {Object} [httpsAgent] - The HTTPS agent for the request.
20-
*/
21-
22-
/**
23-
* @typedef {Object} Callbacks
24-
* @property {Function} [handleChatModelStart] - A callback function for handleChatModelStart
25-
* @property {Function} [handleLLMEnd] - A callback function for handleLLMEnd
26-
* @property {Function} [handleLLMError] - A callback function for handleLLMError
27-
*/
28-
29-
/**
30-
* @typedef {Object} AzureOptions
31-
* @property {string} [azureOpenAIApiKey] - The Azure OpenAI API key.
32-
* @property {string} [azureOpenAIApiInstanceName] - The Azure OpenAI API instance name.
33-
* @property {string} [azureOpenAIApiDeploymentName] - The Azure OpenAI API deployment name.
34-
* @property {string} [azureOpenAIApiVersion] - The Azure OpenAI API version.
35-
*/
36-
375
/**
386
* Creates a new instance of a language model (LLM) for chat interactions.
397
*
@@ -96,6 +64,7 @@ function createLLM({
9664
configuration,
9765
...azureOptions,
9866
...modelOptions,
67+
...credentials,
9968
callbacks,
10069
},
10170
configOptions,

api/typedefs.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,3 +337,39 @@
337337
* @property {number} order - The order of the endpoint.
338338
* @memberof typedefs
339339
*/
340+
341+
/**
342+
* @typedef {Object} ModelOptions
343+
* @property {string} modelName - The name of the model.
344+
* @property {number} [temperature] - The temperature setting for the model.
345+
* @property {number} [presence_penalty] - The presence penalty setting.
346+
* @property {number} [frequency_penalty] - The frequency penalty setting.
347+
* @property {number} [max_tokens] - The maximum number of tokens to generate.
348+
* @memberof typedefs
349+
*/
350+
351+
/**
352+
* @typedef {Object} ConfigOptions
353+
* @property {string} [basePath] - The base path for the API requests.
354+
* @property {Object} [baseOptions] - Base options for the API requests, including headers.
355+
* @property {Object} [httpAgent] - The HTTP agent for the request.
356+
* @property {Object} [httpsAgent] - The HTTPS agent for the request.
357+
* @memberof typedefs
358+
*/
359+
360+
/**
361+
* @typedef {Object} Callbacks
362+
* @property {Function} [handleChatModelStart] - A callback function for handleChatModelStart
363+
* @property {Function} [handleLLMEnd] - A callback function for handleLLMEnd
364+
* @property {Function} [handleLLMError] - A callback function for handleLLMError
365+
* @memberof typedefs
366+
*/
367+
368+
/**
369+
* @typedef {Object} AzureOptions
370+
* @property {string} [azureOpenAIApiKey] - The Azure OpenAI API key.
371+
* @property {string} [azureOpenAIApiInstanceName] - The Azure OpenAI API instance name.
372+
* @property {string} [azureOpenAIApiDeploymentName] - The Azure OpenAI API deployment name.
373+
* @property {string} [azureOpenAIApiVersion] - The Azure OpenAI API version.
374+
* @memberof typedefs
375+
*/

client/src/hooks/useSSE.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,16 +209,23 @@ export default function useSSE(submission: TSubmission | null, index = 0) {
209209
};
210210

211211
const errorHandler = ({ data, submission }: { data?: TResData; submission: TSubmission }) => {
212-
const { messages, message } = submission;
212+
const { messages, message, initialResponse } = submission;
213213

214214
const conversationId = message?.conversationId ?? submission?.conversationId;
215215
const parseErrorResponse = (data: TResData | Partial<TMessage>) => {
216216
const metadata = data['responseMessage'] ?? data;
217-
return tMessageSchema.parse({
217+
const errorMessage = {
218+
...initialResponse,
218219
...metadata,
219220
error: true,
220221
parentMessageId: message?.messageId,
221-
});
222+
};
223+
224+
if (!errorMessage.messageId) {
225+
errorMessage.messageId = v4();
226+
}
227+
228+
return tMessageSchema.parse(errorMessage);
222229
};
223230

224231
if (!data) {

0 commit comments

Comments
 (0)