rpc text stream

Does this make sense to do like this if this code is running in a method meant to return over RPC to another worker?
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4o',
messages: messages,
stream: true,
});
let { readable, writable } = new TransformStream()
const writer = writable.getWriter();
const encoder = new TextEncoder();

(async () => {
try {
for await (const chunk of stream) {
const content = chunk['choices'][0].delta.content;
const encodedMessage = encoder.encode(content);
writer.write(encodedMessage);
}
writer.close();
} catch (e) {
console.error(e);
// writer.abort(e);
return new Response('bad bad error', {
headers: headerCode
});
}
})();
return readable;
const chatCompletion = await openai.chat.completions.create({
model: 'gpt-4o',
messages: messages,
stream: true,
});
let { readable, writable } = new TransformStream()
const writer = writable.getWriter();
const encoder = new TextEncoder();

(async () => {
try {
for await (const chunk of stream) {
const content = chunk['choices'][0].delta.content;
const encodedMessage = encoder.encode(content);
writer.write(encodedMessage);
}
writer.close();
} catch (e) {
console.error(e);
// writer.abort(e);
return new Response('bad bad error', {
headers: headerCode
});
}
})();
return readable;
1 Reply
zensin
zensinOP7mo ago
and then in the calling worker I return the readable stream like:
new Response(readableStream, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
}
});
new Response(readableStream, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Headers': 'Origin, X-Requested-With, Content-Type, Accept',
}
});
(return to client) is this good or bad way?
Want results from more Discord servers?
Add your server