langchainjs icon indicating copy to clipboard operation
langchainjs copied to clipboard

Stream ConversationalRetrievalQAChain is not working

Open mohamedcherifmo opened this issue 1 year ago • 10 comments

I'm using a pinecone vector with OpenAIChat model in a ConversationalRetrievalQAChain, it works as intended when im not streaming the output however if I change the OpenAIChat to streaming true as per the below code snippet it fails with the following error "TypeError: stream.getReader is not a function"

 const model = new OpenAIChat({
        streaming: true,
  callbackManager: CallbackManager.fromHandlers({
    async handleLLMNewToken(token: string) {
      console.log(token);
    },
  }),
    });

This is in a nextjs API route

mohamedcherifmo avatar Mar 31 '23 04:03 mohamedcherifmo

I did find out that the reason for the issue is the axios-fetch-adapter is relying on a JS ReadableStream however NextJS readable stream is handled differently... The question now becomes is there a way when defining my call to override the fetch-adapter and create my own to cater for this and make it actually work with NextJS?

mohamedcherifmo avatar Mar 31 '23 06:03 mohamedcherifmo

@mohamedcherifmo Try using fetch-event-source

Example usage:

fetchEventSource('/api/chat', {
    method: 'POST',
    headers: {
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        question,
        history,
    }),
    onmessage: (event) => {
        console.log(event)
    }
});

Jasonkoolman avatar Mar 31 '23 20:03 Jasonkoolman

@Jasonkoolman where do u mean to try that?, the error is coming from the api router itself not the client side code

mohamedcherifmo avatar Mar 31 '23 23:03 mohamedcherifmo

Same problem. But it's works file on 0.0.43

eternnoir avatar Apr 01 '23 18:04 eternnoir

It works fine on node v18 but getting the same error for node v16

KMKoushik avatar Apr 04 '23 12:04 KMKoushik

I got 0.0.47, node v18 and fetch-event-source and it's not working for me

error:

TypeError: stream.getReader is not a function
  at getBytes (file:///app/node_modules/@fortaine/fetch-event-source/lib/esm/parse.js:2:27)
  at getResponse (file:///app/node_modules/langchain/dist/util/axios-fetch-adapter.js:206:19)
  at runMicrotasks (<anonymous>)
  at processTicksAndRejections (node:internal/process/task_queues:96:5)
  at async fetchAdapter (file:///app/node_modules/langchain/dist/util/axios-fetch-adapter.js:167:18) undefined

AlessandroAnnini avatar Apr 04 '23 21:04 AlessandroAnnini

Hi, can you confirm if this isin a serverless or edge function?

nfcampos avatar Apr 05 '23 10:04 nfcampos

I got it working using node 16 (not changing the dockerfile), using v0.0.47 and spinning a server on fly.io

it is wired because locally it was working with node 18 too

AlessandroAnnini avatar Apr 05 '23 12:04 AlessandroAnnini

I also have a problem with it in Next.js 13 + Node 19. :( Which detail can I provide? :)

BleedingDev avatar Apr 06 '23 00:04 BleedingDev

same error with Next.js edge functions, [email protected] & [email protected]

felipetodev avatar Apr 10 '23 03:04 felipetodev

the problem still exists

mrslimslim avatar May 04 '23 06:05 mrslimslim

I think i find why it didnt work, it should be the misuse the origin web env "fetch" and "node-fetch". Check the detail here https://stackoverflow.com/questions/57664058/response-body-getreader-is-not-a-function.

Then I change the dep file event-source-parse getBytes function like this , problem solved.

export async function getBytes(stream, onChunk) {
    // const reader = stream.getReader();
    // let result;
    // // eslint-disable-next-line no-cond-assign
    // while (!(result = await reader.read()).done) {
    //     onChunk(result.value);
    // }
    stream.on('readable', () => {
        let chunk;
        while (null !== (chunk = stream.read())) {
            onChunk(chunk);
        }
    }
    );
}

mrslimslim avatar May 04 '23 08:05 mrslimslim

@dqbd do you want to have a look at this one? Would be pretty strange if nextjs/vercel didn't support the ReadableStream web standard? Or is that people are running this in Node 16 without realising and Node 16 doesn't support ReadableStream

nfcampos avatar May 11 '23 16:05 nfcampos

I think i find why it didnt work, it should be the misuse the origin web env "fetch" and "node-fetch". Check the detail here https://stackoverflow.com/questions/57664058/response-body-getreader-is-not-a-function.

Then I change the dep file event-source-parse getBytes function like this , problem solved.

export async function getBytes(stream, onChunk) {
    // const reader = stream.getReader();
    // let result;
    // // eslint-disable-next-line no-cond-assign
    // while (!(result = await reader.read()).done) {
    //     onChunk(result.value);
    // }
    stream.on('readable', () => {
        let chunk;
        while (null !== (chunk = stream.read())) {
            onChunk(chunk);
        }
    }
    );
}

This solves the issue. I had the same problem while trying to stream the response from the /experimental/autogpt function with node v17.9.1 in my local environment. getReader() function is not supported by Node.js node-fetch package.

nsoticek avatar Jun 26 '23 16:06 nsoticek

This solution is fine! Can next version adapter browser fetch (node v18+) and node-fetch (node v16) ?

zhaoo avatar Jul 04 '23 12:07 zhaoo