Skip to content

Commit

Permalink
Initial attempt to fix
Browse files Browse the repository at this point in the history
  • Loading branch information
IzzyPutterman committed Jul 24, 2024
1 parent 18a377c commit 2dc2111
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,14 @@ def _preprocess_response(
if self._service_kind == "openai":
# PA sometimes receives multiple SSE responses at once (as a single
# response). Handle these responses by merging into a single response.
for i in range(len(res_outputs) - 1, -1, -1):
response = res_outputs[i]["response"]
if not response.startswith("data: "):
first_data = response.find("data: ")
res_outputs[i - 1]["response"] = (
res_outputs[i - 1]["response"] + response[0:first_data].strip()
)
res_outputs[i]["response"] = response[first_data:].strip()
for i in range(len(res_outputs)):
response = res_outputs[i]["response"]
responses = response.strip().split("\n\n")
Expand All @@ -193,7 +201,9 @@ def _preprocess_response(
# Remove responses without any content
indices_to_remove = []
for idx, out in enumerate(res_outputs):
if self._is_openai_empty_response(out["response"]):
if not out["response"] or self._is_openai_empty_response(
out["response"]
):
indices_to_remove.append(idx)
indices_to_remove.sort(reverse=True)
for index in indices_to_remove:
Expand Down Expand Up @@ -268,7 +278,9 @@ def _extract_openai_text_output(self, response: str) -> str:
"""Extracts text/content of the OpenAI response object."""
response = remove_sse_prefix(response)

if response == "[DONE]":
if response == "[DONE]" or not (
response.strip().startswith("{") and response.strip().endswith("}")
):
return ""

data = load_json_str(response)
Expand Down

0 comments on commit 2dc2111

Please sign in to comment.