ai-chatbot icon indicating copy to clipboard operation
ai-chatbot copied to clipboard

While using LangChain Stream, not able to determine when Stream is completed

Open waseemhnyc opened this issue 1 year ago • 2 comments

Hey everyone. I am attempting to use this template and replace the direct OpenAI calls with LangChain.

I updated the code in /app/api/chat/route.ts. I used the LangChain example here as a reference: https://github.com/vercel-labs/ai/tree/main/examples/next-langchain

Notice in the screenshot below that once the stream has completed, the button stop generating is still showing. The reason why its showing is cause: isLoading in chat-panel.tsx is still set to true.

Screenshot 2023-06-22 at 12 42 04 PM

Trying to understand why is it that with the OpenAI example isLoading is flipped to false after streaming is complete. I tried to analyze the API call with OpenAI /app/api/chat/route.ts to see if its responding with something to notify that streaming is over but dont see anything there. I see that the stop generating button is calling the stop method from useChat:

{isLoading ? (
        <Button
          variant="outline"
          onClick={() => stop()}
          className="bg-background"
        >
          <IconStop className="mr-2" />
          Stop generating
        </Button>
      ) : (
import { useChat, type Message } from 'ai/react'
  const { messages, append, reload, stop, isLoading, input, setInput } =
    useChat({
      initialMessages,
      id,
      body: {
        id,
        previewToken
      }
    })

I think I am on the right track but wonder if there is something missing in the docs about this or if anyone has an example that would be helpful in understanding this.

waseemhnyc avatar Jun 22 '23 16:06 waseemhnyc

It's a problem with SDK at the moment https://github.com/vercel-labs/ai/issues/205

The handlers that are being returned don't necessarily call writer.close()

aranlucas avatar Jun 23 '23 04:06 aranlucas

Ahhh @aranlucas. It was helpful to go through https://github.com/vercel-labs/ai/issues/205 to get better context. Thanks!

waseemhnyc avatar Jun 23 '23 14:06 waseemhnyc

@waseemhnyc I have exactly same issue. Have you fixed this?

Seokhyeon315 avatar Jul 13 '23 11:07 Seokhyeon315

Hey @Seokhyeon315. @aranlucas goes through a potential solution here. I also looked at their example resume-chat to see it implemented. Hope it helps!

waseemhnyc avatar Jul 13 '23 21:07 waseemhnyc

^ Looks like there was a resolution 😄

leerob avatar Nov 27 '23 02:11 leerob