2024-08-29 20:17:17 +08:00
|
|
|
import json
|
|
|
|
from collections.abc import Generator
|
2024-09-26 17:22:39 +08:00
|
|
|
from typing import Generic, Optional, TypeVar
|
2024-08-29 20:17:17 +08:00
|
|
|
|
|
|
|
from pydantic import BaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class BaseBackwardsInvocation:
|
|
|
|
@classmethod
|
2024-08-29 20:50:36 +08:00
|
|
|
def convert_to_event_stream(cls, response: Generator[BaseModel | dict | str, None, None] | BaseModel | dict):
|
2024-08-29 20:17:17 +08:00
|
|
|
if isinstance(response, Generator):
|
2024-09-26 17:22:39 +08:00
|
|
|
try:
|
|
|
|
for chunk in response:
|
|
|
|
if isinstance(chunk, BaseModel):
|
|
|
|
yield BaseBackwardsInvocationResponse(data=chunk).model_dump_json().encode() + b"\n\n"
|
|
|
|
|
|
|
|
elif isinstance(chunk, str):
|
|
|
|
yield f"event: {chunk}\n\n".encode()
|
|
|
|
else:
|
|
|
|
yield json.dumps(chunk).encode() + b"\n\n"
|
|
|
|
except Exception as e:
|
|
|
|
error_message = BaseBackwardsInvocationResponse(error=str(e)).model_dump_json()
|
|
|
|
yield f"{error_message}\n\n".encode()
|
2024-08-29 20:17:17 +08:00
|
|
|
else:
|
|
|
|
if isinstance(response, BaseModel):
|
2024-09-26 17:22:39 +08:00
|
|
|
yield response.model_dump_json().encode() + b"\n\n"
|
2024-08-29 20:17:17 +08:00
|
|
|
else:
|
2024-09-26 17:22:39 +08:00
|
|
|
yield json.dumps(response).encode() + b"\n\n"
|
|
|
|
|
|
|
|
|
|
|
|
T = TypeVar("T", bound=BaseModel | dict | str | bool | int)
|
|
|
|
|
|
|
|
|
|
|
|
class BaseBackwardsInvocationResponse(BaseModel, Generic[T]):
|
|
|
|
data: Optional[T] = None
|
|
|
|
error: str = ""
|