44 lines
1.5 KiB
Python
44 lines
1.5 KiB
Python
"""OpenRouter LLM-Wrapper für natürliche Antworten."""
|
|
|
|
import requests
|
|
import os
|
|
import sys
|
|
|
|
sys.path.insert(0, os.path.dirname(__file__))
|
|
from core import config
|
|
|
|
MODEL = "openai/gpt-4o-mini"
|
|
SYSTEM_PROMPT = """Du bist der Hausmeister-Bot für ein Homelab mit mehreren Proxmox-Servern.
|
|
Du antwortest kurz, präzise und auf Deutsch.
|
|
Du bekommst Live-Daten aus Loki (Logs), Proxmox (Container-Status) und homelab.conf.
|
|
Wenn alles in Ordnung ist, sag das kurz. Bei Problemen erkläre was los ist und schlage Lösungen vor.
|
|
Nutze Emojis sparsam. Formatiere für Telegram (kein Markdown, nur einfacher Text)."""
|
|
|
|
|
|
def _get_api_key() -> str:
|
|
cfg = config.parse_config()
|
|
return cfg.api_keys.get("openrouter_key", "")
|
|
|
|
|
|
def ask(question: str, context: str) -> str:
|
|
"""Stellt eine Frage mit Kontext an OpenRouter."""
|
|
api_key = _get_api_key()
|
|
if not api_key:
|
|
return "OpenRouter API Key fehlt in homelab.conf"
|
|
|
|
messages = [
|
|
{"role": "system", "content": SYSTEM_PROMPT},
|
|
{"role": "user", "content": f"Kontext (Live-Daten):\n{context}\n\nFrage: {question}"},
|
|
]
|
|
|
|
try:
|
|
r = requests.post(
|
|
"https://openrouter.ai/api/v1/chat/completions",
|
|
headers={"Authorization": f"Bearer {api_key}"},
|
|
json={"model": MODEL, "messages": messages, "max_tokens": 500},
|
|
timeout=30,
|
|
)
|
|
r.raise_for_status()
|
|
return r.json()["choices"][0]["message"]["content"]
|
|
except Exception as e:
|
|
return f"LLM-Fehler: {e}"
|