Skip to content

Commit

Permalink
修复支持流请求发生错误时,通过流将异常信息返回
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinlic committed Apr 13, 2024
1 parent 93a6ada commit 3c5ff23
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/api/routes/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import _ from 'lodash';
import Request from '@/lib/request/Request.ts';
import Response from '@/lib/response/Response.ts';
import chat from '@/api/controllers/chat.ts';
import logger from '@/lib/logger.ts';
import util from '@/lib/util.ts';

export default {

Expand All @@ -30,7 +30,19 @@ export default {
});
}
catch(err) {
return new Response(Buffer.from(`data: ${err.message}\n\ndata: [DONE]\n\n`), {
return new Response(Buffer.from(`data: ${JSON.stringify({
id: "",
model,
object: "chat.completion.chunk",
choices: [
{
index: 0,
delta: { role: "assistant", content: err.message },
finish_reason: "stop",
},
],
created: util.unixTimestamp(),
})}\n\ndata: [DONE]\n\n`), {
type: "text/event-stream"
});
}
Expand Down

0 comments on commit 3c5ff23

Please sign in to comment.