Merge restoration policy into system message

This commit is contained in:
Mikei386
2026-09-08 08:37:53 +02:00
parent 118e32005e
commit 636e48ce93
2 changed files with 27 additions and 1 deletions
+15
View File
@@ -294,6 +294,21 @@ class RestorationSystemPolicyTests(unittest.TestCase):
self.assertIn("Photo-restoration mode", normalized["instructions"]) self.assertIn("Photo-restoration mode", normalized["instructions"])
self.assertTrue(normalized["instructions"].endswith("Client policy.")) self.assertTrue(normalized["instructions"].endswith("Client policy."))
def test_chat_policy_merges_with_existing_leading_system_message(self) -> None:
request = {"messages": [
{"role": "system", "content": "Global policy."},
{"role": "user", "content": "Mach schöner"},
]}
normalized = _inject_restoration_system_policy(
request, "/v1/chat/completions")
system_messages = [
message for message in normalized["messages"]
if message.get("role") == "system"
]
self.assertEqual(len(system_messages), 1)
self.assertIn("Photo-restoration mode", system_messages[0]["content"])
self.assertTrue(system_messages[0]["content"].endswith("Global policy."))
class RetentionTests(unittest.TestCase): class RetentionTests(unittest.TestCase):
def test_oldest_pairs_are_removed(self) -> None: def test_oldest_pairs_are_removed(self) -> None:
+11
View File
@@ -1396,6 +1396,17 @@ def _inject_restoration_system_policy(data: dict, path: str) -> dict:
if path == "/v1/chat/completions": if path == "/v1/chat/completions":
messages = data.get("messages") messages = data.get("messages")
if isinstance(messages, list): if isinstance(messages, list):
# Qwen's chat template permits exactly one system message and it
# must be the first message. The global router policy may already
# have created that message, so extend it instead of inserting a
# second system message in front of it.
if (messages and isinstance(messages[0], dict)
and messages[0].get("role") == "system"
and isinstance(messages[0].get("content"), str)):
existing = messages[0]["content"]
if policy not in existing:
messages[0]["content"] = f"{policy}\n\n{existing}"
else:
messages.insert(0, {"role": "system", "content": policy}) messages.insert(0, {"role": "system", "content": policy})
elif path == "/v1/responses": elif path == "/v1/responses":
instructions = data.get("instructions") instructions = data.get("instructions")