zensin
CDCloudflare Developers
•Created by zensin on 5/20/2024 in #workers-help
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;
4 replies