Skip to content

Commit c97940b

Browse files
fix(llm-server): llm server timesout issue (#173)
<!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Chat requests now include a configurable timeout (default: 200 seconds) to ensure timely responses and provide clearer feedback when delays occur. - **Chores** - Removed an outdated timeout limit in background operations to enhance flexibility for longer-running tasks. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent ef243ae commit c97940b

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

llm-server/src/llm-provider.ts

+18-4
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,27 @@ export class LLMProvider {
104104
return model;
105105
}
106106

107-
async chat(input: MessageInput): Promise<string> {
107+
async chat(input: MessageInput, timeoutMs: number): Promise<string> {
108108
try {
109109
const model = this.getModelInstance(input.model);
110-
const completion = await model.chat(input.messages);
111-
return completion.choices[0].message.content || '';
110+
111+
// Set a timeout dynamically based on the provided value
112+
const timeoutPromise = new Promise<string>((_, reject) =>
113+
setTimeout(
114+
() => reject(new Error('Chat request timed out')),
115+
timeoutMs,
116+
),
117+
);
118+
119+
// Race between the actual model call and the timeout
120+
const completion = await Promise.race([
121+
model.chat(input.messages),
122+
timeoutPromise,
123+
]);
124+
125+
return (completion as any).choices[0].message.content || '';
112126
} catch (error) {
113-
this.logger.error('Error in chat:', error);
127+
this.logger.error(`Error in chat (Timeout: ${timeoutMs}ms):`, error);
114128
throw error;
115129
}
116130
}

llm-server/src/main.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ export class App {
137137
}
138138
} else {
139139
// Handle regular response
140-
const response = await this.llmProvider.chat(input);
140+
// TODO make it to dynamic Now is 200 second by defult.
141+
const response = await this.llmProvider.chat(input, 200000);
141142
res.json({
142143
model: input.model,
143144
choices: [

llm-server/src/model/remote-model-instance.ts

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export class RemoteOpenAIModelEngine implements ModelInstance {
3030
interval: 1000, // per 1000ms (1 second)
3131
carryoverConcurrencyCount: true, // Carry over pending tasks
3232
// FIXME: hack way to set up timeout
33-
timeout: 120000, // 120 second timeout to accommodate longer streams
3433
});
3534

3635
// Log queue events for monitoring

0 commit comments

Comments
 (0)