-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
utils.py
73 lines (57 loc) · 2.49 KB
/
utils.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
import abc
import codecs
import json
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from botocore.response import StreamingBody
from llama_index.core.bridge.pydantic import BaseModel, Field
class BaseIOHandler(BaseModel, metaclass=abc.ABCMeta):
content_type: str = Field(
description="The MIME type of the input data in the request body.",
)
accept: str = Field(
description="The desired MIME type of the inference response from the model container.",
)
@classmethod
def __subclasshook__(cls, subclass: type) -> bool:
return (
hasattr(subclass, "content_type")
and hasattr(subclass, "accept")
and hasattr(subclass, "serialize_input")
and callable(subclass.serialize_input)
and hasattr(subclass, "deserialize_output")
and callable(subclass.deserialize_output)
and hasattr(subclass, "deserialize_streaming_output")
and callable(subclass.deserialize_streaming_output)
and hasattr(subclass, "remove_prefix")
and callable(subclass.remove_prefix)
or NotImplemented
)
@abc.abstractmethod
def serialize_input(self, request: str, model_kwargs: dict) -> bytes:
raise NotImplementedError
@abc.abstractmethod
def deserialize_output(self, response: "StreamingBody") -> str:
raise NotImplementedError
@abc.abstractmethod
def deserialize_streaming_output(self, response: bytes) -> str:
raise NotImplementedError
@abc.abstractmethod
def remove_prefix(self, response: str, prompt: str) -> str:
raise NotImplementedError
class IOHandler(BaseIOHandler):
content_type: str = "application/json"
accept: str = "application/json"
def serialize_input(self, request: str, model_kwargs: dict) -> bytes:
request_str = json.dumps({"inputs": request, "parameters": model_kwargs})
return request_str.encode("utf-8")
def deserialize_output(self, response: "StreamingBody") -> str:
return json.load(codecs.getreader("utf-8")(response))[0]["generated_text"]
def deserialize_streaming_output(self, response: bytes) -> str:
response_str = (
response.decode("utf-8").lstrip('[{"generated_text":"').rstrip('"}]')
)
clean_response = '{"response":"' + response_str + '"}'
return json.loads(clean_response)["response"]
def remove_prefix(self, raw_text: str, prompt: str) -> str:
return raw_text[len(prompt) :]