Sagemaker Utils
AwsSagemakerEngine
AwsEngine uses boto3 for sagemaker connections
Source code in app/utils/aws_sagemaker_utils.py
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 |
|
LineIterator
A helper class for parsing the byte stream input.
The output of the model will be in the following format:
{"outputs": [" a"]}
{"outputs": [" challenging"]}
{"outputs": [" problem"]}
...
While usually each PayloadPart event from the event stream will contain a byte array with a full json, this is not guaranteed and some of the json objects may be split across PayloadPart events. For example:
{'PayloadPart': {'Bytes': {"outputs": '}}
{'PayloadPart': {'Bytes': [" problem"]}'}}
This class accounts for this by concatenating bytes written via the 'write' function and then exposing a method which will return lines (ending with a '\n' character) within the buffer via the 'scan_lines' function. It maintains the position of the last read position to ensure that previous bytes are not exposed again.
Source code in app/utils/aws_sagemaker_utils.py
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 |
|
get_realtime_response_stream(sagemaker_runtime, endpoint_name, payload)
Fetch Streaming Text Generation response from AWS Sagemaker Enpoint.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sagemaker_runtime |
sagemaker_runtime
|
|
required |
endpoint_name |
str
|
Endpoint Name. |
required |
payload |
json
|
Request |
required |
Returns:
Name | Type | Description |
---|---|---|
_type_ |
description |
Source code in app/utils/aws_sagemaker_utils.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
|
parse_response_stream(response_stream, verbose=True)
Prints Streaming Text
Parameters:
Name | Type | Description | Default |
---|---|---|---|
response_stream |
response stream |
required | |
verbose |
bool
|
description. Defaults to True. |
True
|
Returns:
Name | Type | Description |
---|---|---|
resonse |
str
|
Generated Text. |
Source code in app/utils/aws_sagemaker_utils.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
|