MEDIUM: Extract system prompt to a single shared source (eliminate triple duplication) #34

Open
opened 2026-02-20 14:45:02 +02:00 by Koko210 · 0 comments
Owner

Problem

The Miku personality system prompt is duplicated in three separate locations:

  1. bot/utils/llm.py — query_llama() builds the system prompt for direct LLM calls
  2. cat-plugins/miku_personality/miku_personality.py — agent_prompt_prefix() builds an identical prompt for Cheshire Cat
  3. bot/api.py — The /chat/stream SSE endpoint builds yet another copy of the system prompt

Any personality or prompt change must be manually synchronized across all three files. If one is missed, the bot exhibits inconsistent personality depending on the code path (direct LLM vs Cat vs dashboard chat).

Proposed Solution

Create a single prompt builder module:

# bot/utils/prompt_builder.py
def build_system_prompt(mood, evil_mode, japanese_mode, context_type='default'):
    # Single source of truth for all system prompt construction
    # Loads lore, lyrics, prompt files
    # Applies mood, evil mode, language overlays
    # Returns the complete system prompt string

Then:

  • bot/utils/llm.py calls prompt_builder.build_system_prompt()
  • bot/api.py /chat/stream calls prompt_builder.build_system_prompt()
  • cat-plugins/miku_personality/ imports or receives the prompt via the Cat working_memory metadata (already receives mood — extend to pass the full prompt string from the bot side, or have the plugin call a shared HTTP endpoint)

For the Cheshire Cat plugin (which runs in a separate container), either:

  • Pass the pre-built prompt string in the WebSocket message metadata
  • Or have the plugin call the bot's API to get the current prompt

Impact

  • Risk: Very Low — behavioral parity guaranteed by sharing one function
  • Effort: Low (extract existing code, replace 3 call sites)
  • Benefit: Eliminates a class of subtle personality inconsistency bugs

Files Affected

  • NEW: bot/utils/prompt_builder.py
  • bot/utils/llm.py (use shared builder)
  • bot/api.py (use shared builder)
  • cat-plugins/miku_personality/miku_personality.py (receive prompt or call API)
  • bot/utils/context_manager.py (may be absorbed into prompt_builder)
## Problem The Miku personality system prompt is duplicated in three separate locations: 1. **bot/utils/llm.py** — query_llama() builds the system prompt for direct LLM calls 2. **cat-plugins/miku_personality/miku_personality.py** — agent_prompt_prefix() builds an identical prompt for Cheshire Cat 3. **bot/api.py** — The /chat/stream SSE endpoint builds yet another copy of the system prompt Any personality or prompt change must be manually synchronized across all three files. If one is missed, the bot exhibits inconsistent personality depending on the code path (direct LLM vs Cat vs dashboard chat). ## Proposed Solution Create a single prompt builder module: # bot/utils/prompt_builder.py def build_system_prompt(mood, evil_mode, japanese_mode, context_type='default'): # Single source of truth for all system prompt construction # Loads lore, lyrics, prompt files # Applies mood, evil mode, language overlays # Returns the complete system prompt string Then: - bot/utils/llm.py calls prompt_builder.build_system_prompt() - bot/api.py /chat/stream calls prompt_builder.build_system_prompt() - cat-plugins/miku_personality/ imports or receives the prompt via the Cat working_memory metadata (already receives mood — extend to pass the full prompt string from the bot side, or have the plugin call a shared HTTP endpoint) For the Cheshire Cat plugin (which runs in a separate container), either: - Pass the pre-built prompt string in the WebSocket message metadata - Or have the plugin call the bot's API to get the current prompt ## Impact - Risk: Very Low — behavioral parity guaranteed by sharing one function - Effort: Low (extract existing code, replace 3 call sites) - Benefit: Eliminates a class of subtle personality inconsistency bugs ## Files Affected - NEW: bot/utils/prompt_builder.py - bot/utils/llm.py (use shared builder) - bot/api.py (use shared builder) - cat-plugins/miku_personality/miku_personality.py (receive prompt or call API) - bot/utils/context_manager.py (may be absorbed into prompt_builder)
Sign in to join this conversation.
No Label
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: Koko210/miku-discord#34