Skip to content

Commit

Permalink
Fix guardrails out handle logics for space linebreak and quote (opea-…
Browse files Browse the repository at this point in the history
…project#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>
  • Loading branch information
2 people authored and sharanshirodkar7 committed Sep 3, 2024
1 parent 3010b0a commit 57313eb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions comps/cores/mega/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,18 +188,20 @@ def extract_chunk_str(self, chunk_str):
if chunk_str == "data: [DONE]\n\n":
return ""
prefix = "data: b'"
prefix_2 = 'data: b"'
suffix = "'\n\n"
if chunk_str.startswith(prefix):
suffix_2 = '"\n\n'
if chunk_str.startswith(prefix) or chunk_str.startswith(prefix_2):
chunk_str = chunk_str[len(prefix) :]
if chunk_str.endswith(suffix):
if chunk_str.endswith(suffix) or chunk_str.endswith(suffix_2):
chunk_str = chunk_str[: -len(suffix)]
return chunk_str

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

0 comments on commit 57313eb

Please sign in to comment.