MCPcopy Create free account
hub / github.com/NVIDIA/TensorRT-LLM / apply_chat_template

Function apply_chat_template

tensorrt_llm/inputs/utils.py:601–653  ·  view source on GitHub ↗

Apply chat template to the conversation.

(
    *,
    model_type: str,
    tokenizer: Union[TransformersTokenizer, TokenizerBase],
    processor: ProcessorMixin,
    conversation: list[ConversationMessage],
    add_generation_prompt: bool,
    mm_placeholder_counts: list[dict[str, int]],
    tools: Optional[list[dict[str, Any]]] = None,
    documents: Optional[list[dict[str, str]]] = None,
    chat_template: Optional[str] = None,
    chat_template_kwargs: Optional[dict[str, Any]] = None,
    enable_tokenize: bool = False,
)

Source from the content-addressed store, hash-verified

599
600
601def apply_chat_template(
602 *,
603 model_type: str,
604 tokenizer: Union[TransformersTokenizer, TokenizerBase],
605 processor: ProcessorMixin,
606 conversation: list[ConversationMessage],
607 add_generation_prompt: bool,
608 mm_placeholder_counts: list[dict[str, int]],
609 tools: Optional[list[dict[str, Any]]] = None,
610 documents: Optional[list[dict[str, str]]] = None,
611 chat_template: Optional[str] = None,
612 chat_template_kwargs: Optional[dict[str, Any]] = None,
613 enable_tokenize: bool = False,
614) -> (str | List[str]):
615 """Apply chat template to the conversation."""
616
617 if model_type in HF_CHAT_TEMPLATE_EXCEPTIONS:
618 # special path for models like llava-llama
619 return "".join([conv["content"] for conv in conversation])
620
621 # Handle DeepSeek V32 tokenizer with custom chat template
622 if isinstance(tokenizer, DeepseekV32Tokenizer):
623 prompt = tokenizer.apply_chat_template(
624 messages=conversation,
625 tools=tools,
626 **(chat_template_kwargs or {}),
627 )
628 if enable_tokenize:
629 return tokenizer.encode(prompt)
630 return prompt
631
632 if isinstance(tokenizer, TransformersTokenizer):
633 tokenizer = tokenizer.tokenizer # we need the TokenizerBase for apply_chat_template
634
635 hf_chat_template = resolve_hf_chat_template(tokenizer, processor,
636 chat_template, tools)
637 if hf_chat_template is None:
638 raise ValueError(
639 "No chat template found for the given tokenizer and tools.")
640 if model_type in PLACEHOLDER_EXCEPTIONS:
641 # flattened content do not work for these models, so go back to other formats as needed
642 conversation = handle_placeholder_exceptions(model_type, conversation,
643 mm_placeholder_counts)
644
645 return tokenizer.apply_chat_template(
646 conversation=conversation,
647 tokenize=enable_tokenize,
648 add_generation_prompt=add_generation_prompt,
649 tools=tools,
650 documents=documents,
651 chat_template=hf_chat_template,
652 **(chat_template_kwargs or {}),
653 )
654
655
656def default_multimodal_input_loader(

Callers 4

_create_input_tokensFunction · 0.90
openai_chatMethod · 0.90
openai_mm_encoderMethod · 0.90

Calls 4

resolve_hf_chat_templateFunction · 0.85
apply_chat_templateMethod · 0.45
encodeMethod · 0.45

Tested by

no test coverage detected