Merge restoration policy into system message
This commit is contained in:
@@ -294,6 +294,21 @@ class RestorationSystemPolicyTests(unittest.TestCase):
|
||||
self.assertIn("Photo-restoration mode", normalized["instructions"])
|
||||
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):
|
||||
def test_oldest_pairs_are_removed(self) -> None:
|
||||
|
||||
@@ -1396,6 +1396,17 @@ def _inject_restoration_system_policy(data: dict, path: str) -> dict:
|
||||
if path == "/v1/chat/completions":
|
||||
messages = data.get("messages")
|
||||
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})
|
||||
elif path == "/v1/responses":
|
||||
instructions = data.get("instructions")
|
||||
|
||||
Reference in New Issue
Block a user