Skip to content

Commit

Permalink
Add: more error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
PawanOsman committed Apr 19, 2024
1 parent b77b7ff commit da84287
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,20 @@ 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
if (message.match(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{6}$/)) continue;

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 ?? "";

Expand Down Expand Up @@ -254,7 +261,7 @@ async function handleChatCompletion(req: Request, res: Response) {
choices: [
{
delta: {
content: "",
content: error ?? "",
},
index: 0,
finish_reason: finish_reason,
Expand All @@ -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",
},
},
Expand Down

0 comments on commit da84287

Please sign in to comment.