Download core/llm.py from LoveLogicAI/kanbot-backend: direct link, hf CLI and curl.
- Browser
- Download file 1.73 kB
-
https://huggingface.co/spaces/LoveLogicAI/kanbot-backend/resolve/main/core/llm.py
- Command line
-
hf download hf://spaces/LoveLogicAI/kanbot-backend/core/llm.py
-
curl -L -o llm.py https://huggingface.co/spaces/LoveLogicAI/kanbot-backend/resolve/main/core/llm.py
1.73 kB
| """ | |
| LLM client — now a thin shim over the provider harness (core/providers). | |
| The public API is unchanged, so workers.py and everything else keep calling | |
| `llm.chat(...)` / `llm.chat_json(...)` exactly as before. What changed: the | |
| backend is no longer hardcoded to Ollama. The harness routes by KANBOT_PROVIDER | |
| (ollama|openai|anthropic|openrouter|vllm|openai_compat|mock) and by role tier: | |
| tier="fast" -> the fast head model | |
| tier="daily" -> the daily/planner/critic model | |
| heavy=True + a heavy-tagged card -> the heavy model (cloud escalation if set) | |
| Local-first stays the default (KANBOT_PROVIDER unset => ollama on the M4). | |
| Keys are read from the environment by the harness; nothing is persisted here. | |
| """ | |
| from __future__ import annotations | |
| from .providers import ProviderError, get_provider, spec_for | |
| # Re-export so callers can `except llm.ProviderError` without importing providers. | |
| ProviderError = ProviderError | |
| async def chat(system: str, user: str, *, tier: str = "daily", | |
| heavy: bool = False) -> str: | |
| """Return the model's text via the active provider + role-resolved model.""" | |
| provider = get_provider() | |
| spec = spec_for(tier, heavy=heavy) | |
| return await provider.chat(system, user, spec) | |
| async def chat_json(system: str, user: str, *, tier: str = "daily", | |
| heavy: bool = False) -> dict: | |
| """Chat that expects a JSON object back. The harness tolerates code-fenced | |
| JSON and salvages the outermost object; on total garbage it raises | |
| ProviderError (a clean, catchable failure — never a raw traceback).""" | |
| provider = get_provider() | |
| spec = spec_for(tier, heavy=heavy) | |
| return await provider.chat_json(system, user, spec) | |