Skip to content

Commit

Permalink
fix: ensure parsing doesn't include tailing parenthesis (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-twhite authored Dec 11, 2024
1 parent c16e088 commit f35553f
Showing 1 changed file with 9 additions and 13 deletions.
22 changes: 9 additions & 13 deletions agent_gateway/gateway/gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,24 +240,20 @@ def _parse_fusion_output(self, raw_answer: str) -> str:

return thought, answer, is_replan

def _extract_answer(self, raw_answer):
@staticmethod
def _extract_answer(raw_answer):
start_marker = "Action: Finish("
end_marker = "<END_OF_RESPONSE>"
end_parens = raw_answer.rfind(")")

start_index = raw_answer.find(start_marker)
if start_index != -1:
start_index += len(start_marker)
end_index = raw_answer.find(end_marker, start_index)
pattern = re.compile(
rf"{re.escape(start_marker)}(.*?)(?=\)\s*{re.escape(end_marker)})",
re.DOTALL,
)

if end_index != -1:
return raw_answer[start_index:end_index].strip()
elif end_parens > start_index:
return raw_answer[start_index:end_parens].strip()
else:
return raw_answer[start_index:].strip()
match = pattern.search(raw_answer)
if match:
return match.group(1).strip()

# Handle "Replan" case
if "Replan" in raw_answer:
return "Replan required. Consider rephrasing your question."

Expand Down

0 comments on commit f35553f

Please sign in to comment.