"""Proxy to a basal-serve instance (github.com/rkinas/basal), e.g. basal-serve --model Remek/basal-omni-pico --port 8000 # CPU ok (0.8B) basal-serve --model Remek/basal-omni-pico-GGUF --mode gguf ... # llama.cpp, CPU Uses the experimental `classify` task (basal engine >= 2.1). The label-set description and labelled examples go into the state; label rubrics go into the question. """ from __future__ import annotations import asyncio import httpx from ..spec import LabelSet, Post, Result def build_request(ls: LabelSet, post: Post, max_examples: int = 12, lang: str = "pl") -> dict: state = { "zadanie": "Sklasyfikuj pole 'wpis' (nie przykłady).", "opis_klasyfikacji": ls.description or None, "wpis": post.text, "autor": f"@{post.author.handle}" if post.author and post.author.handle else None, "odpowiedz_na": post.parent_text, "przyklady_klasyfikacji": [{"tekst": e.text, "etykieta": e.label} for e in ls.examples[:max_examples]], } state = {k: v for k, v in state.items() if v not in (None, [], "")} return { "state": state, "lang": lang, "questions": { "label": { "type": "classify", "none": ls.none, "labels": [{"name": l.key, "rubric": l.rubric or l.key} for l in ls.labels], } }, } def parse_answer(answer: dict) -> Result: probs = answer.get("scores") or answer.get("probabilities") or {} label = answer.get("label") or answer.get("choice") or max(probs, key=probs.get) conf = answer.get("confidence", probs.get(label, 0.0)) return Result(label=label, probabilities=probs, confidence=conf, backend="basal") class BasalBackend: name = "basal" def __init__(self, url: str = "http://127.0.0.1:8000", concurrency: int = 4, timeout: float = 30.0): self.url = url.rstrip("/") self.sem = asyncio.Semaphore(concurrency) self.timeout = timeout async def classify(self, ls: LabelSet, posts: list[Post]) -> dict[str, Result]: async with httpx.AsyncClient(timeout=self.timeout) as client: async def one(p: Post): async with self.sem: r = await client.post(f"{self.url}/v1/systemone", json=build_request(ls, p)) r.raise_for_status() return p.id, parse_answer(r.json()["answers"]["label"]) pairs = await asyncio.gather(*(one(p) for p in posts)) return dict(pairs)