File size: 2,343 Bytes
2dd2de0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
63
64
65
66
67
"""Central configuration, loaded from the environment / .env file."""
import os

from dotenv import load_dotenv

load_dotenv()


def _require(name: str) -> str:
    value = os.getenv(name)
    if not value:
        raise RuntimeError(
            f"Missing required environment variable {name!r}. "
            "Copy .env.example to .env and fill it in."
        )
    return value


# --- Database ---
DB_SERVER = _require("DB_SERVER")
DB_PORT = os.getenv("DB_PORT", "1433")
DB_NAME = _require("DB_NAME")
DB_USER = _require("DB_USER")
DB_PASSWORD = _require("DB_PASSWORD")
DB_DRIVER = os.getenv("DB_DRIVER", "ODBC Driver 18 for SQL Server")

# --- LLM backend (OpenAI-compatible: Groq, ngrok AI Gateway, OpenAI, etc.) ---
# Default: Groq free tier. Swap LLM_BASE_URL/LLM_MODEL to use another backend.
LLM_BASE_URL = os.getenv("LLM_BASE_URL", "https://api.groq.com/openai/v1")
LLM_API_KEY = os.getenv("LLM_API_KEY", "")
LLM_MODEL = os.getenv("LLM_MODEL", "llama-3.3-70b-versatile")

# Price per 1M tokens for cost roll-ups (defaults = Groq Llama 3.3 70B paid rate).
# On the free tier your real cost is $0 — these power the projected-cost metric.
LLM_PRICE_IN = float(os.getenv("LLM_PRICE_IN", "0.59"))
LLM_PRICE_OUT = float(os.getenv("LLM_PRICE_OUT", "0.79"))

# --- Monitoring ---
# If set, /api/metrics requires header  X-Metrics-Token: <this>  (empty = open).
METRICS_TOKEN = os.getenv("METRICS_TOKEN", "")

# --- Behaviour ---
MAX_RESULT_ROWS = int(os.getenv("MAX_RESULT_ROWS", "200"))
QUERY_TIMEOUT = int(os.getenv("QUERY_TIMEOUT", "30"))

# Comma-separated list of web origins allowed to call the API (for embedding the
# widget on another site). Use "*" for any origin (dev only). In production set
# this to your site, e.g. "https://www.antern.com,https://antern.com".
ALLOWED_ORIGINS = [
    o.strip() for o in os.getenv("ALLOWED_ORIGINS", "*").split(",") if o.strip()
]


def connection_string() -> str:
    """Build a pyodbc connection string. Password is wrapped in braces so
    special characters (e.g. & in the password) are handled safely."""
    return (
        f"DRIVER={{{DB_DRIVER}}};"
        f"SERVER={DB_SERVER},{DB_PORT};"
        f"DATABASE={DB_NAME};"
        f"UID={DB_USER};"
        f"PWD={{{DB_PASSWORD}}};"
        "Encrypt=yes;"
        "TrustServerCertificate=yes;"
        f"Connection Timeout=15;"
    )