Skip to content

Commit

Permalink
separate chat api for multi-agent code
Browse files Browse the repository at this point in the history
  • Loading branch information
leehuwuj committed Oct 1, 2024
1 parent 6138bac commit 7a9a72c
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions templates/components/multiagent/python/app/api/routers/chat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import logging

from app.api.routers.events import EventCallbackHandler
from app.api.routers.models import (
ChatData,
)
from app.api.routers.vercel_response import (
WorkflowVercelStreamResponse,
)
from app.engine import get_chat_engine
from fastapi import APIRouter, BackgroundTasks, HTTPException, Request, status

chat_router = r = APIRouter()

logger = logging.getLogger("uvicorn")


# streaming endpoint - delete if not needed
@r.post("")
async def chat(
request: Request,
data: ChatData,
background_tasks: BackgroundTasks,
):
try:
last_message_content = data.get_last_message_content()
messages = data.get_history_messages()

event_handler = EventCallbackHandler()
# The chat API supports passing private document filters and chat params
# but agent workflow does not support them yet
# ignore chat params and use all documents for now
# TODO: generate filters based on doc_ids
# TODO: use chat params
engine = get_chat_engine(chat_history=messages)

event_handler = engine.run(input=last_message_content, streaming=True)
return WorkflowVercelStreamResponse(
request=request,
chat_data=data,
event_handler=event_handler,
events=engine.stream_events(),
)
except Exception as e:
logger.exception("Error in chat engine", exc_info=True)
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
detail=f"Error in chat engine: {e}",
) from e

0 comments on commit 7a9a72c

Please sign in to comment.