Reputation: 1
I am using langchain.js streamEvents
for streaming events to frontend. The intermediate steps output some sensitive data that I want to strip away before forwarding the stream so I am using the following transformer.
Reference: https://github.com/langchain-ai/langserve/discussions/531
Once in a while the code throws an error TypeError: Response body object should not be disturbed or locked
that I am unable to decipher.
const transformStream = new TransformStream({
transform(chunk, controller) {
try {
const text = new TextDecoder().decode(chunk);
const lines = text.split('\n');
for (let i = 0; i < lines.length; i++) {
if (lines[i].startsWith('data:')) {
const data = JSON.parse(lines[i].slice(5));
delete data.data.input;
if (typeof data.data.output === 'object') {
delete data.data.output.context;
delete data.data.output.question;
}
lines[i] = `data: ${JSON.stringify(data)}`;
}
}
controller.enqueue(new TextEncoder().encode(lines.join('\n')));
} catch(_) {
controller.enqueue(chunk);
}
}
});
I have attempted decoding and rencoding the stream after data transformation
Upvotes: 0
Views: 44