From 608d4e6c0af12fffcf4fbb7cd89d64879eae9c12 Mon Sep 17 00:00:00 2001 From: Igor Lins e Silva <4753812+igorls@users.noreply.github.com> Date: Sun, 10 May 2026 12:21:58 -0300 Subject: [PATCH] feat(llm_client): add think parameter to classify() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 token cost — see benchmarks/model_eval for the first user. --- mempalace/llm_client.py | 46 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 42 insertions(+), 4 deletions(-) diff --git a/mempalace/llm_client.py b/mempalace/llm_client.py index f6292fa..9e1a92c 100644 --- a/mempalace/llm_client.py +++ b/mempalace/llm_client.py @@ -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 ```` 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