serve: skip completed empty assistant turns in chat history
Filter earlier assistant messages with no text, images or tool calls before rendering the prompt. Keep the final message. Co-authored-by: jramirez-uab <238218748+jramirez-uab@users.noreply.github.com> Signed-off-by: Arthur031221 <levi74108520963@gmail.com>
This commit is contained in:
1 parent
8d0bb44620
commit
ffbc39f4fb
2 files changed
+86
No files matched your search
@@ -49,6 +49,11 @@ 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.
|
||||
messages = [m for i, m in enumerate(messages)
|
||||
if i == len(messages) - 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,
|
||||
**kwargs)
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
"""Chat history normalization before rendering the model's prompt."""
|
||||
import copy
|
||||
from pathlib import Path
|
||||
import unittest
|
||||
|
||||
from serve.frontend import ChatTemplate, anthropic_to_messages, openai_to_messages
|
||||
from serve.server import ByteTokenizer, MockEngine, Service
|
||||
|
||||
|
||||
class EmptyAssistantHistory(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.template = ChatTemplate(Path(__file__).with_name("chat_template.jinja"))
|
||||
tok = ByteTokenizer()
|
||||
self.service = Service(MockEngine(tok, "ok"), tok, self.template)
|
||||
|
||||
def test_openai_empty_replies_are_not_rendered(self):
|
||||
for content in (None, "", " \n\t", [], [{"type": "text", "text": ""}]):
|
||||
for preserve in (True, False):
|
||||
with self.subTest(content=content, preserve_thinking=preserve):
|
||||
history = []
|
||||
for _ in range(3):
|
||||
history.extend([{"role": "user", "content": "haz un ls"},
|
||||
{"role": "assistant", "content": content,
|
||||
"reasoning_content": "Let me run ls."}])
|
||||
history.append({"role": "user", "content": "haz un ls"})
|
||||
messages, tools, kwargs = openai_to_messages({"messages": history})
|
||||
kwargs["preserve_thinking"] = preserve
|
||||
clean = [m for m in messages if m["role"] == "user"]
|
||||
self.assertEqual(self.service.encode_prompt(messages, tools, kwargs),
|
||||
self.service.encode_prompt(clean, tools, kwargs))
|
||||
|
||||
def test_anthropic_thinking_only_replies_are_not_rendered(self):
|
||||
messages, tools, kwargs = anthropic_to_messages({"messages": [
|
||||
{"role": "user", "content": "haz un ls"},
|
||||
{"role": "assistant", "content": [{"type": "thinking", "thinking": "Let me run ls."}]},
|
||||
{"role": "user", "content": "haz un ls"},
|
||||
]})
|
||||
self.assertEqual(self.service.encode_prompt(messages, tools, kwargs),
|
||||
self.service.encode_prompt([messages[0], messages[2]], tools, kwargs))
|
||||
|
||||
def test_text_and_tool_calls_are_kept(self):
|
||||
messages, tools, kwargs = openai_to_messages({"messages": [
|
||||
{"role": "user", "content": "haz un ls"},
|
||||
{"role": "assistant", "content": [{"type": "text", "text": "Running ls."}]},
|
||||
{"role": "assistant", "content": None, "tool_calls": [
|
||||
{"type": "function", "function": {"name": "run", "arguments": '{"command":"ls"}'}},
|
||||
]},
|
||||
{"role": "tool", "content": "file.txt"},
|
||||
{"role": "user", "content": "thanks"},
|
||||
]})
|
||||
before = copy.deepcopy(messages)
|
||||
prompt = self.template.render(messages, tools=tools, **kwargs)
|
||||
self.assertIn("Running ls.", prompt)
|
||||
self.assertIn("<function=run>", prompt)
|
||||
self.assertIn("file.txt", prompt)
|
||||
self.assertEqual(messages, before)
|
||||
|
||||
def test_image_only_assistant_turn_is_kept(self):
|
||||
messages, tools, kwargs = openai_to_messages({"messages": [
|
||||
{"role": "user", "content": "describe the image"},
|
||||
{"role": "assistant", "content": [{"type": "image_url", "image_url": "image.png"}]},
|
||||
{"role": "user", "content": "thanks"},
|
||||
]})
|
||||
self.assertIn("<|vision_start|><|image_pad|><|vision_end|>",
|
||||
self.template.render(messages, tools=tools, **kwargs))
|
||||
|
||||
def test_final_empty_assistant_turn_is_kept(self):
|
||||
prompt = self.template.render([{"role": "user", "content": "hi"},
|
||||
{"role": "assistant", "content": ""}], add_generation_prompt=False)
|
||||
self.assertIn("<|im_start|>assistant\n", prompt)
|
||||
|
||||
def test_empty_user_and_tool_turns_are_kept(self):
|
||||
prompt = self.template.render([{"role": "user", "content": ""},
|
||||
{"role": "tool", "content": ""},
|
||||
{"role": "user", "content": "hi"}])
|
||||
self.assertEqual(prompt.count("<|im_start|>user\n"), 3)
|
||||
self.assertIn("<tool_response>\n\n</tool_response>", prompt)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user