/
base.py
86 lines (64 loc) · 2.21 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
from __future__ import annotations
import typing as t
from abc import ABC
from abc import abstractmethod
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from types import UnionType
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 Response as OpenAPIResponse
from ..service.openapi.specification import Reference
from ..service.openapi.specification import RequestBody
InputType = (
UnionType
| t.Type[t.Any]
| LazyType[t.Any]
| dict[str, t.Type[t.Any] | UnionType | LazyType[t.Any]]
)
IOType = t.TypeVar("IOType")
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]
def __repr__(self) -> str:
return self.__class__.__qualname__
@abstractmethod
def input_type(self) -> InputType:
...
@abstractmethod
def openapi_schema(self) -> Schema | Reference:
raise NotImplementedError
@abstractmethod
def openapi_components(self) -> dict[str, t.Any] | None:
raise NotImplementedError
@abstractmethod
def openapi_request_body(self) -> RequestBody:
raise NotImplementedError
@abstractmethod
def openapi_responses(self) -> OpenAPIResponse:
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:
...