-
Notifications
You must be signed in to change notification settings - Fork 756
/
base.py
133 lines (100 loc) · 3.64 KB
/
base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
from __future__ import annotations
import typing as t
from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
from ...exceptions import InvalidArgument
if TYPE_CHECKING:
from types import UnionType
from typing_extensions import Self
from starlette.requests import Request
from starlette.responses import Response
from bentoml.grpc.types import ProtoField
from ..types import LazyType
from ..context import InferenceApiContext as Context
from ..service.openapi.specification import Schema
from ..service.openapi.specification import Reference
InputType = (
UnionType
| t.Type[t.Any]
| LazyType[t.Any]
| dict[str, t.Type[t.Any] | UnionType | LazyType[t.Any]]
)
OpenAPIResponse = dict[str, str | dict[str, MediaType] | dict[str, t.Any]]
IO_DESCRIPTOR_REGISTRY: dict[str, type[IODescriptor[t.Any]]] = {}
IOType = t.TypeVar("IOType")
def from_spec(spec: dict[str, str]) -> IODescriptor[t.Any]:
if "id" not in spec:
raise InvalidArgument(f"IO descriptor spec ({spec}) missing ID.")
return IO_DESCRIPTOR_REGISTRY[spec["id"]].from_spec(spec)
class IODescriptor(ABC, t.Generic[IOType]):
"""
IODescriptor describes the input/output data format of an InferenceAPI defined
in a :code:`bentoml.Service`. This is an abstract base class for extending new HTTP
endpoint IO descriptor types in BentoServer.
"""
HTTP_METHODS = ["POST"]
_mime_type: str
_rpc_content_type: str = "application/grpc"
_proto_fields: tuple[ProtoField]
_sample: IOType | None = None
descriptor_id: str | None
def __init_subclass__(cls, *, descriptor_id: str | None = None):
if descriptor_id is not None:
if descriptor_id in IO_DESCRIPTOR_REGISTRY:
raise ValueError(
f"Descriptor ID {descriptor_id} already registered to {IO_DESCRIPTOR_REGISTRY[descriptor_id]}."
)
IO_DESCRIPTOR_REGISTRY[descriptor_id] = cls
cls.descriptor_id = descriptor_id
@property
def sample(self) -> IOType | None:
return self._sample
@sample.setter
def sample(self, value: IOType) -> None:
self._sample = value
@abstractmethod
def to_spec(self) -> dict[str, t.Any]:
raise NotImplementedError
@classmethod
@abstractmethod
def from_spec(cls, spec: dict[str, t.Any]) -> Self:
raise NotImplementedError
def __repr__(self) -> str:
return self.__class__.__qualname__
@abstractmethod
def input_type(self) -> InputType:
raise NotImplementedError
@abstractmethod
def openapi_schema(self) -> Schema | Reference:
raise NotImplementedError
def openapi_example(self) -> t.Any:
if self.sample is not None:
return self.sample
@abstractmethod
def openapi_components(self) -> dict[str, t.Any] | None:
raise NotImplementedError
@abstractmethod
def openapi_request_body(self) -> dict[str, t.Any]:
raise NotImplementedError
@abstractmethod
def openapi_responses(self) -> dict[str, t.Any]:
raise NotImplementedError
@abstractmethod
async def from_http_request(self, request: Request) -> IOType:
...
@abstractmethod
async def to_http_response(
self, obj: IOType, ctx: Context | None = None
) -> Response:
...
@abstractmethod
async def from_proto(self, field: t.Any) -> IOType:
...
@abstractmethod
async def to_proto(self, obj: IOType) -> t.Any:
...
@classmethod
@abstractmethod
def from_sample(cls, sample: IOType, **kwargs: t.Any) -> Self:
...