File tree 3 files changed +19
-6
lines changed
3 files changed +19
-6
lines changed Original file line number Diff line number Diff line change @@ -104,16 +104,29 @@ export class LLMProvider {
104
104
return model ;
105
105
}
106
106
107
- async chat ( input : MessageInput ) : Promise < string > {
107
+ async chat ( input : MessageInput , timeoutMs : number ) : Promise < string > {
108
108
try {
109
109
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 || '' ;
112
123
} catch ( error ) {
113
- this . logger . error ( ' Error in chat:' , error ) ;
124
+ this . logger . error ( ` Error in chat (Timeout: ${ timeoutMs } ms):` , error ) ;
114
125
throw error ;
115
126
}
116
127
}
128
+
129
+
117
130
118
131
async * chatStream (
119
132
input : MessageInput ,
Original file line number Diff line number Diff line change @@ -137,7 +137,8 @@ export class App {
137
137
}
138
138
} else {
139
139
// 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 ) ;
141
142
res . json ( {
142
143
model : input . model ,
143
144
choices : [
Original file line number Diff line number Diff line change @@ -73,7 +73,6 @@ export class RemoteOpenAIModelEngine implements ModelInstance {
73
73
return await this . client . chat . completions . create ( {
74
74
model : this . config . model ,
75
75
messages,
76
- temperature : 1 , // Default to 0.7 if not specified
77
76
} ) ;
78
77
} ) ;
79
78
You can’t perform that action at this time.
0 commit comments