Skip to content

Commit 3fd7a20

Browse files
committed
Add timesout for each chat
1 parent a9c2fb0 commit 3fd7a20

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

llm-server/src/llm-provider.ts

+17-4
Original file line numberDiff line numberDiff line change
@@ -104,16 +104,29 @@ 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(() => reject(new Error('Chat request timed out')), timeoutMs),
114+
);
115+
116+
// Race between the actual model call and the timeout
117+
const completion = await Promise.race([
118+
model.chat(input.messages),
119+
timeoutPromise,
120+
]);
121+
122+
return (completion as any).choices[0].message.content || '';
112123
} catch (error) {
113-
this.logger.error('Error in chat:', error);
124+
this.logger.error(`Error in chat (Timeout: ${timeoutMs}ms):`, error);
114125
throw error;
115126
}
116127
}
128+
129+
117130

118131
async *chatStream(
119132
input: MessageInput,

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
@@ -73,7 +73,6 @@ export class RemoteOpenAIModelEngine implements ModelInstance {
7373
return await this.client.chat.completions.create({
7474
model: this.config.model,
7575
messages,
76-
temperature: 1, // Default to 0.7 if not specified
7776
});
7877
});
7978

0 commit comments

Comments
 (0)