Spaces:
Runtime error
Runtime error
Download backend/integrations/llm/resolver.py from Amitkasar/ai-api-testing: direct link, hf CLI and curl.
- Browser
- Download file 1.31 kB
-
https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/integrations/llm/resolver.py
- Command line
-
hf download hf://spaces/Amitkasar/ai-api-testing/backend/integrations/llm/resolver.py
-
curl -L -o resolver.py https://huggingface.co/spaces/Amitkasar/ai-api-testing/resolve/main/backend/integrations/llm/resolver.py
1.31 kB
| 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() | |