[Fix] revert sagemaker llm to support model hub (#12378)

pull/12396/head
Warren Chen 1 year ago committed by GitHub
parent 9c317b64c3
commit 147d578922
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -1,5 +1,6 @@
import json import json
import logging import logging
import re
from collections.abc import Generator, Iterator from collections.abc import Generator, Iterator
from typing import Any, Optional, Union, cast from typing import Any, Optional, Union, cast
@ -131,115 +132,58 @@ class SageMakerLargeLanguageModel(LargeLanguageModel):
""" """
handle stream chat generate response handle stream chat generate response
""" """
class ChunkProcessor:
def __init__(self):
self.buffer = bytearray()
def try_decode_chunk(self, chunk: bytes) -> list[dict]:
"""尝试从chunk中解码出完整的JSON对象"""
self.buffer.extend(chunk)
results = []
while True:
try:
start = self.buffer.find(b"{")
if start == -1:
self.buffer.clear()
break
bracket_count = 0
end = start
for i in range(start, len(self.buffer)):
if self.buffer[i] == ord("{"):
bracket_count += 1
elif self.buffer[i] == ord("}"):
bracket_count -= 1
if bracket_count == 0:
end = i + 1
break
if bracket_count != 0:
# JSON不完整等待更多数据
if start > 0:
self.buffer = self.buffer[start:]
break
json_bytes = self.buffer[start:end]
try:
data = json.loads(json_bytes)
results.append(data)
self.buffer = self.buffer[end:]
except json.JSONDecodeError:
self.buffer = self.buffer[start + 1 :]
except Exception as e:
logger.debug(f"Warning: Error processing chunk ({str(e)})")
if start > 0:
self.buffer = self.buffer[start:]
break
return results
full_response = "" full_response = ""
processor = ChunkProcessor() buffer = ""
for chunk_bytes in resp:
try: buffer += chunk_bytes.decode("utf-8")
for chunk in resp: last_idx = 0
json_objects = processor.try_decode_chunk(chunk) for match in re.finditer(r"^data:\s*(.+?)(\n\n)", buffer):
try:
for data in json_objects: data = json.loads(match.group(1).strip())
if data.get("choices"): last_idx = match.span()[1]
choice = data["choices"][0]
if "content" in data["choices"][0]["delta"]:
if "delta" in choice and "content" in choice["delta"]: chunk_content = data["choices"][0]["delta"]["content"]
chunk_content = choice["delta"]["content"] assistant_prompt_message = AssistantPromptMessage(content=chunk_content, tool_calls=[])
assistant_prompt_message = AssistantPromptMessage(content=chunk_content, tool_calls=[])
if data["choices"][0]["finish_reason"] is not None:
if choice.get("finish_reason") is not None: temp_assistant_prompt_message = AssistantPromptMessage(content=full_response, tool_calls=[])
temp_assistant_prompt_message = AssistantPromptMessage( prompt_tokens = self._num_tokens_from_messages(messages=prompt_messages, tools=tools)
content=full_response, tool_calls=[] completion_tokens = self._num_tokens_from_messages(
) messages=[temp_assistant_prompt_message], tools=[]
)
prompt_tokens = self._num_tokens_from_messages(messages=prompt_messages, tools=tools) usage = self._calc_response_usage(
completion_tokens = self._num_tokens_from_messages( model=model,
messages=[temp_assistant_prompt_message], tools=[] credentials=credentials,
) prompt_tokens=prompt_tokens,
completion_tokens=completion_tokens,
usage = self._calc_response_usage( )
model=model,
credentials=credentials, yield LLMResultChunk(
prompt_tokens=prompt_tokens, model=model,
completion_tokens=completion_tokens, prompt_messages=prompt_messages,
) system_fingerprint=None,
delta=LLMResultChunkDelta(
yield LLMResultChunk( index=0,
model=model, message=assistant_prompt_message,
prompt_messages=prompt_messages, finish_reason=data["choices"][0]["finish_reason"],
system_fingerprint=None, usage=usage,
delta=LLMResultChunkDelta( ),
index=0, )
message=assistant_prompt_message, else:
finish_reason=choice["finish_reason"], yield LLMResultChunk(
usage=usage, model=model,
), prompt_messages=prompt_messages,
) system_fingerprint=None,
else: delta=LLMResultChunkDelta(index=0, message=assistant_prompt_message),
yield LLMResultChunk( )
model=model,
prompt_messages=prompt_messages, full_response += chunk_content
system_fingerprint=None, except (json.JSONDecodeError, KeyError, IndexError) as e:
delta=LLMResultChunkDelta(index=0, message=assistant_prompt_message), logger.info("json parse exception, content: {}".format(match.group(1).strip()))
) pass
full_response += chunk_content buffer = buffer[last_idx:]
except Exception as e:
raise
if not full_response:
logger.warning("No content received from stream response")
def _invoke( def _invoke(
self, self,

Loading…
Cancel
Save