-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ce6aeb5
commit 814c49c
Showing
2 changed files
with
115 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
templates/types/streaming/fastapi/app/api/routers/messaging.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import asyncio | ||
from typing import AsyncGenerator, Dict, Any, List, Optional | ||
|
||
from llama_index.core.callbacks.base import BaseCallbackHandler | ||
from llama_index.core.callbacks.schema import CBEventType, EventPayload | ||
from pydantic import BaseModel | ||
|
||
|
||
class CallbackEvent(BaseModel): | ||
event_type: CBEventType | ||
payload: Optional[Dict[str, Any]] = None | ||
event_id: str = "" | ||
|
||
def get_title(self): | ||
# TODO: we get two CBEventType.RETRIEVE events | ||
# For the on_event_start we should render: | ||
# "Retrieving context for query <query_str>" | ||
# For the on_event_end we should render: | ||
# "Retrieved <nodes> sources to use as context for the query" | ||
return self.event_id | ||
|
||
|
||
class EventCallbackHandler(BaseCallbackHandler): | ||
_aqueue: asyncio.Queue | ||
is_done: False | ||
|
||
def __init__( | ||
self, | ||
): | ||
"""Initialize the base callback handler.""" | ||
ignored_events = [ | ||
CBEventType.CHUNKING, | ||
CBEventType.NODE_PARSING, | ||
CBEventType.EMBEDDING, | ||
CBEventType.LLM, | ||
] | ||
super().__init__(ignored_events, ignored_events) | ||
self._aqueue = asyncio.Queue() | ||
|
||
def on_event_start( | ||
self, | ||
event_type: CBEventType, | ||
payload: Optional[Dict[str, Any]] = None, | ||
event_id: str = "", | ||
**kwargs: Any, | ||
) -> str: | ||
self._aqueue.put_nowait( | ||
CallbackEvent(event_id=event_id, event_type=event_type, payload=payload) | ||
) | ||
|
||
def on_event_end( | ||
self, | ||
event_type: CBEventType, | ||
payload: Optional[Dict[str, Any]] = None, | ||
event_id: str = "", | ||
**kwargs: Any, | ||
) -> None: | ||
self._aqueue.put_nowait( | ||
CallbackEvent(event_id=event_id, event_type=event_type, payload=payload) | ||
) | ||
|
||
def start_trace(self, trace_id: Optional[str] = None) -> None: | ||
"""No-op.""" | ||
|
||
def end_trace( | ||
self, | ||
trace_id: Optional[str] = None, | ||
trace_map: Optional[Dict[str, List[str]]] = None, | ||
) -> None: | ||
"""No-op.""" | ||
|
||
async def async_event_gen(self) -> AsyncGenerator[CallbackEvent, None]: | ||
while True: | ||
if not self._aqueue.empty() or not self.is_done: | ||
try: | ||
event = await asyncio.wait_for(self._aqueue.get(), timeout=0.1) | ||
except asyncio.TimeoutError: | ||
if self.is_done: | ||
break | ||
continue | ||
yield event | ||
else: | ||
break |