@@ -45,9 +45,8 @@ const fileParsing = async (options: any, req: any) => {
45
45
return options . messages
46
46
}
47
47
48
- openaiRouter . post ( '/stream/:version? ' , upload . single ( 'file' ) , async ( r , res ) => {
48
+ openaiRouter . post ( '/stream/v2 ' , upload . single ( 'file' ) , async ( r , res ) => {
49
49
const req = r as RequestWithUser
50
- const { version } = r . params
51
50
const { options, courseId } = JSON . parse ( req . body . data )
52
51
const { model, userConsent } = options
53
52
const { user } = req
@@ -105,31 +104,134 @@ openaiRouter.post('/stream/:version?', upload.single('file'), async (r, res) =>
105
104
model,
106
105
} )
107
106
108
- let events
109
- if ( version === 'v2' ) {
110
- const latestMessage = options . messages [ options . messages . length - 1 ] // Adhoc to input only the latest message
111
- events = await responsesClient . createResponse ( { input : [ latestMessage ] , prevResponseId : options . prevResponseId } )
112
- } else {
113
- events = await getCompletionEvents ( options )
114
- }
107
+ const latestMessage = options . messages [ options . messages . length - 1 ] // Adhoc to input only the latest message
108
+ const events = await responsesClient . createResponse ( { input : [ latestMessage ] , prevResponseId : options . prevResponseId } )
109
+
115
110
if ( isError ( events ) ) {
116
111
res . status ( 424 )
117
112
return
118
113
}
119
114
120
115
res . setHeader ( 'content-type' , 'text/event-stream' )
121
116
122
- let completion
123
- if ( version === 'v2' ) {
124
- completion = await responsesClient . handleResponse ( {
125
- events,
126
- encoding,
127
- res,
117
+ const completion = await responsesClient . handleResponse ( {
118
+ events,
119
+ encoding,
120
+ res,
121
+ } )
122
+
123
+ tokenCount += completion . tokenCount
124
+
125
+ let userToCharge = user
126
+ if ( inProduction && req . hijackedBy ) {
127
+ userToCharge = req . hijackedBy
128
+ }
129
+
130
+ if ( courseId ) {
131
+ await incrementCourseUsage ( userToCharge , courseId , tokenCount )
132
+ } else if ( model !== FREE_MODEL ) {
133
+ await incrementUsage ( userToCharge , tokenCount )
134
+ }
135
+
136
+ logger . info ( `Stream ended. Total tokens: ${ tokenCount } ` , {
137
+ tokenCount,
138
+ model,
139
+ user : user . username ,
140
+ courseId,
141
+ } )
142
+
143
+ const course =
144
+ courseId &&
145
+ ( await ChatInstance . findOne ( {
146
+ where : { courseId } ,
147
+ } ) )
148
+
149
+ const consentToSave = courseId && course . saveDiscussions && options . saveConsent
150
+
151
+ console . log ( 'consentToSave' , options . saveConsent , user . username )
152
+
153
+ if ( consentToSave ) {
154
+ const discussion = {
155
+ userId : user . id ,
156
+ courseId,
157
+ response : completion . response ,
158
+ metadata : options ,
159
+ }
160
+ await Discussion . create ( discussion )
161
+ }
162
+
163
+ encoding . free ( )
164
+
165
+ res . end ( )
166
+ return
167
+ } )
168
+
169
+ openaiRouter . post ( '/stream' , upload . single ( 'file' ) , async ( r , res ) => {
170
+ const req = r as RequestWithUser
171
+ const { options, courseId } = JSON . parse ( req . body . data )
172
+ const { model, userConsent } = options
173
+ const { user } = req
174
+
175
+ options . options = { temperature : options . modelTemperature }
176
+
177
+ if ( ! user . id ) {
178
+ res . status ( 401 ) . send ( 'Unauthorized' )
179
+ return
180
+ }
181
+
182
+ const usageAllowed = courseId ? await checkCourseUsage ( user , courseId ) : model === FREE_MODEL || ( await checkUsage ( user , model ) )
183
+
184
+ if ( ! usageAllowed ) {
185
+ res . status ( 403 ) . send ( 'Usage limit reached' )
186
+ return
187
+ }
188
+
189
+ let optionsMessagesWithFile = null
190
+
191
+ try {
192
+ if ( req . file ) {
193
+ optionsMessagesWithFile = await fileParsing ( options , req )
194
+ }
195
+ } catch ( error ) {
196
+ logger . error ( 'Error parsing file' , { error } )
197
+ res . status ( 400 ) . send ( 'Error parsing file' )
198
+ return
199
+ }
200
+
201
+ options . messages = getMessageContext ( optionsMessagesWithFile || options . messages )
202
+ options . stream = true
203
+
204
+ const encoding = getEncoding ( model )
205
+ let tokenCount = calculateUsage ( options , encoding )
206
+ const tokenUsagePercentage = Math . round ( ( tokenCount / DEFAULT_TOKEN_LIMIT ) * 100 )
207
+
208
+ if ( model !== FREE_MODEL && tokenCount > 0.1 * DEFAULT_TOKEN_LIMIT && ! userConsent ) {
209
+ res . status ( 201 ) . json ( {
210
+ tokenConsumtionWarning : true ,
211
+ message : `You are about to use ${ tokenUsagePercentage } % of your monthly CurreChat usage` ,
128
212
} )
129
- } else {
130
- completion = await streamCompletion ( events , options , encoding , res )
213
+ return
131
214
}
132
215
216
+ const contextLimit = getModelContextLimit ( model )
217
+
218
+ if ( tokenCount > contextLimit ) {
219
+ logger . info ( 'Maximum context reached' )
220
+ res . status ( 403 ) . send ( 'Model maximum context reached' )
221
+ return
222
+ }
223
+
224
+ const events = await getCompletionEvents ( options )
225
+
226
+ if ( isError ( events ) ) {
227
+ res . status ( 424 )
228
+ return
229
+ }
230
+
231
+ res . setHeader ( 'content-type' , 'text/event-stream' )
232
+
233
+ const completion = await streamCompletion ( events , options , encoding , res )
234
+
133
235
tokenCount += completion . tokenCount
134
236
135
237
let userToCharge = user
0 commit comments