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