Next.js Discord

Discord Forum

Send streaming response and then do something with the complete response from the backend / api

Unanswered
Lionhead posted this in #help-forum
Open in Discord
LionheadOP
Is it possible with app router to send a streaming response to the client and when the response is complete do something with the complete response on the backend?

Kind of like this but with the new api route handler (check my next message):

1 Reply

LionheadOP
const chatStream = await openai.chat.completions.create({
        messages: [
            {
            "role":"system",
            "content": "some content"
            },
            {
            "role":"user",
            "content":"some content"
            }
        ],
        model: 'gpt-4-1106-preview',
        temperature: 1.0,
        max_tokens: 3000,
        stream: true,
        response_format: { type: 'json_object' },
      });
    
      res.setHeader('Content-Type', 'application/json');
      res.setHeader('Transfer-Encoding', 'chunked');
    
      let completeResponse = ""

      for await (const message of chatStream) {
        const choice = message.choices[0];
        res.write(choice.delta.content ?? '');
        completeResponse += choice.delta.content ?? ''
    
        if (choice.finish_reason === 'stop') {
          //Do something with completeResponse

          res.end();
          break;
        }