File size: 1,312 Bytes
1e24842
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from fastapi import HTTPException
from sqlalchemy.orm import Session

from backend import database
from backend.core.crypto import decrypt_credentials
from backend.integrations.llm import factory
from backend.integrations.llm.base import BaseLLMClient

_cache: dict[str, BaseLLMClient] = {}


def get_llm_for_task(task_name: str, db: Session) -> BaseLLMClient:
    if task_name in _cache:
        return _cache[task_name]

    config = db.query(database.LLMTaskConfig).filter_by(task_name=task_name).first()
    if not config or not config.provider_id:
        raise HTTPException(
            status_code=503,
            detail=f"No AI provider configured for '{task_name}'. Go to Settings → AI Models.",
        )

    provider = db.query(database.LLMProvider).filter_by(provider_id=config.provider_id).first()
    if not provider or not provider.api_key_enc:
        raise HTTPException(
            status_code=400,
            detail=f"Provider '{config.provider_id}' has no API key saved. Go to Settings → AI Models.",
        )

    api_key = decrypt_credentials(provider.api_key_enc)["api_key"]
    client = factory.build_client(config.provider_id, config.model_name, api_key, config.temperature)
    _cache[task_name] = client
    return client


def invalidate_cache() -> None:
    _cache.clear()