Skip to content

Commit

Permalink
fix(ai): make newlines match the spec of server-sent events
Browse files Browse the repository at this point in the history
  • Loading branch information
benjidotsh committed Aug 27, 2024
1 parent 951db3f commit 9ef3e91
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions plugins/backend/rag-ai-backend/src/service/RagAiController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,24 @@ export class RagAiController {
const stream = await this.llmService.query(embeddingDocs, query);

for await (const chunk of stream) {
const text = typeof chunk === 'string' ? chunk : chunk.content;
const text =
typeof chunk === 'string' ? chunk : (chunk.content as string);
const event = `event: response\n`;
const data = `data: ${text}\n\n`;
const data = this.parseSseText(text);
res.write(event + data);
}

res.end();
};

private parseSseText = (text: string): string => {
const lines = text.split('\n');

const output = lines.reduce((result, line) => {
const data = `data: ${line}\n`;
return result + data;
}, '');

return `${output}\n`;
};
}

0 comments on commit 9ef3e91

Please sign in to comment.