feat(bot): improve prompt-building and help command handling

- Added a default system prompt and adjusted the structure to use XML for clarity.
- Improved help command handling by simplifying triggers and updating responses.
- Enhanced NixOS configuration with support for optional custom instructions.
This commit is contained in:
Harald Hoyer 2026-02-03 17:39:31 +01:00
parent 9342933987
commit f25aab2441
2 changed files with 33 additions and 10 deletions

View file

@ -101,22 +101,44 @@ def verify_signature(body: bytes, signature: str, random: Optional[str] = None)
return False return False
DEFAULT_SYSTEM_PROMPT = """Du bist Claude, ein KI-Assistent im Nextcloud Talk Chat.
Deine Antworten werden direkt in den Chatraum gepostet.
Halte deine Antworten kurz und prägnant, da es ein Chat ist.
Nutze Markdown für Formatierung wenn sinnvoll.
Du erhältst:
- <chat_history>: Die letzten Nachrichten im Chatraum (User und deine Antworten)
- <current_message>: Die aktuelle Nachricht, auf die du antworten sollst"""
def build_prompt(conversation_token: str, current_message: str, current_user: str) -> str: def build_prompt(conversation_token: str, current_message: str, current_user: str) -> str:
"""Build prompt with in-memory conversation history.""" """Build prompt with in-memory conversation history using XML structure."""
parts = [] parts = []
# Add system prompt (hardcoded + optional custom)
parts.append("<system>")
parts.append(DEFAULT_SYSTEM_PROMPT)
if SYSTEM_PROMPT: if SYSTEM_PROMPT:
parts.append(f"System: {SYSTEM_PROMPT}\n") parts.append("")
parts.append(SYSTEM_PROMPT.strip())
parts.append("</system>")
parts.append("")
# Add recent history from memory # Add chat history if available
history = conversations.get(conversation_token, []) history = conversations.get(conversation_token, [])
for role, msg in history[-MAX_HISTORY:]: if history:
parts.append(f"{role}: {msg}") parts.append("<chat_history>")
for role, msg in history[-MAX_HISTORY:]:
parts.append(f"{role}: {msg}")
parts.append("</chat_history>")
parts.append("")
# Add current message # Add current message
parts.append(f"User ({current_user}): {current_message}") parts.append(f"<current_message user=\"{current_user}\">")
parts.append(current_message)
parts.append("</current_message>")
return "\n\n".join(parts) return "\n".join(parts)
async def call_claude(prompt: str) -> str: async def call_claude(prompt: str) -> str:
@ -277,17 +299,16 @@ async def handle_webhook(
log.info(f"Processing message from {actor_id}: {message_text[:100]}") log.info(f"Processing message from {actor_id}: {message_text[:100]}")
if message_text.strip().lower() in ("/help", "/hilfe"): if message_text.strip().lower() in ("hilfe", "help", "?"):
help_text = """🤖 **Claude Bot Hilfe** help_text = """🤖 **Claude Bot Hilfe**
Schreib mir einfach eine Nachricht und ich antworte dir. Schreib mir einfach eine Nachricht und ich antworte dir.
**Nutzung:** **Nutzung:**
In Direktnachrichten: Einfach schreiben
In Gruppenchats: @Claude gefolgt von deiner Frage In Gruppenchats: @Claude gefolgt von deiner Frage
**Befehle:** **Befehle:**
`/help` oder `/hilfe` Diese Hilfe anzeigen `hilfe` oder `?` Diese Hilfe anzeigen
Der Bot merkt sich die letzten Nachrichten pro Raum (bis zum Neustart).""" Der Bot merkt sich die letzten Nachrichten pro Raum (bis zum Neustart)."""
await send_reply(conversation_token, help_text, reply_to=message_id) await send_reply(conversation_token, help_text, reply_to=message_id)

View file

@ -7,6 +7,8 @@
nextcloudUrl = "https://nc.hoyer.xyz"; nextcloudUrl = "https://nc.hoyer.xyz";
botSecretFile = config.sops.secrets."nextcloud-claude-bot/secret".path; botSecretFile = config.sops.secrets."nextcloud-claude-bot/secret".path;
allowedUsers = []; # Allow all registered users allowedUsers = []; # Allow all registered users
# Optional extra instructions (base prompt is hardcoded in bot.py)
# systemPrompt = "Additional custom instructions here";
}; };
sops.secrets."nextcloud-claude-bot/secret" = { sops.secrets."nextcloud-claude-bot/secret" = {