-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
separate chat api for multi-agent code
- Loading branch information
Showing
1 changed file
with
49 additions
and
0 deletions.
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
templates/components/multiagent/python/app/api/routers/chat.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,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 |