aboutsummaryrefslogtreecommitdiffstats
path: root/cogs/utils/llm.py
blob: 2b05b12bc97f62f12dadeeecb85b2b59693fe6d3 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# This file is part of Senator Sperma
# Licensed under the GPL-3.0 License
# Copyright (C) 2026 Salmonik @ salmonik@tuta.io
# https://codeberg.org/salmonik/senator-sperma

import os
import httpx
import logging

logger = logging.getLogger(__name__)

BACKWARD_COMPAT_MSG = (
    "OPENROUTER_API_KEY/OPENROUTER_MODEL are deprecated. "
    "Use LLM_API_KEY, LLM_MODEL instead."
)


def _get_config():
    api_url = os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1/chat/completions")
    api_key = os.getenv("LLM_API_KEY") or os.getenv("OPENROUTER_API_KEY") or ""
    model = os.getenv("LLM_MODEL") or os.getenv("OPENROUTER_MODEL") or ""

    if os.getenv("OPENROUTER_API_KEY") and not os.getenv("LLM_API_KEY"):
        logger.warning(BACKWARD_COMPAT_MSG)
    if os.getenv("OPENROUTER_MODEL") and not os.getenv("LLM_MODEL"):
        logger.warning(BACKWARD_COMPAT_MSG)

    return api_url, api_key, model


async def llm_completion(
    messages: list[dict],
    model: str | None = None,
    api_url: str | None = None,
    api_key: str | None = None,
    extra: dict | None = None,
    timeout: float = 180.0,
) -> str | None:
    url = api_url or os.getenv("LLM_API_URL", "https://openrouter.ai/api/v1/chat/completions")
    model = model or os.getenv("LLM_MODEL") or os.getenv("OPENROUTER_MODEL") or ""
    api_key = api_key or os.getenv("LLM_API_KEY") or os.getenv("OPENROUTER_API_KEY") or ""

    headers = {"Content-Type": "application/json"}
    if api_key:
        headers["Authorization"] = f"Bearer {api_key}"

    payload: dict = {"model": model, "messages": messages}
    if extra:
        payload.update(extra)

    verify = os.getenv("LLM_API_VERIFY", "true").lower() in ("true", "1", "yes")

    try:
        async with httpx.AsyncClient(timeout=timeout, verify=verify) as client:
            resp = await client.post(url, headers=headers, json=payload)
            resp.raise_for_status()
            data = resp.json()
            logger.info("LLM response: %s", data)
            return data["choices"][0]["message"]["content"]
    except httpx.HTTPError as e:
        logger.error("LLM API request failed (url=%s, model=%s): %s", url, model, e, exc_info=True)
        return None