Files
tabbyAPI/utils.py
kingbri ae69b18583 API: Use FastAPI streaming instead of sse_starlette
sse_starlette kept firing a ping response if it was taking too long
to set an event. Rather than using a hacky workaround, switch to
FastAPI's inbuilt streaming response and construct SSE requests with
a utility function.

This helps the API become more robust and removes an extra requirement.

Signed-off-by: kingbri <bdashore3@proton.me>
2023-12-01 01:54:35 -05:00

33 lines
847 B
Python

import traceback
from pydantic import BaseModel
from typing import Optional
# Wrapper callback for load progress
def load_progress(module, modules):
yield module, modules
# Common error types
class TabbyGeneratorErrorMessage(BaseModel):
message: str
trace: Optional[str] = None
class TabbyGeneratorError(BaseModel):
error: TabbyGeneratorErrorMessage
def get_generator_error(exception: Exception):
error_message = TabbyGeneratorErrorMessage(
message = str(exception),
trace = traceback.format_exc()
)
generator_error = TabbyGeneratorError(
error = error_message
)
# Log and send the exception
print(f"\n{generator_error.error.trace}")
return get_sse_packet(generator_error.json(ensure_ascii = False))
def get_sse_packet(json_data: str):
return f"data: {json_data}\n\n"