Skip to content

Commit

Permalink
Refactor StreamHandler to simplify Vercel streaming response creation
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj committed Feb 5, 2025
1 parent 3f72567 commit 9d9cd5c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from typing import List, Optional

from llama_index.core.workflow.handler import WorkflowHandler
from app.api.routers.vercel_response import VercelStreamResponse

from app.api.callbacks.base import EventCallback

Expand All @@ -22,6 +23,10 @@ def __init__(
self.callbacks = callbacks or []
self.accumulated_text = ""

def vercel_stream(self):
"""Create a streaming response with Vercel format."""
return VercelStreamResponse(stream_handler=self)

async def cancel_run(self):
"""Cancel the workflow handler."""
await self.workflow_handler.cancel_run()
Expand Down Expand Up @@ -49,3 +54,12 @@ async def stream_events(self):
async def accumulate_text(self, text: str):
"""Accumulate text from the workflow handler."""
self.accumulated_text += text

@classmethod
def from_default(
cls,
handler: WorkflowHandler,
callbacks: Optional[List[EventCallback]] = None,
) -> "StreamHandler":
"""Create a new instance with the given workflow handler and callbacks."""
return cls(workflow_handler=handler, callbacks=callbacks)
16 changes: 7 additions & 9 deletions templates/components/multiagent/python/app/api/routers/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,13 @@ async def chat(
chat_history=messages,
stream=True,
)
return VercelStreamResponse(
stream_handler=StreamHandler(
workflow_handler=handler,
callbacks=[
LlamaCloudFileDownload.from_default(background_tasks),
SuggestNextQuestions.from_default(data),
],
),
)
return StreamHandler.from_default(
handler=handler,
callbacks=[
LlamaCloudFileDownload.from_default(background_tasks),
SuggestNextQuestions.from_default(data),
],
).vercel_stream()
except Exception as e:
logger.exception("Error in chat engine", exc_info=True)
raise HTTPException(
Expand Down

0 comments on commit 9d9cd5c

Please sign in to comment.