I'm trying to build a ChatGPT website clone and now I need to make the stream completion effect that shows the result word-per-word. My server is a TypeScript Node.js app that uses the Express.js framework.
Here's the route:
import express, { Request, Response } from 'express';
import cors from 'cors';
import { Configuration, OpenAIAPI } from 'openai';
// ...
app.post('/api/admin/testStream', async (req: Request, res: Response) => {
const { password } = req.body;
try {
if (password !== process.env.ADMIN_PASSWORD) {
res.send({ message: 'Incorrect password' });
return;
}
const completion = await openai.createCompletion({
model: 'text-davinci-003',
prompt: 'Say this is a test',
stream: true,
}, { responseType: 'stream' });
completion.data.on('data', (chunk: any) => {
console.log(chunk.toString());
});
res.send({ message: 'Stream started' });
} catch (err) {
console.log(err);
res.send(err);
}
});
// ...
Right now, it gives me an error saying
Property 'on' does not exist on type 'CreateCompletionResponse'.ts(2339)
even if I set the { responseType: 'stream' }
.
How can I solve this problem and send the response chunk-per-chunk to the frontend? (I'm using Socket.IO.)
completion.data.on('data', ...);
, you might need to docompletion.on('data', ...);
. – Myrilla