File size: 997 Bytes
66c0d0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
Endpoints defination
"""

from fastapi import APIRouter, Depends, HTTPException

from auth import verify_bearer
from schema import SingleResponse, UserInput
from utils import get_llm_response, infer_chat_message

bot_router = APIRouter(tags=["bot"], dependencies=[Depends(verify_bearer)])


@bot_router.post("/chat")
async def invoke(user_input: UserInput) -> SingleResponse:
    """
    Invoke an agent with user input to retrieve a final response.

    Use thread_id to persist and continue a multi-turn conversation.
    """
    try:
        response, thread_id = await get_llm_response(user_input)
        last_message = infer_chat_message(response["messages"][-1])
        response = {
            "response_message": last_message.content,
            "responder": last_message.type,
            "thread_id": thread_id,
        }
        return SingleResponse(**response)
    except Exception as e:
        print(e)
        raise HTTPException(status_code=500, detail="Unexpected error")