Skip to content

Commit 8cff18f

Browse files
committed
Minor enhancement
Signed-off-by: Pengyun Lin <[email protected]>
1 parent 20d9050 commit 8cff18f

File tree

3 files changed

+10
-26
lines changed

3 files changed

+10
-26
lines changed

tensorrt_llm/commands/serve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,8 @@ def convert(self, value: Any, param: Optional["click.Parameter"],
371371
@click.option("--chat_template",
372372
type=str,
373373
default=None,
374-
help="[Experimental] Specify the chat template.")
374+
help="[Experimental] Specify a custom chat template. "
375+
"Can be a file path or one-liner template string")
375376
def serve(
376377
model: str, tokenizer: Optional[str], host: str, port: int,
377378
log_level: str, backend: str, max_beam_width: int, max_batch_size: int,

tensorrt_llm/serve/chat_utils.py

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,8 @@ def make_tool_call_id(id_type: str = "random", func_name=None, idx=None):
259259

260260
# Adapted from
261261
# https://github.com/vllm-project/vllm/blob/44b5ce956d3cf28841615a58c1c0873af87bcfe2/vllm/entrypoints/chat_utils.py
262-
def _load_chat_template(
262+
@lru_cache
263+
def load_chat_template(
263264
chat_template: Path | str | None,
264265
*,
265266
is_literal: bool = False,
@@ -290,15 +291,4 @@ def _load_chat_template(
290291

291292
# If opening a file fails, set chat template to be args to
292293
# ensure we decode so our escape are interpreted correctly
293-
return _load_chat_template(chat_template, is_literal=True)
294-
295-
296-
_cached_load_chat_template = lru_cache(_load_chat_template)
297-
298-
299-
def load_chat_template(
300-
chat_template: Path | str | None,
301-
*,
302-
is_literal: bool = False,
303-
) -> str | None:
304-
return _cached_load_chat_template(chat_template, is_literal=is_literal)
294+
return load_chat_template(chat_template, is_literal=True)

tests/unittest/llmapi/apps/test_chat_utils.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import os
2-
import tempfile
31
from unittest.mock import MagicMock
42

53
import pytest
@@ -187,17 +185,12 @@ def test_tool_message_without_tool_call_id(self, mock_mm_data_tracker):
187185

188186

189187
@pytest.fixture
190-
def chat_template_path():
188+
def chat_template_path(tmp_path):
191189
"""Return the path to the chat template."""
192-
temp_dir = tempfile.gettempdir()
193-
temp_file_path = os.path.join(temp_dir, "chat_template.jinja")
194-
try:
195-
with open(temp_file_path, "w") as f:
196-
f.write(TEMPLATE_CHATML)
197-
yield temp_file_path
198-
finally:
199-
if os.path.exists(temp_file_path):
200-
os.remove(temp_file_path)
190+
temp_file_path = tmp_path / "chat_template.jinja"
191+
with open(temp_file_path, "w") as f:
192+
f.write(TEMPLATE_CHATML)
193+
return temp_file_path
201194

202195

203196
class TestLoadChatTemplate:

0 commit comments

Comments
 (0)