2023-09-29 00:09:01 +00:00
|
|
|
from application.llm.base import BaseLLM
|
|
|
|
from application.core.settings import settings
|
|
|
|
import json
|
2023-10-06 00:52:29 +00:00
|
|
|
import io
|
|
|
|
|
|
|
|
|
|
|
|
class LineIterator:
|
|
|
|
"""
|
2024-04-15 14:05:59 +00:00
|
|
|
A helper class for parsing the byte stream input.
|
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
The output of the model will be in the following format:
|
|
|
|
```
|
|
|
|
b'{"outputs": [" a"]}\n'
|
|
|
|
b'{"outputs": [" challenging"]}\n'
|
|
|
|
b'{"outputs": [" problem"]}\n'
|
|
|
|
...
|
|
|
|
```
|
2024-04-15 14:05:59 +00:00
|
|
|
|
|
|
|
While usually each PayloadPart event from the event stream will contain a byte array
|
2023-10-06 00:52:29 +00:00
|
|
|
with a full json, this is not guaranteed and some of the json objects may be split across
|
|
|
|
PayloadPart events. For example:
|
|
|
|
```
|
|
|
|
{'PayloadPart': {'Bytes': b'{"outputs": '}}
|
|
|
|
{'PayloadPart': {'Bytes': b'[" problem"]}\n'}}
|
|
|
|
```
|
2024-04-15 14:05:59 +00:00
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
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
|
2024-04-15 14:05:59 +00:00
|
|
|
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.
|
2023-10-06 00:52:29 +00:00
|
|
|
"""
|
2024-04-15 14:05:59 +00:00
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
def __init__(self, stream):
|
|
|
|
self.byte_iterator = iter(stream)
|
|
|
|
self.buffer = io.BytesIO()
|
|
|
|
self.read_pos = 0
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return self
|
|
|
|
|
|
|
|
def __next__(self):
|
|
|
|
while True:
|
|
|
|
self.buffer.seek(self.read_pos)
|
|
|
|
line = self.buffer.readline()
|
2024-04-15 14:05:59 +00:00
|
|
|
if line and line[-1] == ord("\n"):
|
2023-10-06 00:52:29 +00:00
|
|
|
self.read_pos += len(line)
|
|
|
|
return line[:-1]
|
|
|
|
try:
|
|
|
|
chunk = next(self.byte_iterator)
|
|
|
|
except StopIteration:
|
|
|
|
if self.read_pos < self.buffer.getbuffer().nbytes:
|
|
|
|
continue
|
|
|
|
raise
|
2024-04-15 14:05:59 +00:00
|
|
|
if "PayloadPart" not in chunk:
|
|
|
|
print("Unknown event type:" + chunk)
|
2023-10-06 00:52:29 +00:00
|
|
|
continue
|
|
|
|
self.buffer.seek(0, io.SEEK_END)
|
2024-04-15 14:05:59 +00:00
|
|
|
self.buffer.write(chunk["PayloadPart"]["Bytes"])
|
|
|
|
|
2023-09-29 00:09:01 +00:00
|
|
|
|
|
|
|
class SagemakerAPILLM(BaseLLM):
|
|
|
|
|
2024-04-16 10:01:11 +00:00
|
|
|
def __init__(self, api_key=None, user_api_key=None, *args, **kwargs):
|
2023-10-06 00:52:29 +00:00
|
|
|
import boto3
|
2024-04-15 14:05:59 +00:00
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
runtime = boto3.client(
|
2024-04-15 14:05:59 +00:00
|
|
|
"runtime.sagemaker",
|
|
|
|
aws_access_key_id="xxx",
|
|
|
|
aws_secret_access_key="xxx",
|
|
|
|
region_name="us-west-2",
|
2023-10-06 00:52:29 +00:00
|
|
|
)
|
|
|
|
|
2024-04-15 14:17:24 +00:00
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.api_key = api_key
|
2024-04-16 10:01:11 +00:00
|
|
|
self.user_api_key = user_api_key
|
2024-04-15 14:05:59 +00:00
|
|
|
self.endpoint = settings.SAGEMAKER_ENDPOINT
|
2023-10-06 00:52:29 +00:00
|
|
|
self.runtime = runtime
|
|
|
|
|
2024-04-15 17:02:24 +00:00
|
|
|
def _raw_gen(self, baseself, model, messages, stream=False, **kwargs):
|
2024-04-15 14:05:59 +00:00
|
|
|
context = messages[0]["content"]
|
|
|
|
user_question = messages[-1]["content"]
|
2023-09-29 00:09:01 +00:00
|
|
|
prompt = f"### Instruction \n {user_question} \n ### Context \n {context} \n ### Answer \n"
|
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
# Construct payload for endpoint
|
|
|
|
payload = {
|
|
|
|
"inputs": prompt,
|
|
|
|
"stream": False,
|
|
|
|
"parameters": {
|
|
|
|
"do_sample": True,
|
|
|
|
"temperature": 0.1,
|
|
|
|
"max_new_tokens": 30,
|
|
|
|
"repetition_penalty": 1.03,
|
2024-04-15 14:05:59 +00:00
|
|
|
"stop": ["</s>", "###"],
|
|
|
|
},
|
2023-10-06 00:52:29 +00:00
|
|
|
}
|
2024-04-15 14:05:59 +00:00
|
|
|
body_bytes = json.dumps(payload).encode("utf-8")
|
2023-09-29 00:09:01 +00:00
|
|
|
|
2023-10-06 00:52:29 +00:00
|
|
|
# Invoke the endpoint
|
2024-04-15 14:05:59 +00:00
|
|
|
response = self.runtime.invoke_endpoint(
|
|
|
|
EndpointName=self.endpoint, ContentType="application/json", Body=body_bytes
|
|
|
|
)
|
|
|
|
result = json.loads(response["Body"].read().decode())
|
2023-10-06 00:52:29 +00:00
|
|
|
import sys
|
2023-09-29 00:09:01 +00:00
|
|
|
|
2024-04-15 14:05:59 +00:00
|
|
|
print(result[0]["generated_text"], file=sys.stderr)
|
|
|
|
return result[0]["generated_text"][len(prompt) :]
|
|
|
|
|
2024-04-15 17:02:24 +00:00
|
|
|
def _raw_gen_stream(self, baseself, model, messages, stream=True, **kwargs):
|
2024-04-15 14:05:59 +00:00
|
|
|
context = messages[0]["content"]
|
|
|
|
user_question = messages[-1]["content"]
|
2023-10-06 00:52:29 +00:00
|
|
|
prompt = f"### Instruction \n {user_question} \n ### Context \n {context} \n ### Answer \n"
|
|
|
|
|
|
|
|
# Construct payload for endpoint
|
|
|
|
payload = {
|
|
|
|
"inputs": prompt,
|
|
|
|
"stream": True,
|
|
|
|
"parameters": {
|
|
|
|
"do_sample": True,
|
|
|
|
"temperature": 0.1,
|
|
|
|
"max_new_tokens": 512,
|
|
|
|
"repetition_penalty": 1.03,
|
2024-04-15 14:05:59 +00:00
|
|
|
"stop": ["</s>", "###"],
|
|
|
|
},
|
2023-10-06 00:52:29 +00:00
|
|
|
}
|
2024-04-15 14:05:59 +00:00
|
|
|
body_bytes = json.dumps(payload).encode("utf-8")
|
2023-10-06 00:52:29 +00:00
|
|
|
|
|
|
|
# Invoke the endpoint
|
2024-04-15 14:05:59 +00:00
|
|
|
response = self.runtime.invoke_endpoint_with_response_stream(
|
|
|
|
EndpointName=self.endpoint, ContentType="application/json", Body=body_bytes
|
|
|
|
)
|
|
|
|
# result = json.loads(response['Body'].read().decode())
|
|
|
|
event_stream = response["Body"]
|
|
|
|
start_json = b"{"
|
2023-10-06 00:52:29 +00:00
|
|
|
for line in LineIterator(event_stream):
|
2024-04-15 14:05:59 +00:00
|
|
|
if line != b"" and start_json in line:
|
|
|
|
# print(line)
|
|
|
|
data = json.loads(line[line.find(start_json) :].decode("utf-8"))
|
|
|
|
if data["token"]["text"] not in ["</s>", "###"]:
|
|
|
|
print(data["token"]["text"], end="")
|
|
|
|
yield data["token"]["text"]
|