Skip to content

Commit e38ed6d

Browse files
Fix guardrails out handle logics for space linebreak and quote (#571)
* fix mismatched response format w/wo streaming guardrails * fix & debug * fix & rm debug * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * debug * debug * debug * fix pre-space and linebreak * fix pre-space and linebreak * fix single/double quote * fix single/double quote * remove debug * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent b6c0785 commit e38ed6d

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

comps/cores/mega/orchestrator.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,18 +188,20 @@ def extract_chunk_str(self, chunk_str):
188188
if chunk_str == "data: [DONE]\n\n":
189189
return ""
190190
prefix = "data: b'"
191+
prefix_2 = 'data: b"'
191192
suffix = "'\n\n"
192-
if chunk_str.startswith(prefix):
193+
suffix_2 = '"\n\n'
194+
if chunk_str.startswith(prefix) or chunk_str.startswith(prefix_2):
193195
chunk_str = chunk_str[len(prefix) :]
194-
if chunk_str.endswith(suffix):
196+
if chunk_str.endswith(suffix) or chunk_str.endswith(suffix_2):
195197
chunk_str = chunk_str[: -len(suffix)]
196198
return chunk_str
197199

198200
def token_generator(self, sentence, is_last=False):
199201
prefix = "data: "
200202
suffix = "\n\n"
201-
tokens = re.findall(r"\S+\s?", sentence, re.UNICODE)
203+
tokens = re.findall(r"\s?\S+\s?", sentence, re.UNICODE)
202204
for token in tokens:
203-
yield prefix + repr(token.encode("utf-8")) + suffix
205+
yield prefix + repr(token.replace("\\n", "\n").encode("utf-8")) + suffix
204206
if is_last:
205207
yield "data: [DONE]\n\n"

0 commit comments

Comments
 (0)