API Reference
This section provides the complete API reference for ollama-classifier.
LLMClassifier
The single, unified, backend-agnostic classifier. Accepts any
LLMBackend instance and exposes
two scoring methods: generate() (adaptive constrained generation) and
classify() (exact multi-call completion scoring).
- class ollama_classifier.classifier.LLMClassifier(backend: LLMBackend, *, max_workers: int = 4)[source]
Bases:
objectBackend-agnostic text classifier with two confidence scoring methods.
- generate()[source]
Adaptive constrained generation with divergence-aware confidence. Budget-controlled via
max_calls.
- Parameters:
backend – An
LLMBackendinstance (e.g.,OllamaBackend,VLLMBackend,SGLangBackend, orLlamaCppBackend).max_workers – Thread pool size for sync batch operations.
- __init__(backend: LLMBackend, *, max_workers: int = 4)[source]
- generate(text: str, choices: List[str] | Dict[str, str], system_prompt: str | None = None, *, max_calls: int | None = 1) ClassificationResult[source]
Adaptive constrained classification with divergence-aware confidence.
Makes 1 to
max_callsconstrained API calls. Each call walks a prefix trie of label tokens. After each call, labels are scored up to their divergence point from the winning path. Unresolved clusters trigger supplementary calls (recursive cluster resolution).With
max_calls=1: single call, partial scoring (fast, approximate). Withmax_calls=None: resolves everything recursively (exact).- Parameters:
text – Text to classify.
choices – Labels as list or
{label: description}dict.system_prompt – Optional custom system prompt.
max_calls – Maximum number of API calls.
None= unlimited.
- Returns:
ClassificationResultwithmethod="adaptive_generate".
- classify(text: str, choices: List[str] | Dict[str, str], system_prompt: str | None = None) ClassificationResult[source]
Multi-call classification with geometric-mean completion scoring.
For each label, scores the label as a completion of the prompt and extracts per-token logprobs WITHOUT generation. Applies geometric-mean normalization to eliminate token-count bias. This is the gold-standard confidence method.
Makes N API calls for N choices (parallelizable via async).
- Parameters:
text – Text to classify.
choices – Labels as list or
{label: description}dict.system_prompt – Optional custom system prompt.
- Returns:
ClassificationResultwithmethod="multi_call",approximate=False.
- batch_generate(texts: List[str], choices: List[str] | Dict[str, str], system_prompt: str | None = None, *, max_calls: int | None = 1) List[ClassificationResult][source]
Batch adaptive generation (parallelized via
ThreadPoolExecutor).
- batch_classify(texts: List[str], choices: List[str] | Dict[str, str], system_prompt: str | None = None) List[ClassificationResult][source]
Batch multi-call classification (parallelized via
ThreadPoolExecutor).
- async agenerate(text: str, choices: List[str] | Dict[str, str], system_prompt: str | None = None, *, max_calls: int | None = 1) ClassificationResult[source]
Async adaptive generation with divergence-aware confidence.
- async aclassify(text: str, choices: List[str] | Dict[str, str], system_prompt: str | None = None) ClassificationResult[source]
Async multi-call classification (labels scored concurrently).
ClassificationResult
A Pydantic model returned by both generate() and classify().
- class ollama_classifier.types.ClassificationResult(*, prediction: str, confidence: float, probabilities: ~typing.Dict[str, float], method: str = 'multi_call', approximate: bool = False, coverage: ~typing.Dict[str, float] = <factory>, n_calls: int = 1, raw_response: ~typing.Dict[str, ~typing.Any] = <factory>)[source]
Bases:
BaseModelResult of a classification operation.
- approximate
True if any label has partial coverage (unresolved tokens). Only relevant for
adaptive_generate; alwaysFalseformulti_call.- Type:
- coverage
Per-label fraction of tokens scored (0.0 to 1.0).
1.0= fully resolved. Only present foradaptive_generate.
- model_config = {}
Configuration for the model, should be a dictionary conforming to [ConfigDict][pydantic.config.ConfigDict].
ChoicesType
Backends
Inference engine backends for ollama-classifier.
Each backend implements the LLMBackend protocol and communicates with
its respective inference engine. OllamaBackend uses the native Ollama SDK;
VLLMBackend, SGLangBackend, and LlamaCppBackend communicate via the
OpenAI-compatible API.
Each backend translates the high-level constrain_labels parameter to its
native constraint mechanism:
Ollama: JSON Schema enum via
formatvLLM:
guided_choiceSGLang:
regexllama.cpp: GBNF
grammar
- class ollama_classifier.backends.LLMBackend(model: str, base_url: str = '', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
ABCAbstract base class for LLM inference backends.
All backends communicate with their respective inference engine via HTTP (OpenAI-compatible API). Each backend translates
constrain_labelsto its native constraint mechanism and implementsscore()for completion scoring andtokenize()for context-dependent tokenization.- __init__(model: str, base_url: str = '', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the backend.
- Parameters:
model – The model identifier to use.
base_url – Base URL of the inference server.
api_key – Optional API key. Defaults to
"not-needed".timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request body.
- abstract property supports_bare_label_constraint: bool
True if
chat()generates bare label text (no JSON wrapper).vLLM/SGLang/llama.cpp:True.Ollama:False(uses JSON enum wrapper).
- abstractmethod chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Perform a synchronous constrained chat completion.
Each backend translates
constrain_labelsto its native constraint mechanism (guided_choice,regex, JSON enum, or GBNF grammar).- Parameters:
messages – List of chat messages.
temperature – Sampling temperature.
constrain_labels – Optional list of valid label strings. If provided, output is constrained to exactly one of these.
logprobs – Whether to return log probabilities.
top_logprobs – Number of top log probabilities per token.
- Returns:
ChatResponsewith content and optional logprobs.
- abstractmethod score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion by computing per-token logprobs of the completion text given the message context.
No generation occurs — the completion is provided and the backend returns the model’s logprob for each token of the completion.
- Parameters:
messages – The chat messages forming the context.
completion – The completion text to score.
- Returns:
ScoringResponsewith per-token logprobs.
- abstractmethod tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text.
If
contextis provided, tokenizescontext + textand returns only the tokens corresponding totext(context-dependent tokenization). This is critical for backends that wrap labels in JSON (Ollama), where the tokenization of a label inside JSON differs from standalone tokenization.- Parameters:
text – The text to tokenize.
context – Optional prefix to include for context-dependent tokenization.
- Returns:
List of
Tokenobjects.
- abstractmethod async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion.
- abstractmethod async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring.
- class ollama_classifier.backends.ChatMessage(role: str, content: str)[source]
Bases:
objectA single chat message.
- class ollama_classifier.backends.ChatResponse(content: str, label: str = '', logprobs: List[TokenLogprob] | None = None, raw: Dict[str, ~typing.Any]=<factory>)[source]
Bases:
objectResponse from a constrained generation call.
- logprobs: List[TokenLogprob] | None = None
- class ollama_classifier.backends.ScoringResponse(completion: str, logprobs: List[TokenLogprob] = <factory>, raw: Dict[str, ~typing.Any]=<factory>)[source]
Bases:
objectResponse from a completion-scoring call (for
classify()).Contains per-token logprobs for the COMPLETION text only, not the prompt.
- logprobs: List[TokenLogprob]
- class ollama_classifier.backends.TokenLogprob(token: str, token_id: int = -1, logprob: float = 0.0, top_logprobs: dict[str, float]=<factory>)[source]
Bases:
objectA single token with its log probability and alternatives.
top_logprobsfollows the OpenAI format: a list of dicts, each with"token","logprob", and optionally"bytes"keys.
- class ollama_classifier.backends.OllamaBackend(model: str, *, host: str | None = None, sync_client: Any = None, async_client: Any = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for the Ollama runtime (≥v0.12) via the official Python SDK.
Ollama provides a local LLM runtime with an OpenAI-compatible API and a native API. This backend uses the native API via the
ollamaPython SDK. JSON schema constraints and logprobs are supported as of v0.12.Note
Ollama’s constraint mechanism is JSON Schema enum, which wraps the label in JSON structural tokens. The
tokenize()method supports context-dependent tokenization so the label is tokenized within the JSON prefix it appears in, ensuring the trie matches the response tokens.- __init__(model: str, *, host: str | None = None, sync_client: Any = None, async_client: Any = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the Ollama backend.
- Parameters:
model – Model name (e.g.,
"llama3.2").host – Ollama server URL. Defaults to
http://localhost:11434.sync_client – Pre-initialized
ollama.Client(sync). Created lazily if None.async_client – Pre-initialized
ollama.AsyncClient. Created lazily if None.timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request options.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Perform a synchronous constrained chat completion via Ollama.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using Ollama’s generate endpoint with suffix.
Uses
client.generate(prompt=..., suffix=completion)to compute the per-token logprobs of the completion given the prompt context. No generation occurs (num_predict=0).
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using Ollama’s tokenize API.
If
contextis provided, tokenizescontext + textand returns only the tokens corresponding totext. This ensures context-dependent tokenization matches the actual response tokens (critical for JSON-wrapped labels).
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via Ollama.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via Ollama.
- class ollama_classifier.backends.VLLMBackend(model: str, base_url: str = 'http://localhost:8000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for vLLM inference server.
vLLM provides a high-throughput serving engine with an OpenAI-compatible API endpoint. It supports guided decoding (
guided_choice) and logprob return out of the box. Logprobs are pre-mask (raw model logits before guided decoding masking).- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via vLLM.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using vLLM’s completions endpoint.
Uses
/v1/completionswithprompt_logprobsto compute the per-token logprobs of the completion given the prompt context.
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using vLLM’s tokenize endpoint.
vLLM provides a
/tokenizeendpoint (non-standard but supported in recent versions).
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via vLLM.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via vLLM.
- class ollama_classifier.backends.SGLangBackend(model: str, base_url: str = 'http://localhost:30000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for SGLang inference server.
SGLang is a fast serving system for large language models with an OpenAI-compatible API. It supports regex-guided decoding and logprobs. Logprobs are pre-mask (raw model logits before regex masking).
- property supports_bare_label_constraint: bool
True — SGLang regex constraint generates bare label text.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via SGLang.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using SGLang’s completions endpoint.
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using SGLang’s tokenize endpoint.
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via SGLang.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via SGLang.
- class ollama_classifier.backends.LlamaCppBackend(model: str, base_url: str = 'http://localhost:8080/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for llama.cpp server (
llama-server).llama.cpp provides a lightweight inference server with an OpenAI-compatible API. GBNF grammar constraints and logprobs are supported when compiled with the appropriate flags. Logprobs are pre-mask (model’s raw distribution before grammar masking).
Note
response_formatwith JSON schema on/v1/chat/completionsis buggy in llama.cpp (GitHub issues #11988, #11847). This backend uses the non-standardgrammarfield with GBNF instead, which works reliably for bare label generation.- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via llama.cpp server.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using llama.cpp’s completions endpoint with suffix.
Uses
/v1/completionswithsuffixto compute the per-token logprobs of the completion given the prompt context (fill-in-the-middle style scoring).
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using llama.cpp’s tokenize endpoint.
llama-server provides a
/tokenizeendpoint.
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via llama.cpp server.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via llama.cpp server.
- class ollama_classifier.backends.base.LLMBackend(model: str, base_url: str = '', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
ABCAbstract base class for LLM inference backends.
All backends communicate with their respective inference engine via HTTP (OpenAI-compatible API). Each backend translates
constrain_labelsto its native constraint mechanism and implementsscore()for completion scoring andtokenize()for context-dependent tokenization.- __init__(model: str, base_url: str = '', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the backend.
- Parameters:
model – The model identifier to use.
base_url – Base URL of the inference server.
api_key – Optional API key. Defaults to
"not-needed".timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request body.
- abstract property supports_bare_label_constraint: bool
True if
chat()generates bare label text (no JSON wrapper).vLLM/SGLang/llama.cpp:True.Ollama:False(uses JSON enum wrapper).
- abstractmethod chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Perform a synchronous constrained chat completion.
Each backend translates
constrain_labelsto its native constraint mechanism (guided_choice,regex, JSON enum, or GBNF grammar).- Parameters:
messages – List of chat messages.
temperature – Sampling temperature.
constrain_labels – Optional list of valid label strings. If provided, output is constrained to exactly one of these.
logprobs – Whether to return log probabilities.
top_logprobs – Number of top log probabilities per token.
- Returns:
ChatResponsewith content and optional logprobs.
- abstractmethod score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion by computing per-token logprobs of the completion text given the message context.
No generation occurs — the completion is provided and the backend returns the model’s logprob for each token of the completion.
- Parameters:
messages – The chat messages forming the context.
completion – The completion text to score.
- Returns:
ScoringResponsewith per-token logprobs.
- abstractmethod tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text.
If
contextis provided, tokenizescontext + textand returns only the tokens corresponding totext(context-dependent tokenization). This is critical for backends that wrap labels in JSON (Ollama), where the tokenization of a label inside JSON differs from standalone tokenization.- Parameters:
text – The text to tokenize.
context – Optional prefix to include for context-dependent tokenization.
- Returns:
List of
Tokenobjects.
- abstractmethod async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion.
- abstractmethod async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring.
- class ollama_classifier.backends.ollama.OllamaBackend(model: str, *, host: str | None = None, sync_client: Any = None, async_client: Any = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for the Ollama runtime (≥v0.12) via the official Python SDK.
Ollama provides a local LLM runtime with an OpenAI-compatible API and a native API. This backend uses the native API via the
ollamaPython SDK. JSON schema constraints and logprobs are supported as of v0.12.Note
Ollama’s constraint mechanism is JSON Schema enum, which wraps the label in JSON structural tokens. The
tokenize()method supports context-dependent tokenization so the label is tokenized within the JSON prefix it appears in, ensuring the trie matches the response tokens.- __init__(model: str, *, host: str | None = None, sync_client: Any = None, async_client: Any = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the Ollama backend.
- Parameters:
model – Model name (e.g.,
"llama3.2").host – Ollama server URL. Defaults to
http://localhost:11434.sync_client – Pre-initialized
ollama.Client(sync). Created lazily if None.async_client – Pre-initialized
ollama.AsyncClient. Created lazily if None.timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request options.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Perform a synchronous constrained chat completion via Ollama.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using Ollama’s generate endpoint with suffix.
Uses
client.generate(prompt=..., suffix=completion)to compute the per-token logprobs of the completion given the prompt context. No generation occurs (num_predict=0).
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using Ollama’s tokenize API.
If
contextis provided, tokenizescontext + textand returns only the tokens corresponding totext. This ensures context-dependent tokenization matches the actual response tokens (critical for JSON-wrapped labels).
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via Ollama.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via Ollama.
- class ollama_classifier.backends.vllm.VLLMBackend(model: str, base_url: str = 'http://localhost:8000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for vLLM inference server.
vLLM provides a high-throughput serving engine with an OpenAI-compatible API endpoint. It supports guided decoding (
guided_choice) and logprob return out of the box. Logprobs are pre-mask (raw model logits before guided decoding masking).- __init__(model: str, base_url: str = 'http://localhost:8000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the backend.
- Parameters:
model – The model identifier to use.
base_url – Base URL of the inference server.
api_key – Optional API key. Defaults to
"not-needed".timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request body.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via vLLM.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using vLLM’s completions endpoint.
Uses
/v1/completionswithprompt_logprobsto compute the per-token logprobs of the completion given the prompt context.
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using vLLM’s tokenize endpoint.
vLLM provides a
/tokenizeendpoint (non-standard but supported in recent versions).
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via vLLM.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via vLLM.
- class ollama_classifier.backends.sglang.SGLangBackend(model: str, base_url: str = 'http://localhost:30000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for SGLang inference server.
SGLang is a fast serving system for large language models with an OpenAI-compatible API. It supports regex-guided decoding and logprobs. Logprobs are pre-mask (raw model logits before regex masking).
- __init__(model: str, base_url: str = 'http://localhost:30000/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the backend.
- Parameters:
model – The model identifier to use.
base_url – Base URL of the inference server.
api_key – Optional API key. Defaults to
"not-needed".timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request body.
- property supports_bare_label_constraint: bool
True — SGLang regex constraint generates bare label text.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via SGLang.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using SGLang’s completions endpoint.
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using SGLang’s tokenize endpoint.
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via SGLang.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via SGLang.
- class ollama_classifier.backends.llamacpp.LlamaCppBackend(model: str, base_url: str = 'http://localhost:8080/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Bases:
LLMBackendBackend for llama.cpp server (
llama-server).llama.cpp provides a lightweight inference server with an OpenAI-compatible API. GBNF grammar constraints and logprobs are supported when compiled with the appropriate flags. Logprobs are pre-mask (model’s raw distribution before grammar masking).
Note
response_formatwith JSON schema on/v1/chat/completionsis buggy in llama.cpp (GitHub issues #11988, #11847). This backend uses the non-standardgrammarfield with GBNF instead, which works reliably for bare label generation.- __init__(model: str, base_url: str = 'http://localhost:8080/v1', *, api_key: str | None = None, timeout: float = 120.0, max_tokens: int = 256, extra_body: Dict[str, Any] | None = None)[source]
Initialize the backend.
- Parameters:
model – The model identifier to use.
base_url – Base URL of the inference server.
api_key – Optional API key. Defaults to
"not-needed".timeout – Request timeout in seconds.
max_tokens – Maximum tokens to generate.
extra_body – Extra parameters merged into every request body.
- chat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Synchronous constrained chat completion via llama.cpp server.
- score(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Score a completion using llama.cpp’s completions endpoint with suffix.
Uses
/v1/completionswithsuffixto compute the per-token logprobs of the completion given the prompt context (fill-in-the-middle style scoring).
- tokenize(text: str, *, context: str | None = None) List[Token][source]
Tokenize text using llama.cpp’s tokenize endpoint.
llama-server provides a
/tokenizeendpoint.
- async achat(messages: List[ChatMessage], *, temperature: float = 0.0, constrain_labels: List[str] | None = None, logprobs: bool = False, top_logprobs: int = 5) ChatResponse[source]
Async constrained chat completion via llama.cpp server.
- async ascore(messages: List[ChatMessage], completion: str) ScoringResponse[source]
Async completion scoring via llama.cpp server.
Method Summary
LLMClassifier exposes the following methods. All return a
ClassificationResult.
Choosing a Scoring Method
Use Case |
Recommended Method |
|---|---|
Speed is critical, approximate OK |
|
Adaptive accuracy within a budget |
|
Fully resolved, unlimited calls |
|
Gold-standard confidence (exact) |
|
Batch processing |
|
Concurrent processing |
Async variants ( |