feat(bot): API-Kosten Tool — OpenRouter Guthaben/Verbrauch per Chat abfragen
This commit is contained in:
parent
36d708bee1
commit
785aa36a35
1 changed files with 84 additions and 0 deletions
84
homelab-ai-bot/tools/api_costs.py
Normal file
84
homelab-ai-bot/tools/api_costs.py
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
"""API-Kosten Tool — OpenRouter Guthaben und Verbrauch abfragen."""
|
||||
|
||||
import requests
|
||||
from core import config
|
||||
|
||||
TOOLS = [
|
||||
{
|
||||
"type": "function",
|
||||
"function": {
|
||||
"name": "get_api_costs",
|
||||
"description": "OpenRouter API-Kosten und Guthaben abfragen. Zeigt Verbrauch (heute/Woche/Monat), Restguthaben und Hochrechnung.",
|
||||
"parameters": {
|
||||
"type": "object",
|
||||
"properties": {},
|
||||
},
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
def _get_key():
|
||||
cfg = config.parse_config()
|
||||
return cfg.api_keys.get("openrouter_key", "")
|
||||
|
||||
|
||||
def handle_get_api_costs(**kw):
|
||||
key = _get_key()
|
||||
if not key:
|
||||
return "OpenRouter API Key fehlt in homelab.conf"
|
||||
|
||||
headers = {"Authorization": f"Bearer {key}"}
|
||||
|
||||
try:
|
||||
r_key = requests.get("https://openrouter.ai/api/v1/auth/key",
|
||||
headers=headers, timeout=10)
|
||||
r_key.raise_for_status()
|
||||
key_data = r_key.json().get("data", {})
|
||||
except Exception as e:
|
||||
return f"API-Abfrage fehlgeschlagen: {e}"
|
||||
|
||||
try:
|
||||
r_credits = requests.get("https://openrouter.ai/api/v1/credits",
|
||||
headers=headers, timeout=10)
|
||||
r_credits.raise_for_status()
|
||||
credit_data = r_credits.json().get("data", {})
|
||||
except Exception:
|
||||
credit_data = {}
|
||||
|
||||
usage_daily = key_data.get("usage_daily", 0)
|
||||
usage_weekly = key_data.get("usage_weekly", 0)
|
||||
usage_monthly = key_data.get("usage_monthly", 0)
|
||||
total_credits = credit_data.get("total_credits", 0)
|
||||
total_usage = credit_data.get("total_usage", 0)
|
||||
remaining = total_credits - total_usage if total_credits else None
|
||||
|
||||
monthly_forecast = usage_daily * 30 if usage_daily > 0 else 0
|
||||
|
||||
lines = [
|
||||
"OpenRouter API-Kosten",
|
||||
"",
|
||||
f"Heute: ${usage_daily:.4f}",
|
||||
f"Woche: ${usage_weekly:.4f}",
|
||||
f"Monat: ${usage_monthly:.4f}",
|
||||
]
|
||||
|
||||
if monthly_forecast > 0:
|
||||
lines.append(f"Prognose (30d): ${monthly_forecast:.2f}")
|
||||
|
||||
lines.append("")
|
||||
|
||||
if remaining is not None:
|
||||
lines.append(f"Guthaben: ${total_credits:.2f}")
|
||||
lines.append(f"Verbraucht: ${total_usage:.2f}")
|
||||
lines.append(f"Restguthaben: ${remaining:.2f}")
|
||||
if usage_daily > 0 and remaining > 0:
|
||||
days_left = remaining / usage_daily
|
||||
lines.append(f"Reicht noch: ~{days_left:.0f} Tage (bei aktuellem Verbrauch)")
|
||||
else:
|
||||
lines.append("Guthaben: unbegrenzt (kein Limit gesetzt)")
|
||||
|
||||
return "\n".join(lines)
|
||||
|
||||
|
||||
HANDLERS = {"get_api_costs": handle_get_api_costs}
|
||||
Loading…
Add table
Reference in a new issue