mirror of
https://github.com/run-llama/multi-agents-workflow.git
synced 2026-07-01 21:24:00 -04:00
44 lines
1.3 KiB
Python
44 lines
1.3 KiB
Python
import asyncio
|
|
import logging
|
|
|
|
from fastapi import APIRouter, HTTPException, Request, status
|
|
from llama_index.core.workflow import Workflow
|
|
|
|
from app.examples.factory import create_agent
|
|
from app.api.routers.models import (
|
|
ChatData,
|
|
)
|
|
from app.api.routers.vercel_response import VercelStreamResponse
|
|
|
|
chat_router = r = APIRouter()
|
|
|
|
logger = logging.getLogger("uvicorn")
|
|
|
|
|
|
# streaming endpoint - delete if not needed
|
|
@r.post("")
|
|
async def chat(
|
|
request: Request,
|
|
data: ChatData,
|
|
):
|
|
try:
|
|
last_message_content = data.get_last_message_content()
|
|
# TODO: use message history
|
|
# messages = data.get_history_messages()
|
|
# TODO: generate filters based on doc_ids
|
|
# for now just use all documents
|
|
# doc_ids = data.get_chat_document_ids()
|
|
# TODO: use params
|
|
# params = data.data or {}
|
|
|
|
agent: Workflow = create_agent()
|
|
task = asyncio.create_task(agent.run(input=last_message_content))
|
|
|
|
return VercelStreamResponse(request, task, agent.stream_events, data)
|
|
except Exception as e:
|
|
logger.exception("Error in agent", exc_info=True)
|
|
raise HTTPException(
|
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
|
detail=f"Error in agent: {e}",
|
|
) from e
|