optimize error response in streaming
This commit is contained in:
+10
-4
@@ -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):
|
||||
|
||||
Reference in New Issue
Block a user