feat(llm_client): add think parameter to classify()

Hybrid-reasoning models (Qwen 3 family, DeepSeek-R1 style) accept a
`think: false` request flag in Ollama 0.7+ to suppress reasoning
emission. Pure-instruct models ignore the flag.

Adds an optional `think` parameter to `LLMProvider.classify` and its
three implementations:
- OllamaProvider: forwards to the wire format only when explicitly set,
  keeping the request body minimal for the default case.
- OpenAICompatProvider, AnthropicProvider: accept the kwarg for
  interface compat but no-op (those providers don't have a per-request
  thinking toggle).

Verified against Ollama 0.23.2 with qwen3:1.7b-q4_K_M:
  think=False: 294ms, no thinking field
  think=True:  523ms (1.8x slower), 516 chars of thinking content
Both return the same answer; only the reasoning overhead differs.

Useful for any caller that wants fast classification without the
<think> token cost — see benchmarks/model_eval for the first user.
This commit is contained in:
Igor Lins e Silva
2026-05-10 12:21:58 -03:00
parent 5f2c554426
commit 608d4e6c0a
+42 -4
View File
@@ -140,7 +140,21 @@ class LLMProvider:
# require explicit user confirmation.
self.api_key_source = api_key_source
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
def classify(
self,
system: str,
user: str,
json_mode: bool = True,
think: Optional[bool] = None,
) -> LLMResponse:
"""Classify a (system, user) pair into a structured response.
``think`` controls reasoning emission for thinking-capable models
(currently honored by ``OllamaProvider`` for Qwen 3 / DeepSeek-R1
style toggles). Other providers ignore it. Pass ``False`` to
disable reasoning when the caller wants a fast classification
without ``<think>`` overhead.
"""
raise NotImplementedError
def check_available(self) -> tuple[bool, str]:
@@ -221,7 +235,13 @@ class OllamaProvider(LLMProvider):
)
return True, "ok"
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
def classify(
self,
system: str,
user: str,
json_mode: bool = True,
think: Optional[bool] = None,
) -> LLMResponse:
body: dict = {
"model": self.model,
"messages": [
@@ -233,6 +253,12 @@ class OllamaProvider(LLMProvider):
}
if json_mode:
body["format"] = "json"
if think is not None:
# Ollama 0.7+ supports `think` for thinking-capable models (Qwen 3
# family, DeepSeek-R1). Pure-instruct models ignore it. We forward
# only when the caller explicitly opts in/out so the wire format
# stays minimal for the common case.
body["think"] = think
data = _http_post_json(f"{self.endpoint}/api/chat", body, headers={}, timeout=self.timeout)
text = (data.get("message") or {}).get("content", "")
if not text:
@@ -300,7 +326,13 @@ class OpenAICompatProvider(LLMProvider):
return False, f"Cannot reach {self.endpoint}: {e}"
return True, "ok"
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
def classify(
self,
system: str,
user: str,
json_mode: bool = True,
think: Optional[bool] = None, # noqa: ARG002 — accepted for interface compat; OpenAI-compat has no thinking toggle
) -> LLMResponse:
body: dict = {
"model": self.model,
"messages": [
@@ -362,7 +394,13 @@ class AnthropicProvider(LLMProvider):
# surface auth errors if the key is invalid.
return True, "ok"
def classify(self, system: str, user: str, json_mode: bool = True) -> LLMResponse:
def classify(
self,
system: str,
user: str,
json_mode: bool = True,
think: Optional[bool] = None, # noqa: ARG002 — accepted for interface compat; Anthropic extended thinking is configured separately
) -> LLMResponse:
if not self.api_key:
raise LLMError("Anthropic provider requires ANTHROPIC_API_KEY env or --llm-api-key")
sys_prompt = system