LangChain: Streaming in JavaScript with React.js & Next.js - Robin Wieruch

LangChain: Streaming in JavaScript with React.js & Next.js - Robin Wieruch

Let’s learn about using LangChain in JavaScript for streaming responses from the backend to the frontend while building a chatbot application that interacts with the OpenAI API to generate responses to user prompts. In this tutorial we will be using Next.js (with React.js) for the frontend (UI) and backend (API). The finished project can be found on GitHub .

We will continue with the previous OpenAI chatbot application and implement streaming responses from the backend to the frontend. We are already displaying the chat messages between user and bot in the frontend, now we add another state for the incoming message coming from the backend in real-time:

Next we are displaying the incoming message in the frontend below the previous chat messages:

For the backend we have to adjust the API route to stream the response to the frontend. We are using the Response object to stream the response as an event stream and use stream() instead of invoke() to get the streamed response from LangChain:

Last we have to adjust the handler in the frontend to handle the streamed response from the backend. We are using the TextDecoderStream to decode the streamed response and update the incoming message in the frontend. Once the response is fully streamed, we add the incoming message to the chat messages and reset the incoming message which has come in real-time:

Congratulations! You’ve now implemented a real-time chatbot application using LangChain and OpenAI in JavaScript with the help of Next.js and React. This tutorial demonstrated how responses can be streamed from the backend to the frontend, providing a more interactive user experience.

Recommended articles