diff --git a/apple_llm.py b/apple_llm.py index 7bba0aa..a790d96 100644 --- a/apple_llm.py +++ b/apple_llm.py @@ -92,8 +92,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text) + # Truncate if model hallucinates fake subsequent user turns + for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"): if marker in text: text = text.split(marker)[0] # Remove markdown code blocks diff --git a/ollama_llm.py b/ollama_llm.py index e33f758..7ee220f 100644 --- a/ollama_llm.py +++ b/ollama_llm.py @@ -42,8 +42,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text) + # Truncate if model hallucinates fake subsequent user turns + for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"): if marker in text: text = text.split(marker)[0] # Remove markdown code blocks diff --git a/opencode_llm.py b/opencode_llm.py index 70fd1f2..872f680 100644 --- a/opencode_llm.py +++ b/opencode_llm.py @@ -46,8 +46,10 @@ def _clean_spoken_text(text: str) -> str: """Clean text for speech output and truncate fake turn generations.""" if not text: return "" - # Truncate if model hallucinates fake turn markers - for marker in ("User:", "Human:", "Assistant:", "\nUser", "\nHuman", "\nAssistant"): + # Strip leading or inline role headers (e.g. "Assistant:") + text = re.sub(r"(?i)\b(Assistant|assistant|Bot|bot):\s*", "", text) + # Truncate if model hallucinates fake subsequent user turns + for marker in ("\nUser:", "\nHuman:", "\nUser", "\nHuman"): if marker in text: text = text.split(marker)[0] # Remove markdown code blocks