vllm.entrypoints.chat_utils
ChatCompletionContentPartParam
module-attribute
¶
ChatCompletionContentPartParam: TypeAlias = Union[
ChatCompletionContentPartParam,
ChatCompletionContentPartAudioParam,
ChatCompletionContentPartInputAudioParam,
ChatCompletionContentPartVideoParam,
ChatCompletionContentPartRefusalParam,
CustomChatCompletionContentSimpleImageParam,
ChatCompletionContentPartImageEmbedsParam,
CustomChatCompletionContentSimpleAudioParam,
CustomChatCompletionContentSimpleVideoParam,
str,
]
ChatCompletionMessageParam
module-attribute
¶
ChatCompletionMessageParam = Union[
ChatCompletionMessageParam,
CustomChatCompletionMessageParam,
]
ChatTemplateContentFormatOption
module-attribute
¶
ChatTemplateContentFormatOption = Literal[
"auto", "string", "openai"
]
MM_PARSER_MAP
module-attribute
¶
MM_PARSER_MAP: dict[
str,
Callable[
[ChatCompletionContentPartParam], _ContentPart
],
] = {
"text": lambda part: get("text", None),
"image_url": lambda part: get("url", None),
"image_embeds": lambda part: get("image_embeds", None),
"audio_url": lambda part: get("url", None),
"input_audio": lambda part: get("input_audio", None),
"refusal": lambda part: get("refusal", None),
"video_url": lambda part: get("url", None),
}
VALID_MESSAGE_CONTENT_MM_PART_TYPES
module-attribute
¶
VALID_MESSAGE_CONTENT_MM_PART_TYPES = (
"text",
"refusal",
"image_url",
"image_embeds",
"audio_url",
"input_audio",
"video_url",
)
_AssistantParser
module-attribute
¶
_ChatTemplateContentFormat
module-attribute
¶
_ChatTemplateContentFormat = Literal['string', 'openai']
_ImageEmbedsParser
module-attribute
¶
_ImageEmbedsParser = partial(
cast, ChatCompletionContentPartImageEmbedsParam
)
_InputAudioParser
module-attribute
¶
_RefusalParser
module-attribute
¶
_cached_load_chat_template
module-attribute
¶
_cached_load_chat_template = lru_cache(_load_chat_template)
AsyncMultiModalContentParser
¶
Bases: BaseMultiModalContentParser
Source code in vllm/entrypoints/chat_utils.py
_connector
instance-attribute
¶
_connector = MediaConnector(
media_io_kwargs=media_io_kwargs,
allowed_local_media_path=allowed_local_media_path,
)
__init__
¶
__init__(tracker: AsyncMultiModalItemTracker) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds
¶
Source code in vllm/entrypoints/chat_utils.py
parse_input_audio
¶
Source code in vllm/entrypoints/chat_utils.py
AsyncMultiModalItemTracker
¶
Bases: BaseMultiModalItemTracker[Awaitable[object]]
Source code in vllm/entrypoints/chat_utils.py
all_mm_data
async
¶
all_mm_data() -> Optional[MultiModalDataDict]
Source code in vllm/entrypoints/chat_utils.py
create_parser
¶
create_parser() -> BaseMultiModalContentParser
BaseMultiModalContentParser
¶
Bases: ABC
Source code in vllm/entrypoints/chat_utils.py
_placeholder_counts
instance-attribute
¶
_placeholder_counts: dict[str, int] = defaultdict(lambda: 0)
__init__
¶
_add_placeholder
¶
mm_placeholder_counts
¶
parse_image_embeds
abstractmethod
¶
parse_input_audio
abstractmethod
¶
BaseMultiModalItemTracker
¶
Tracks multi-modal items in a given request and ensures that the number of multi-modal items in a given request does not exceed the configured maximum per prompt.
Source code in vllm/entrypoints/chat_utils.py
__init__
¶
__init__(
model_config: ModelConfig, tokenizer: AnyTokenizer
)
add
¶
add(modality: ModalityStr, item: _T) -> Optional[str]
Add a multi-modal item to the current prompt and returns the placeholder string to use, if any.
Source code in vllm/entrypoints/chat_utils.py
create_parser
abstractmethod
¶
create_parser() -> BaseMultiModalContentParser
ChatCompletionContentPartAudioParam
¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
ChatCompletionContentPartImageEmbedsParam
¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
ChatCompletionContentPartVideoParam
¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
ConversationMessage
¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
CustomChatCompletionContentSimpleAudioParam
¶
Bases: TypedDict
A simpler version of the param that only accepts a plain audio_url.
Example: { "audio_url": "https://example.com/audio.mp3" }
Source code in vllm/entrypoints/chat_utils.py
CustomChatCompletionContentSimpleImageParam
¶
Bases: TypedDict
A simpler version of the param that only accepts a plain image_url. This is supported by OpenAI API, although it is not documented.
Example: { "image_url": "https://example.com/image.jpg" }
Source code in vllm/entrypoints/chat_utils.py
CustomChatCompletionContentSimpleVideoParam
¶
Bases: TypedDict
A simpler version of the param that only accepts a plain audio_url.
Example: { "video_url": "https://example.com/video.mp4" }
Source code in vllm/entrypoints/chat_utils.py
CustomChatCompletionMessageParam
¶
Bases: TypedDict
Enables custom roles in the Chat Completion API.
Source code in vllm/entrypoints/chat_utils.py
content
instance-attribute
¶
content: Union[str, list[ChatCompletionContentPartParam]]
The contents of the message.
name
instance-attribute
¶
name: str
An optional name for the participant.
Provides the model information to differentiate between participants of the same role.
tool_call_id
instance-attribute
¶
Tool call that this message is responding to.
MultiModalContentParser
¶
Bases: BaseMultiModalContentParser
Source code in vllm/entrypoints/chat_utils.py
_connector
instance-attribute
¶
_connector = MediaConnector(
media_io_kwargs=media_io_kwargs,
allowed_local_media_path=allowed_local_media_path,
)
__init__
¶
__init__(tracker: MultiModalItemTracker) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds
¶
Source code in vllm/entrypoints/chat_utils.py
parse_input_audio
¶
Source code in vllm/entrypoints/chat_utils.py
MultiModalItemTracker
¶
Bases: BaseMultiModalItemTracker[object]
Source code in vllm/entrypoints/chat_utils.py
all_mm_data
¶
all_mm_data() -> Optional[MultiModalDataDict]
Source code in vllm/entrypoints/chat_utils.py
create_parser
¶
create_parser() -> BaseMultiModalContentParser
_detect_content_format
cached
¶
_detect_content_format(
chat_template: str,
*,
default: _ChatTemplateContentFormat,
) -> _ChatTemplateContentFormat
Source code in vllm/entrypoints/chat_utils.py
_get_full_multimodal_text_prompt
¶
Combine multimodal prompts for a multimodal language model.
Source code in vllm/entrypoints/chat_utils.py
_is_attr_access
¶
Source code in vllm/entrypoints/chat_utils.py
_is_var_access
¶
_is_var_or_elems_access
¶
Source code in vllm/entrypoints/chat_utils.py
_iter_nodes_assign_content_item
¶
Source code in vllm/entrypoints/chat_utils.py
_iter_nodes_assign_messages_item
¶
Source code in vllm/entrypoints/chat_utils.py
_iter_nodes_assign_var_or_elems
¶
_iter_nodes_assign_var_or_elems(root: Node, varname: str)
Source code in vllm/entrypoints/chat_utils.py
_load_chat_template
¶
_load_chat_template(
chat_template: Optional[Union[Path, str]],
*,
is_literal: bool = False,
) -> Optional[str]
Source code in vllm/entrypoints/chat_utils.py
_log_chat_template_content_format
cached
¶
_log_chat_template_content_format(
chat_template: Optional[str],
given_format: ChatTemplateContentFormatOption,
detected_format: ChatTemplateContentFormatOption,
)
Source code in vllm/entrypoints/chat_utils.py
_parse_chat_message_content
¶
_parse_chat_message_content(
message: ChatCompletionMessageParam,
mm_tracker: BaseMultiModalItemTracker,
content_format: _ChatTemplateContentFormat,
) -> list[ConversationMessage]
Source code in vllm/entrypoints/chat_utils.py
_parse_chat_message_content_mm_part
¶
_parse_chat_message_content_mm_part(
part: ChatCompletionContentPartParam,
) -> tuple[str, _ContentPart]
Parses a given multi-modal content part based on its type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
part
|
ChatCompletionContentPartParam
|
A dict containing the content part, with a potential 'type' field. |
required |
Returns:
Type | Description |
---|---|
str
|
A tuple (part_type, content) where: |
_ContentPart
|
|
tuple[str, _ContentPart]
|
|
Raises:
Type | Description |
---|---|
ValueError
|
If the 'type' field is missing and no direct URL is found. |
Source code in vllm/entrypoints/chat_utils.py
_parse_chat_message_content_part
¶
_parse_chat_message_content_part(
part: ChatCompletionContentPartParam,
mm_parser: BaseMultiModalContentParser,
*,
wrap_dicts: bool,
) -> Optional[_ContentPart]
Parses a single part of a conversation. If wrap_dicts is True, structured dictionary pieces for texts and images will be wrapped in dictionaries, i.e., {"type": "text", "text", ...} and {"type": "image"}, respectively. Otherwise multimodal data will be handled by mm_parser, and texts will be returned as strings to be joined with multimodal placeholders.
Source code in vllm/entrypoints/chat_utils.py
_parse_chat_message_content_parts
¶
_parse_chat_message_content_parts(
role: str,
parts: Iterable[ChatCompletionContentPartParam],
mm_tracker: BaseMultiModalItemTracker,
*,
wrap_dicts: bool,
) -> list[ConversationMessage]
Source code in vllm/entrypoints/chat_utils.py
_postprocess_messages
¶
_postprocess_messages(
messages: list[ConversationMessage],
) -> None
Source code in vllm/entrypoints/chat_utils.py
_resolve_chat_template_content_format
¶
_resolve_chat_template_content_format(
chat_template: Optional[str],
tools: Optional[list[dict[str, Any]]],
tokenizer: AnyTokenizer,
*,
model_config: ModelConfig,
) -> _ChatTemplateContentFormat
Source code in vllm/entrypoints/chat_utils.py
_try_extract_ast
¶
Source code in vllm/entrypoints/chat_utils.py
apply_hf_chat_template
¶
apply_hf_chat_template(
tokenizer: Union[
PreTrainedTokenizer, PreTrainedTokenizerFast
],
conversation: list[ConversationMessage],
chat_template: Optional[str],
tools: Optional[list[dict[str, Any]]],
*,
model_config: ModelConfig,
tokenize: bool = False,
trust_remote_code: Optional[bool] = None,
**kwargs: Any,
) -> str
Source code in vllm/entrypoints/chat_utils.py
apply_mistral_chat_template
¶
apply_mistral_chat_template(
tokenizer: MistralTokenizer,
messages: list[ChatCompletionMessageParam],
chat_template: Optional[str],
tools: Optional[list[dict[str, Any]]],
**kwargs: Any,
) -> list[int]
Source code in vllm/entrypoints/chat_utils.py
load_chat_template
¶
parse_chat_messages
¶
parse_chat_messages(
messages: list[ChatCompletionMessageParam],
model_config: ModelConfig,
tokenizer: AnyTokenizer,
content_format: _ChatTemplateContentFormat,
) -> tuple[
list[ConversationMessage], Optional[MultiModalDataDict]
]
Source code in vllm/entrypoints/chat_utils.py
parse_chat_messages_futures
¶
parse_chat_messages_futures(
messages: list[ChatCompletionMessageParam],
model_config: ModelConfig,
tokenizer: AnyTokenizer,
content_format: _ChatTemplateContentFormat,
) -> tuple[
list[ConversationMessage],
Awaitable[Optional[MultiModalDataDict]],
]
Source code in vllm/entrypoints/chat_utils.py
resolve_chat_template_content_format
¶
resolve_chat_template_content_format(
chat_template: Optional[str],
tools: Optional[list[dict[str, Any]]],
given_format: ChatTemplateContentFormatOption,
tokenizer: AnyTokenizer,
*,
model_config: ModelConfig,
trust_remote_code: Optional[bool] = None,
) -> _ChatTemplateContentFormat
Source code in vllm/entrypoints/chat_utils.py
resolve_hf_chat_template
¶
resolve_hf_chat_template(
tokenizer: Union[
PreTrainedTokenizer, PreTrainedTokenizerFast
],
chat_template: Optional[str],
tools: Optional[list[dict[str, Any]]],
*,
model_config: ModelConfig,
trust_remote_code: Optional[bool] = None,
) -> Optional[str]
Source code in vllm/entrypoints/chat_utils.py
resolve_mistral_chat_template
¶
Source code in vllm/entrypoints/chat_utils.py
validate_chat_template
¶
Raises if the provided chat template appears invalid.