From c9036201f70a66d5bc86c01b5fd799ca6ac168ab Mon Sep 17 00:00:00 2001 From: Mikei386 <44135113+Mikei386@users.noreply.github.com> Date: Fri, 2 Oct 2026 10:21:26 +0200 Subject: [PATCH] Accept validated thinking template options in chat compatibility layer --- api_compat.py | 9 ++++++++- test_api_compat.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/api_compat.py b/api_compat.py index 2486520..cf5b583 100644 --- a/api_compat.py +++ b/api_compat.py @@ -5,7 +5,7 @@ positive levels to the model template; it handles 'none' as thinking disabled. """ import math -CHAT_FIELDS=frozenset({'model','messages','stream','stream_options','temperature','top_p','top_k','max_tokens','max_completion_tokens','stop','seed','tools','tool_choice','parallel_tool_calls','response_format','repeat_penalty','presence_penalty','frequency_penalty','logprobs','top_logprobs','user','n','reasoning_effort'}) +CHAT_FIELDS=frozenset({'model','messages','stream','stream_options','temperature','top_p','top_k','max_tokens','max_completion_tokens','stop','seed','tools','tool_choice','parallel_tool_calls','response_format','repeat_penalty','presence_penalty','frequency_penalty','logprobs','top_logprobs','user','n','reasoning_effort','chat_template_kwargs'}) LLAMA_EFFORTS=frozenset({'none','low','medium','high','xhigh'}) # The installed llama.cpp server rejects minimal and max. Use nearest supported hints. EFFORT_ALIASES={'minimal':'low','max':'xhigh','ultra':'xhigh'} @@ -17,6 +17,13 @@ def normalize_chat(data): unknown=set(data)-CHAT_FIELDS if unknown:raise CompatibilityError('Nicht unterstützte Chat-Felder: '+', '.join(sorted(unknown))) body=dict(data) + if 'chat_template_kwargs' in body: + kwargs=body['chat_template_kwargs'] + if not isinstance(kwargs,dict) or set(kwargs)-{'enable_thinking','preserve_thinking'}: + raise CompatibilityError('chat_template_kwargs unterstützt enable_thinking und preserve_thinking.') + if any(type(v) is not bool for v in kwargs.values()): + raise CompatibilityError('Thinking-Template-Optionen müssen true oder false sein.') + body['chat_template_kwargs']=dict(kwargs) for key,lo,hi in [('repeat_penalty',0,2),('presence_penalty',-2,2),('frequency_penalty',-2,2)]: if key in body: value=body[key] diff --git a/test_api_compat.py b/test_api_compat.py index 3f15ac2..6e3ece1 100644 --- a/test_api_compat.py +++ b/test_api_compat.py @@ -14,3 +14,12 @@ class CompatibilityTests(unittest.TestCase): for value in [True,1,{},[], '', 'MAX', 'x\r\ny']: with self.assertRaises(CompatibilityError):normalize_chat({'reasoning_effort':value}) with self.assertRaises(CompatibilityError):normalize_chat({'unknown':1}) + + def test_template_thinking_options(self): + original={'chat_template_kwargs':{'enable_thinking':False,'preserve_thinking':True}} + body,_=normalize_chat(original) + self.assertEqual(body,original) + body['chat_template_kwargs']['enable_thinking']=True + self.assertFalse(original['chat_template_kwargs']['enable_thinking']) + for bad in [None,[],{'enable_thinking':'false'},{'template':'custom'},{'preserve_thinking':1}]: + with self.assertRaises(CompatibilityError):normalize_chat({'chat_template_kwargs':bad})