langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Add BaseCallbackHandler, AsyncCallbackHandler and AsyncCallbackManager to exports of langchain.callbacks

Open sostholmwork opened this issue 1 year ago • 1 comments

Hello!

I would like to propose adding BaseCallbackHandler, AsyncCallbackHandler, and AsyncCallbackManager to the exports of langchain.callbacks. Doing so would enable developers to create custom CallbackHandlers and run their own code for each of the steps handled by the BaseCallbackHandler, as well as their async counterparts.

In my opinion, this is an essential feature for scaling langchain-based applications. It would allow for things such as streaming step-by-step information to a frontend client for live debugging.

I believe that incorporating these features into the langchain.callbacks exports would make langchain even more powerful and user-friendly for developers.

Thank you for considering my proposal.

sostholmwork avatar Mar 31 '23 12:03 sostholmwork

"It would allow for things such as streaming step-by-step information to a frontend client for live debugging."

Does this mean streaming to frontend is not supported yet? Like using websockets? Because I'm working on that right now and I'm struggling!

My Context: I'm using AsyncCallbackHandler with RetrievalQA and yes, I'm having issue of :

RuntimeWarning: coroutine 'AsyncCallbackHandler.on_llm_end' was never awaited
  getattr(handler, event_name)(*args, **kwargs)

khaledadrani avatar Jun 13 '23 17:06 khaledadrani

Same here, struggling to get it working with CTransformers and Websockets.

When I print to console works nicely, but when I try to send the token through the WebSocket never ends.

Edit: for future readers, the problems on my side were two things, the llm() call was blocking the main thread and the web socket.send_json wasn't working while the llm was generating the text.

To fix that I've used asyncio.create_task:

task = asyncio.create_task(
                llm._acall(
                    prompt=query,
                    run_manager=stream_handler,
                    **asdict(generation_config)
                )
            )

and from other side, I've pushed a fix to Langchain to accept _acall() from the Ctransformers wrapper.

semoal avatar Jun 27 '23 17:06 semoal

Hi, these can be imported from langchain.callbacks.base

nfcampos avatar Sep 25 '23 10:09 nfcampos