diff --git a/apple_llm.py b/apple_llm.py index 81d2ed3..7bba0aa 100644 --- a/apple_llm.py +++ b/apple_llm.py @@ -86,13 +86,34 @@ def _latest_user_text(context) -> str: return "" +import re + +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"): + if marker in text: + text = text.split(marker)[0] + # Remove markdown code blocks + text = re.sub(r"```[\s\S]*?```", "", text) + # Remove inline code ticks + text = re.sub(r"`[^`]*`", "", text) + # Remove markdown syntax characters + text = re.sub(r"[\#\*\_\~]", "", text) + # Flatten newlines into clear speech + lines = [line.strip() for line in text.splitlines() if line.strip()] + return " ".join(lines).strip() + + class MacOSLLM(FrameProcessor): """Runs user turns through macOS native LLM (Apple Intelligence or MLX).""" def __init__( self, *, - model: str = "mlx-community/gemma-2-2b-it-4bit", + model: str = "mlx-community/Qwen2.5-7B-Instruct-4bit", system_prompt: str | None = None, observer=None, **kwargs, diff --git a/bot.py b/bot.py index 90c22a0..41d311e 100644 --- a/bot.py +++ b/bot.py @@ -159,8 +159,8 @@ def parse_args() -> argparse.Namespace: ) parser.add_argument( "--mlx-model", - default="mlx-community/gemma-2-2b-it-4bit", - help="MLX model repo or path for local macOS execution (e.g. mlx-community/gemma-2-2b-it-4bit).", + default="mlx-community/Qwen2.5-7B-Instruct-4bit", + help="MLX model repo or path for local macOS execution (e.g. mlx-community/Qwen2.5-7B-Instruct-4bit).", ) parser.add_argument( "--claude-model", @@ -495,6 +495,8 @@ def build_llm(args: argparse.Namespace, vocabulary=None, brain=None, observer=No logger.info(f"LLM: macOS native model ({reason})") personality = read_personality(args.cwd) or "You are a helpful macOS voice assistant." system_prompt = personality + "\n\n" + VOICE_STYLE + if brain and (memory := brain.prompt_block()): + system_prompt += "\n\n" + memory return MacOSLLM(model=args.mlx_model, system_prompt=system_prompt, observer=observer) logger.info("LLM: Claude Code")