Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(llm-server): llm server timesout issue #173

Merged
merged 3 commits into from
Mar 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions llm-server/src/llm-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,27 @@ export class LLMProvider {
return model;
}

async chat(input: MessageInput): Promise<string> {
async chat(input: MessageInput, timeoutMs: number): Promise<string> {
try {
const model = this.getModelInstance(input.model);
const completion = await model.chat(input.messages);
return completion.choices[0].message.content || '';

// Set a timeout dynamically based on the provided value
const timeoutPromise = new Promise<string>((_, reject) =>
setTimeout(
() => reject(new Error('Chat request timed out')),
timeoutMs,
),
);

// Race between the actual model call and the timeout
const completion = await Promise.race([
model.chat(input.messages),
timeoutPromise,
]);

return (completion as any).choices[0].message.content || '';
} catch (error) {
this.logger.error('Error in chat:', error);
this.logger.error(`Error in chat (Timeout: ${timeoutMs}ms):`, error);
throw error;
}
}
Expand Down
3 changes: 2 additions & 1 deletion llm-server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ export class App {
}
} else {
// Handle regular response
const response = await this.llmProvider.chat(input);
// TODO make it to dynamic Now is 200 second by defult.
const response = await this.llmProvider.chat(input, 200000);
res.json({
model: input.model,
choices: [
Expand Down
1 change: 0 additions & 1 deletion llm-server/src/model/remote-model-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export class RemoteOpenAIModelEngine implements ModelInstance {
interval: 1000, // per 1000ms (1 second)
carryoverConcurrencyCount: true, // Carry over pending tasks
// FIXME: hack way to set up timeout
timeout: 120000, // 120 second timeout to accommodate longer streams
});

// Log queue events for monitoring
Expand Down
Loading