Next.js Discord

Discord Forum

Streaming chunk

Unanswered
West African Lion posted this in #help-forum
Open in Discord
West African LionOP
hello guys, its my first experience with next and typescript, im trying to create an api that stream the token as soon they are generated (using langchain.js)
Im not figuring out how i can do that. I tried with res.write() method but the return is write is not a function.
here is the script. This is inside a route.ts inside src/app/api
import {chainQA} from "@/utils/conversationalQA"
import {Request, Response} from 'express';
import type { NextApiRequest, NextApiResponse } from 'next'

export async function POST(req: Request , res : Response){   
    await (await chainQA("openAi")).call(
        {
            question: "ciao.",
            chat_history: [],
        },
        [
            {
                handleLLMNewToken: async (token) => {
                    console.log(token) // here the token are generated
                },

            },
        ]
    );

}

0 Replies