optimize error response in streaming

This commit is contained in:
Aiden Dai
2025-03-26 11:32:39 +08:00
parent 4f1a75b49f
commit c98e123c8f
4 changed files with 64 additions and 40 deletions
+10 -4
View File
@@ -11,6 +11,7 @@ from api.schema import (
# Embeddings
EmbeddingsRequest,
EmbeddingsResponse,
Error,
)
@@ -43,14 +44,19 @@ class BaseChatModel(ABC):
return "chatcmpl-" + str(uuid.uuid4())[:8]
@staticmethod
def stream_response_to_bytes(response: ChatStreamResponse | None = None) -> bytes:
if response:
def stream_response_to_bytes(response: ChatStreamResponse | Error | None = None) -> bytes:
if isinstance(response, Error):
data = response.model_dump_json()
elif isinstance(response, ChatStreamResponse):
# to populate other fields when using exclude_unset=True
response.system_fingerprint = "fp"
response.object = "chat.completion.chunk"
response.created = int(time.time())
return "data: {}\n\n".format(response.model_dump_json(exclude_unset=True)).encode("utf-8")
return "data: [DONE]\n\n".encode("utf-8")
data = response.model_dump_json(exclude_unset=True)
else:
data = "[DONE]"
return f"data: {data}\n\n".encode("utf-8")
class BaseEmbeddingsModel(ABC):