Spaces:
Build error
Build error
File size: 470 Bytes
d97a6fa |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
from queue import Queue
from typing import Any
from langchain.callbacks.base import BaseCallbackHandler
class QueueCallback(BaseCallbackHandler):
"""Callback handler for streaming LLM responses to a queue."""
def __init__(self, queue: Queue):
self.queue = queue
def on_llm_new_token(self, token: str, **kwargs: Any) -> None:
self.queue.put(token)
def on_llm_end(self, *args, **kwargs: Any) -> None:
return self.queue.empty() |