mirror of
https://github.com/PawanOsman/ChatGPT.git
synced 2026-06-02 06:14:25 +02:00
Add: more error handling
This commit is contained in:
+9
-2
@@ -179,6 +179,7 @@ async function handleChatCompletion(req: Request, res: Response) {
|
||||
let requestId = GenerateCompletionId("chatcmpl-");
|
||||
let created = Math.floor(Date.now() / 1000); // Unix timestamp in seconds
|
||||
let finish_reason = null;
|
||||
let error: string;
|
||||
|
||||
for await (const message of StreamCompletion(response.data)) {
|
||||
// Skip heartbeat detection
|
||||
@@ -186,6 +187,12 @@ async function handleChatCompletion(req: Request, res: Response) {
|
||||
|
||||
const parsed = JSON.parse(message);
|
||||
|
||||
if(parsed.error){
|
||||
error = `Error message from OpenAI: ${parsed.error}`;
|
||||
finish_reason = "stop";
|
||||
break;
|
||||
}
|
||||
|
||||
let content = parsed?.message?.content?.parts[0] ?? "";
|
||||
let status = parsed?.message?.status ?? "";
|
||||
|
||||
@@ -254,7 +261,7 @@ async function handleChatCompletion(req: Request, res: Response) {
|
||||
choices: [
|
||||
{
|
||||
delta: {
|
||||
content: "",
|
||||
content: error ?? "",
|
||||
},
|
||||
index: 0,
|
||||
finish_reason: finish_reason,
|
||||
@@ -274,7 +281,7 @@ async function handleChatCompletion(req: Request, res: Response) {
|
||||
finish_reason: finish_reason,
|
||||
index: 0,
|
||||
message: {
|
||||
content: fullContent,
|
||||
content: error ?? fullContent,
|
||||
role: "assistant",
|
||||
},
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user