122 lines
4.6 KiB
Python
122 lines
4.6 KiB
Python
import os
|
|
import logging
|
|
import aiohttp
|
|
import asyncio
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
OPENROUTER_API_KEY = os.environ.get('OPENROUTER_API_KEY', '')
|
|
OPENROUTER_BASE = "https://openrouter.ai/api/v1"
|
|
DEFAULT_MODEL = os.environ.get('AI_MODEL', 'openai/gpt-4o-mini')
|
|
|
|
|
|
async def generate_article(source: str, prompt_template: str, date_str: str, tag: str = "allgemein") -> str:
|
|
system_prompt = prompt_template.format(
|
|
source=source,
|
|
date=date_str,
|
|
tag=tag.lower().replace(" ", "")
|
|
)
|
|
payload = {
|
|
"model": DEFAULT_MODEL,
|
|
"messages": [
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": f"Schreibe jetzt den Artikel basierend auf dieser Quelle:\n\n{source}"}
|
|
],
|
|
"max_tokens": 600,
|
|
"temperature": 0.8
|
|
}
|
|
headers = {
|
|
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
"HTTP-Referer": "https://fuenfvoracht.orbitalo.net",
|
|
"X-Title": "FünfVorAcht Bot"
|
|
}
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.post(f"{OPENROUTER_BASE}/chat/completions", json=payload, headers=headers) as resp:
|
|
data = await resp.json()
|
|
if resp.status != 200:
|
|
raise Exception(f"OpenRouter Fehler {resp.status}: {data}")
|
|
return data["choices"][0]["message"]["content"].strip()
|
|
|
|
|
|
async def get_balance() -> dict:
|
|
headers = {
|
|
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
"Content-Type": "application/json"
|
|
}
|
|
try:
|
|
async with aiohttp.ClientSession() as session:
|
|
async with session.get(f"{OPENROUTER_BASE}/auth/key", headers=headers) as resp:
|
|
if resp.status == 200:
|
|
data = await resp.json()
|
|
key_data = data.get("data", {})
|
|
limit = key_data.get("limit")
|
|
usage = key_data.get("usage", 0)
|
|
if limit:
|
|
remaining = round(limit - usage, 4)
|
|
else:
|
|
remaining = None
|
|
return {
|
|
"usage": round(usage, 4),
|
|
"limit": limit,
|
|
"remaining": remaining,
|
|
"label": key_data.get("label", ""),
|
|
"is_free_tier": key_data.get("is_free_tier", False)
|
|
}
|
|
except Exception as e:
|
|
logger.error("Balance-Abfrage fehlgeschlagen: %s", e)
|
|
return {"usage": None, "limit": None, "remaining": None}
|
|
|
|
|
|
def get_balance_sync() -> dict:
|
|
return asyncio.run(get_balance())
|
|
|
|
|
|
def generate(system_prompt: str, user_message: str) -> str:
|
|
import asyncio, aiohttp as _aiohttp
|
|
async def _gen():
|
|
payload = {
|
|
"model": DEFAULT_MODEL,
|
|
"messages": [
|
|
{"role": "system", "content": system_prompt},
|
|
{"role": "user", "content": user_message}
|
|
],
|
|
"max_tokens": 2500,
|
|
"temperature": 0.8
|
|
}
|
|
headers = {
|
|
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
timeout = _aiohttp.ClientTimeout(total=90)
|
|
async with _aiohttp.ClientSession(timeout=timeout) as session:
|
|
async with session.post(f"{OPENROUTER_BASE}/chat/completions", json=payload, headers=headers) as resp:
|
|
data = await resp.json()
|
|
if resp.status != 200:
|
|
raise Exception(f"OpenRouter Fehler {resp.status}: {data}")
|
|
return data["choices"][0]["message"]["content"].strip()
|
|
return asyncio.run(_gen())
|
|
|
|
|
|
def generate_chat(messages: list) -> str:
|
|
"""Chat mit History. messages: [{"role":"system/user/assistant","content":"..."}, ...]"""
|
|
import asyncio, aiohttp as _aiohttp
|
|
async def _gen():
|
|
payload = {
|
|
"model": DEFAULT_MODEL,
|
|
"messages": messages,
|
|
"max_tokens": 2500,
|
|
"temperature": 0.7
|
|
}
|
|
headers = {
|
|
"Authorization": f"Bearer {OPENROUTER_API_KEY}",
|
|
"Content-Type": "application/json",
|
|
}
|
|
timeout = _aiohttp.ClientTimeout(total=90)
|
|
async with _aiohttp.ClientSession(timeout=timeout) as session:
|
|
async with session.post(f"{OPENROUTER_BASE}/chat/completions", json=payload, headers=headers) as resp:
|
|
data = await resp.json()
|
|
if resp.status != 200:
|
|
raise Exception(f"OpenRouter Fehler {resp.status}: {data}")
|
|
return data["choices"][0]["message"]["content"].strip()
|
|
return asyncio.run(_gen())
|