fix(bot): Systemvorhersage bei OpenRouter-404 nicht als Erfolg senden

Fallback auf gpt-4o-mini wenn grok fehlschlaegt; klare Fehlermeldung
statt rohem LLM-Fehler-String in Telegram.
This commit is contained in:
Homelab Cursor 2026-05-26 11:06:56 +02:00
parent 6506f99447
commit 169ea0c71f
2 changed files with 26 additions and 3 deletions

View file

@ -341,7 +341,7 @@ def ask(question: str, context: str) -> str:
return f"LLM-Fehler: {e}" return f"LLM-Fehler: {e}"
def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None, document_mode: bool = False) -> str: def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None, document_mode: bool = False, model_override: str = None) -> str:
"""Freitext-Frage mit automatischem Routing und Tool-Calling. """Freitext-Frage mit automatischem Routing und Tool-Calling.
Routing: Routing:
@ -573,11 +573,12 @@ def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None, d
return f"Online-Suche Fehler: {e}" return f"Online-Suche Fehler: {e}"
# --- Lokal: Tool-Calling mit allen Tools --- # --- Lokal: Tool-Calling mit allen Tools ---
local_model = model_override or MODEL_LOCAL
passthrough_result = None passthrough_result = None
try: try:
for _round in range(MAX_TOOL_ROUNDS): for _round in range(MAX_TOOL_ROUNDS):
data = _call_api(messages, api_key, use_tools=True, model=MODEL_LOCAL) data = _call_api(messages, api_key, use_tools=True, model=local_model)
choice = data["choices"][0] choice = data["choices"][0]
msg = choice["message"] msg = choice["message"]
@ -623,7 +624,7 @@ def ask_with_tools(question: str, tool_handlers: dict, session_id: str = None, d
if passthrough_result: if passthrough_result:
return passthrough_result return passthrough_result
data = _call_api(messages, api_key, use_tools=False, model=MODEL_LOCAL) data = _call_api(messages, api_key, use_tools=False, model=local_model)
return data["choices"][0]["message"]["content"] return data["choices"][0]["message"]["content"]
except Exception as e: except Exception as e:

View file

@ -1016,6 +1016,28 @@ async def _send_daily_forecast(job_context):
session_id=None, session_id=None,
document_mode=False, document_mode=False,
) )
if (analysis or "").startswith(("LLM-Fehler:", "Vision-LLM-Fehler:")):
log.error("Systemvorhersage LLM-Fehler: %s", analysis[:200])
analysis = await asyncio.to_thread(
llm.ask_with_tools,
prompt,
handlers,
session_id=None,
document_mode=False,
model_override=llm.MODEL_VISION,
)
if (analysis or "").startswith(("LLM-Fehler:", "Vision-LLM-Fehler:")):
await bot.send_message(
chat_id=CHAT_ID,
text=(
"🔭 *Taegliche Systemvorhersage*\n\n"
"⚠️ Analyse fehlgeschlagen (OpenRouter). "
"Bitte spaeter erneut versuchen."
),
parse_mode="Markdown",
)
log.error("Systemvorhersage endgueltig fehlgeschlagen: %s", analysis[:200])
return
msg = "🔭 *Taegliche Systemvorhersage*\n\n" + analysis msg = "🔭 *Taegliche Systemvorhersage*\n\n" + analysis
await bot.send_message(chat_id=CHAT_ID, text=msg, parse_mode="Markdown") await bot.send_message(chat_id=CHAT_ID, text=msg, parse_mode="Markdown")
log.info("Taegl. Systemvorhersage gesendet") log.info("Taegl. Systemvorhersage gesendet")