-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstream_handler.py
More file actions
24 lines (19 loc) · 795 Bytes
/
stream_handler.py
File metadata and controls
24 lines (19 loc) · 795 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
from langchain_core.callbacks.base import BaseCallbackHandler
from typing import Dict, List, Optional, Tuple, Iterator, Callable
class StreamingCallbackHandler(BaseCallbackHandler):
"""Callback handler for streaming LLM responses."""
def __init__(self, stream_callback: Callable[[str], None]):
"""
Initialize the streaming callback handler.
Args:
stream_callback: Function to call with each token chunk
"""
self.stream_callback = stream_callback
def on_llm_new_token(self, token: str, **kwargs) -> None:
"""
Process a new token from the LLM.
Args:
token: The token string
**kwargs: Additional arguments
"""
self.stream_callback(token)