Accept validated thinking template options in chat compatibility layer

This commit is contained in:
Mikei386 committed 2026-10-02 10:21:26 +02:00
1 parent b451b52a57
commit c9036201f7
2 files changed
+17 -1

No files matched your search

+8 -1
View File
@@ -5,7 +5,7 @@ positive levels to the model template; it handles 'none' as thinking disabled.
""" """
import math 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'}) LLAMA_EFFORTS=frozenset({'none','low','medium','high','xhigh'})
# The installed llama.cpp server rejects minimal and max. Use nearest supported hints. # The installed llama.cpp server rejects minimal and max. Use nearest supported hints.
EFFORT_ALIASES={'minimal':'low','max':'xhigh','ultra':'xhigh'} EFFORT_ALIASES={'minimal':'low','max':'xhigh','ultra':'xhigh'}
@@ -17,6 +17,13 @@ def normalize_chat(data):
unknown=set(data)-CHAT_FIELDS unknown=set(data)-CHAT_FIELDS
if unknown:raise CompatibilityError('Nicht unterstützte Chat-Felder: '+', '.join(sorted(unknown))) if unknown:raise CompatibilityError('Nicht unterstützte Chat-Felder: '+', '.join(sorted(unknown)))
body=dict(data) 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)]: for key,lo,hi in [('repeat_penalty',0,2),('presence_penalty',-2,2),('frequency_penalty',-2,2)]:
if key in body: if key in body:
value=body[key] value=body[key]
+9
View File
@@ -14,3 +14,12 @@ class CompatibilityTests(unittest.TestCase):
for value in [True,1,{},[], '', 'MAX', 'x\r\ny']: for value in [True,1,{},[], '', 'MAX', 'x\r\ny']:
with self.assertRaises(CompatibilityError):normalize_chat({'reasoning_effort':value}) with self.assertRaises(CompatibilityError):normalize_chat({'reasoning_effort':value})
with self.assertRaises(CompatibilityError):normalize_chat({'unknown':1}) 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})