Skip to content

Commit c996508

Browse files
committed
support non-streaming api
1 parent 21b7df1 commit c996508

File tree

1 file changed

+36
-3
lines changed
  • templates/types/streaming/fastapi/app/api/routers

1 file changed

+36
-3
lines changed

templates/types/streaming/fastapi/app/api/routers/chat.py

+36-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from app.api.callbacks.llamacloud import LlamaCloudFileDownload
66
from app.api.callbacks.next_question import SuggestNextQuestions
7-
from app.api.callbacks.stream_handler import StreamHandler
87
from app.api.callbacks.source_nodes import AddNodeUrl
8+
from app.api.callbacks.stream_handler import StreamHandler
99
from app.api.routers.models import (
1010
ChatData,
1111
)
@@ -50,8 +50,41 @@ async def chat(
5050
],
5151
).vercel_stream()
5252
except Exception as e:
53-
logger.exception("Error in chat engine", exc_info=True)
53+
logger.exception("Error in chat", exc_info=True)
54+
raise HTTPException(
55+
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
56+
detail=f"Error in chat: {e}",
57+
) from e
58+
59+
60+
# non-streaming endpoint - delete if not needed
61+
@r.post("/request")
62+
async def chat_request(
63+
request: Request,
64+
data: ChatData,
65+
):
66+
try:
67+
last_message_content = data.get_last_message_content()
68+
messages = data.get_history_messages(include_agent_messages=True)
69+
70+
doc_ids = data.get_chat_document_ids()
71+
filters = generate_filters(doc_ids)
72+
params = data.data or {}
73+
74+
workflow = create_workflow(
75+
params=params,
76+
filters=filters,
77+
)
78+
79+
handler = workflow.run(
80+
user_msg=last_message_content,
81+
chat_history=messages,
82+
stream=False,
83+
)
84+
return await handler
85+
except Exception as e:
86+
logger.exception("Error in chat request", exc_info=True)
5487
raise HTTPException(
5588
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
56-
detail=f"Error in chat engine: {e}",
89+
detail=f"Error in chat request: {e}",
5790
) from e

0 commit comments

Comments
 (0)