diff --git a/nemoguardrails/actions/llm/utils.py b/nemoguardrails/actions/llm/utils.py index 012043c04..34169b028 100644 --- a/nemoguardrails/actions/llm/utils.py +++ b/nemoguardrails/actions/llm/utils.py @@ -534,6 +534,23 @@ def get_first_user_intent(strings: List[str]) -> Optional[str]: return None +def quote_bot_say(line: str) -> str: + if line.startswith("bot say "): + message = line[len("bot say "):] + return f"{line}\nbot say \"{message}\"" + return line + + +def correct_bot_action(line: str) -> str: + match = re.search(r'"(.*)"', line, re.DOTALL) + if match: + message = match.group(1).strip() + return f'bot say "{message}"' + else: + # fallback: no quoted part, return as-is or handle differently + return f'bot say "{line.strip()}"' + + def get_first_bot_intent(strings: List[str]) -> Optional[str]: """Returns first bot intent.""" for string in strings: @@ -545,8 +562,11 @@ def get_first_bot_intent(strings: List[str]) -> Optional[str]: def get_first_bot_action(strings: List[str]) -> Optional[str]: """Returns first bot action.""" action_started = False + fallback_action: str = "" action: str = "" + for string in strings: + fallback_action = string if string.startswith("bot action: "): if action != "": action += "\n" @@ -561,7 +581,12 @@ def get_first_bot_action(strings: List[str]) -> Optional[str]: continue elif action != "": return action - return action + + result = action if action != "" else fallback_action + + result = correct_bot_action(result) + + return result def escape_flow_name(name: str) -> str: