dan92 commited on
Commit
4ba6a3e
·
verified ·
1 Parent(s): 863deaf

Upload 2 files

Browse files
Files changed (1) hide show
  1. src/index.js +35 -26
src/index.js CHANGED
@@ -174,6 +174,19 @@ const handleChatCompletions = async (ctx) => {
174
 
175
  const messageId = crypto.randomUUID()
176
  const reader = response.body.getReader()
 
 
 
 
 
 
 
 
 
 
 
 
 
177
  if (stream == "true" || stream == true) {
178
  ctx.res.write(`data: ${JSON.stringify({
179
  "id": `chatcmpl-${messageId}`,
@@ -193,8 +206,6 @@ const handleChatCompletions = async (ctx) => {
193
  }
194
 
195
  try {
196
- let resBody = {}
197
-
198
  while (true) {
199
  const { done, value } = await reader.read()
200
  if (done) {
@@ -204,7 +215,7 @@ const handleChatCompletions = async (ctx) => {
204
  break
205
  }
206
 
207
- if (stream) {
208
  const text = new TextDecoder().decode(value)
209
  const textContent = [...text.matchAll(/data:.*"}/g)]
210
 
@@ -237,40 +248,38 @@ const handleChatCompletions = async (ctx) => {
237
  const content = JSON.parse(item[0].replace("data: ", ''))
238
  if (!content || !content.field_value || content.field_name == 'session_state.answer_is_finished' || content.field_name == 'content' || content.field_name == 'session_state' || content.delta || content.type == 'project_field') return
239
 
240
- resBody = {
241
- id: `chatcmpl-${messageId}`,
242
- object: 'chat.completion',
243
- created: Math.floor(Date.now() / 1000),
244
- model,
245
- choices: [
246
- {
247
- index: 0,
248
- message: {
249
- role: 'assistant',
250
- content: content.field_value,
251
- },
252
- finish_reason: 'stop',
253
- },
254
- ],
255
- usage: {
256
- prompt_tokens: 0,
257
- completion_tokens: 0,
258
- total_tokens: content.field_value.length,
259
  },
260
- }
 
 
261
  })
262
  }
263
  }
264
 
265
  if (stream == "false" || stream == false) {
 
 
 
 
 
 
 
 
 
 
266
  ctx.body = resBody
267
  } else {
268
  ctx.res.end()
269
  }
270
- return
271
  } catch (error) {
272
  console.error('流式响应出错:', error)
273
- ctx.res.end()
 
274
  }
275
  } catch (error) {
276
  console.error('请求处理出错:', error)
@@ -316,7 +325,7 @@ router.get('/', async (ctx) => {
316
  }
317
  })
318
 
319
- // 注册路由��间件 (移到错误处理之后)
320
  app.use(router.routes()).use(router.allowedMethods())
321
 
322
  // 启动服务器
 
174
 
175
  const messageId = crypto.randomUUID()
176
  const reader = response.body.getReader()
177
+ let resBody = {
178
+ id: `chatcmpl-${messageId}`,
179
+ object: 'chat.completion',
180
+ created: Math.floor(Date.now() / 1000),
181
+ model,
182
+ choices: [],
183
+ usage: {
184
+ prompt_tokens: 0,
185
+ completion_tokens: 0,
186
+ total_tokens: 0,
187
+ },
188
+ }
189
+
190
  if (stream == "true" || stream == true) {
191
  ctx.res.write(`data: ${JSON.stringify({
192
  "id": `chatcmpl-${messageId}`,
 
206
  }
207
 
208
  try {
 
 
209
  while (true) {
210
  const { done, value } = await reader.read()
211
  if (done) {
 
215
  break
216
  }
217
 
218
+ if (stream == "true" || stream == true) {
219
  const text = new TextDecoder().decode(value)
220
  const textContent = [...text.matchAll(/data:.*"}/g)]
221
 
 
248
  const content = JSON.parse(item[0].replace("data: ", ''))
249
  if (!content || !content.field_value || content.field_name == 'session_state.answer_is_finished' || content.field_name == 'content' || content.field_name == 'session_state' || content.delta || content.type == 'project_field') return
250
 
251
+ resBody.choices.push({
252
+ index: 0,
253
+ message: {
254
+ role: 'assistant',
255
+ content: content.field_value,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
256
  },
257
+ finish_reason: 'stop',
258
+ })
259
+ resBody.usage.total_tokens = content.field_value.length
260
  })
261
  }
262
  }
263
 
264
  if (stream == "false" || stream == false) {
265
+ if (resBody.choices.length === 0) {
266
+ resBody.choices.push({
267
+ index: 0,
268
+ message: {
269
+ role: 'assistant',
270
+ content: '抱歉,我无法处理您的请求。',
271
+ },
272
+ finish_reason: 'stop',
273
+ })
274
+ }
275
  ctx.body = resBody
276
  } else {
277
  ctx.res.end()
278
  }
 
279
  } catch (error) {
280
  console.error('流式响应出错:', error)
281
+ ctx.status = 500
282
+ ctx.body = { error: '流式响应处理失败', details: error.toString() }
283
  }
284
  } catch (error) {
285
  console.error('请求处理出错:', error)
 
325
  }
326
  })
327
 
328
+ // 注册路由中间件
329
  app.use(router.routes()).use(router.allowedMethods())
330
 
331
  // 启动服务器