serve: STRATA_KEEP_EMPTY_TURNS=1 restores rendering of empty assistant turns (#843)
#886 skips them by default (recommended on; the owner can flip it). Only prompts that carry an empty assistant turn change.
This commit is contained in:
1 parent
ffbc39f4fb
commit
9cd3d00b6d
2 files changed
+16
-1
No files matched your search
+3
-1
@@ -16,6 +16,7 @@ and the formats change often; the engine boundary is token ids in, text deltas o
|
||||
from __future__ import annotations
|
||||
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import uuid
|
||||
from dataclasses import dataclass, field
|
||||
@@ -50,8 +51,9 @@ class ChatTemplate:
|
||||
def render(self, messages: list[dict], tools: list[dict] | None = None, add_generation_prompt: bool = True,
|
||||
**kwargs) -> str:
|
||||
# Do not use completed empty assistant turns as examples for the next reply. Keep the final message.
|
||||
# (#843. STRATA_KEEP_EMPTY_TURNS=1 renders them as before.)
|
||||
messages = [m for i, m in enumerate(messages)
|
||||
if i == len(messages) - 1 or not (isinstance(m, dict) and m.get("role") == "assistant"
|
||||
if i == len(messages) - 1 or os.environ.get("STRATA_KEEP_EMPTY_TURNS") == "1" or not (isinstance(m, dict) and m.get("role") == "assistant"
|
||||
and not _text_of(m.get("content")).strip()
|
||||
and not _has_image(m.get("content")) and not m.get("tool_calls"))]
|
||||
return self.template.render(messages=messages, tools=tools, add_generation_prompt=add_generation_prompt,
|
||||
|
||||
@@ -79,3 +79,16 @@ class EmptyAssistantHistory(unittest.TestCase):
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
|
||||
class EmptyTurnsSwitch(unittest.TestCase):
|
||||
def test_keep_empty_turns_restores_the_old_prompt(self):
|
||||
import os
|
||||
from unittest import mock
|
||||
template = ChatTemplate(Path(__file__).with_name("chat_template.jinja"))
|
||||
msgs = [{"role": "user", "content": "a"}, {"role": "assistant", "content": ""},
|
||||
{"role": "user", "content": "b"}]
|
||||
clean = template.render([msgs[0], msgs[2]])
|
||||
self.assertEqual(template.render(msgs), clean)
|
||||
with mock.patch.dict(os.environ, {"STRATA_KEEP_EMPTY_TURNS": "1"}):
|
||||
self.assertNotEqual(template.render(msgs), clean)
|
||||
Reference in new issue
Block a user