vllm.entrypoints.chat_utils ¶
ChatCompletionContentPartParam module-attribute
¶
ChatCompletionContentPartParam: TypeAlias = Union[
ChatCompletionContentPartParam,
ChatCompletionContentPartAudioParam,
ChatCompletionContentPartInputAudioParam,
ChatCompletionContentPartVideoParam,
ChatCompletionContentPartRefusalParam,
CustomChatCompletionContentPILImageParam,
CustomChatCompletionContentSimpleImageParam,
ChatCompletionContentPartImageEmbedsParam,
CustomChatCompletionContentSimpleAudioParam,
CustomChatCompletionContentSimpleVideoParam,
str,
CustomThinkCompletionContentParam,
]
ChatCompletionMessageParam module-attribute
¶
ChatCompletionMessageParam = Union[
ChatCompletionMessageParam,
CustomChatCompletionMessageParam,
Message,
]
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),
"thinking": lambda part: get("thinking", None),
"input_text": lambda part: get("text", None),
"input_image": lambda part: get("image_url", None),
"image_url": lambda part: get("url", None),
"image_embeds": lambda part: get("image_embeds", None),
"image_pil": lambda part: get("image_pil", 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),
}
MODALITY_PLACEHOLDERS_MAP module-attribute
¶
MODALITY_PLACEHOLDERS_MAP = {
"image": "<##IMAGE##>",
"audio": "<##AUDIO##>",
"video": "<##VIDEO##>",
}
PART_TYPES_TO_SKIP_NONE_CONTENT module-attribute
¶
_AssistantParser module-attribute
¶
_ChatTemplateContentFormat module-attribute
¶
_ChatTemplateContentFormat = Literal['string', 'openai']
_ContentPart module-attribute
¶
_ImageEmbedsParser module-attribute
¶
_ImageEmbedsParser = partial(
cast, ChatCompletionContentPartImageEmbedsParam
)
_InputAudioParser module-attribute
¶
_PILImageParser module-attribute
¶
_PILImageParser = partial(
cast, CustomChatCompletionContentPILImageParam
)
_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
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 |
|
_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_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds ¶
parse_image_embeds(
image_embeds: Union[str, dict[str, str], None],
uuid: Optional[str] = None,
) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_image_pil ¶
Source code in vllm/entrypoints/chat_utils.py
parse_input_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_video ¶
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
__init__ ¶
Source code in vllm/entrypoints/chat_utils.py
_add_placeholder ¶
_add_placeholder(
modality: ModalityStr, placeholder: Optional[str]
)
mm_placeholder_storage ¶
parse_audio abstractmethod
¶
parse_image abstractmethod
¶
parse_image_embeds abstractmethod
¶
parse_image_pil 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
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 |
|
_items_by_modality instance-attribute
¶
_uuids_by_modality instance-attribute
¶
__init__ ¶
__init__(
model_config: ModelConfig, tokenizer: AnyTokenizer
)
Source code in vllm/entrypoints/chat_utils.py
add ¶
Add a multi-modal item to the current prompt and returns the placeholder string to use, if any.
An optional uuid can be added which serves as a unique identifier of the media.
Source code in vllm/entrypoints/chat_utils.py
all_mm_uuids ¶
all_mm_uuids() -> Optional[MultiModalUUIDDict]
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
image_embeds instance-attribute
¶
The image embeddings. It can be either: - A single base64 string. - A dictionary where each value is a base64 string.
ChatCompletionContentPartVideoParam ¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
ConversationMessage ¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
CustomChatCompletionContentPILImageParam ¶
Bases: TypedDict
A simpler version of the param that only accepts a PIL image.
Example: { "image_pil": ImageAsset('cherry_blossom').pil_image }
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.
CustomThinkCompletionContentParam ¶
Bases: TypedDict
A Think Completion Content Param that accepts a plain text and a boolean.
Example: { "thinking": "I am thinking about the answer", "closed": True, "type": "thinking" }
Source code in vllm/entrypoints/chat_utils.py
MultiModalContentParser ¶
Bases: BaseMultiModalContentParser
Source code in vllm/entrypoints/chat_utils.py
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 |
|
_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_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds ¶
parse_image_embeds(
image_embeds: Union[str, dict[str, str], None],
uuid: Optional[str] = None,
) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_image_pil ¶
parse_input_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_video ¶
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
PILImage ¶
Bases: BaseModel
A PIL.Image.Image object.
Source code in vllm/entrypoints/chat_utils.py
_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 ¶
_get_full_multimodal_text_prompt(
placeholder_storage: dict[str, list],
texts: list[str],
interleave_strings: bool,
) -> str
Combine multimodal prompts for a multimodal language model.
Source code in vllm/entrypoints/chat_utils.py
_get_interleaved_text_prompt ¶
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,
interleave_strings: bool,
) -> 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
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 |
|
_parse_chat_message_content_part ¶
_parse_chat_message_content_part(
part: ChatCompletionContentPartParam,
mm_parser: BaseMultiModalContentParser,
*,
wrap_dicts: bool,
interleave_strings: 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
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 |
|
_parse_chat_message_content_parts ¶
_parse_chat_message_content_parts(
role: str,
parts: Iterable[ChatCompletionContentPartParam],
mm_tracker: BaseMultiModalItemTracker,
*,
wrap_dicts: bool,
interleave_strings: 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,
**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
get_history_tool_calls_cnt ¶
get_history_tool_calls_cnt(
conversation: list[ConversationMessage],
)
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],
Optional[MultiModalUUIDDict],
]
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]],
Optional[MultiModalUUIDDict],
]
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,
) -> _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,
) -> 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.