vllm.entrypoints.chat_utils ¶
ChatCompletionContentPartParam module-attribute ¶
ChatCompletionContentPartParam: TypeAlias = (
ChatCompletionContentPartParam
| ChatCompletionContentPartAudioParam
| ChatCompletionContentPartInputAudioParam
| ChatCompletionContentPartVideoParam
| ChatCompletionContentPartRefusalParam
| CustomChatCompletionContentPILImageParam
| CustomChatCompletionContentSimpleImageParam
| ChatCompletionContentPartImageEmbedsParam
| ChatCompletionContentPartAudioEmbedsParam
| CustomChatCompletionContentSimpleAudioParam
| CustomChatCompletionContentSimpleVideoParam
| str
| CustomThinkCompletionContentParam
)
ChatCompletionMessageParam module-attribute ¶
ChatCompletionMessageParam: TypeAlias = (
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),
"output_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),
"audio_embeds": lambda part: get("audio_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##>",
}
ModalityStr module-attribute ¶
ModalityStr = Literal[
"image",
"audio",
"video",
"image_embeds",
"audio_embeds",
]
PART_TYPES_TO_SKIP_NONE_CONTENT module-attribute ¶
_AssistantParser module-attribute ¶
_AudioEmbedsParser module-attribute ¶
_AudioEmbedsParser = partial(
cast, ChatCompletionContentPartAudioEmbedsParam
)
_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
)
_PROCESSOR_CHAT_TEMPLATES module-attribute ¶
Used in _try_get_processor_chat_template to avoid calling cached_get_processor again if the processor fails to be loaded.
This is needed because lru_cache does not cache when an exception happens.
_RefusalParser module-attribute ¶
_cached_load_chat_template module-attribute ¶
_cached_load_chat_template = lru_cache(_load_chat_template)
_cached_resolve_chat_template_kwargs module-attribute ¶
_cached_resolve_chat_template_kwargs = lru_cache(
_resolve_chat_template_kwargs
)
AssistantTracker ¶
Bases: Extension
Source code in vllm/entrypoints/chat_utils.py
parse ¶
Source code in vllm/entrypoints/chat_utils.py
AsyncMultiModalContentParser ¶
Bases: BaseMultiModalContentParser
Source code in vllm/entrypoints/chat_utils.py
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 | |
_connector instance-attribute ¶
_connector: MediaConnector = load(
VLLM_MEDIA_CONNECTOR,
media_io_kwargs=media_io_kwargs,
allowed_local_media_path=allowed_local_media_path,
allowed_media_domains=allowed_media_domains,
)
__init__ ¶
__init__(tracker: AsyncMultiModalItemTracker) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_audio_embeds ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_pil ¶
Source code in vllm/entrypoints/chat_utils.py
parse_input_audio ¶
parse_input_audio(
input_audio: InputAudio | None, uuid: str | None = None
) -> None
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() -> MultiModalDataDict | None
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: str | None
)
mm_placeholder_storage ¶
parse_audio abstractmethod ¶
parse_audio_embeds abstractmethod ¶
parse_image abstractmethod ¶
parse_image_embeds abstractmethod ¶
parse_image_pil 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
670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 | |
_items_by_modality instance-attribute ¶
_items_by_modality = defaultdict[str, list[_T | None]](list)
_uuids_by_modality instance-attribute ¶
_uuids_by_modality = defaultdict[str, list[str | None]](
list
)
add ¶
add(
modality: ModalityStr,
item: _T | None,
uuid: str | None = None,
) -> str | None
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() -> MultiModalUUIDDict | None
Source code in vllm/entrypoints/chat_utils.py
create_parser abstractmethod ¶
create_parser() -> BaseMultiModalContentParser
ChatCompletionContentPartAudioEmbedsParam ¶
Bases: TypedDict
Source code in vllm/entrypoints/chat_utils.py
audio_embeds instance-attribute ¶
The audio embeddings. It can be either: - A single base64 string representing a serialized torch tensor. - A dictionary where each value is a base64 string.
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
reasoning instance-attribute ¶
reasoning: str | None
The reasoning content for interleaved thinking.
reasoning_content instance-attribute ¶
reasoning_content: str | None
Deprecated: The reasoning content for interleaved thinking.
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: 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.
reasoning instance-attribute ¶
reasoning: str | None
The reasoning content for interleaved thinking.
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
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 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 | |
_connector instance-attribute ¶
_connector: MediaConnector = load(
VLLM_MEDIA_CONNECTOR,
media_io_kwargs=media_io_kwargs,
allowed_local_media_path=allowed_local_media_path,
allowed_media_domains=allowed_media_domains,
)
__init__ ¶
__init__(tracker: MultiModalItemTracker) -> None
Source code in vllm/entrypoints/chat_utils.py
parse_audio ¶
Source code in vllm/entrypoints/chat_utils.py
parse_audio_embeds ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_embeds ¶
Source code in vllm/entrypoints/chat_utils.py
parse_image_pil ¶
parse_input_audio ¶
parse_input_audio(
input_audio: InputAudio | None, uuid: str | None = None
) -> None
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() -> MultiModalDataDict | None
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
_extract_embeds ¶
Source code in vllm/entrypoints/chat_utils.py
_get_embeds_data ¶
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_hf_base_chat_template_params cached ¶
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 ¶
Source code in vllm/entrypoints/chat_utils.py
_log_chat_template_content_format cached ¶
_log_chat_template_content_format(
chat_template: str | None,
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
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 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 | |
_parse_chat_message_content_part ¶
_parse_chat_message_content_part(
part: ChatCompletionContentPartParam,
mm_parser: BaseMultiModalContentParser,
*,
wrap_dicts: bool,
interleave_strings: bool,
) -> _ContentPart | None
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
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 | |
_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: str | None,
tools: list[dict[str, Any]] | None,
tokenizer: TokenizerLike | None,
*,
model_config: ModelConfig,
) -> _ChatTemplateContentFormat
Source code in vllm/entrypoints/chat_utils.py
_resolve_chat_template_kwargs ¶
_resolve_chat_template_kwargs(chat_template: str)
Source code in vllm/entrypoints/chat_utils.py
_try_extract_ast ¶
_try_extract_ast(chat_template: str) -> Template | None
Source code in vllm/entrypoints/chat_utils.py
_try_get_processor_chat_template ¶
_try_get_processor_chat_template(
tokenizer: PreTrainedTokenizer
| PreTrainedTokenizerFast,
model_config: ModelConfig,
) -> str | None
Source code in vllm/entrypoints/chat_utils.py
apply_hf_chat_template ¶
apply_hf_chat_template(
tokenizer: PreTrainedTokenizer
| PreTrainedTokenizerFast,
conversation: list[ConversationMessage],
chat_template: str | None,
tools: list[dict[str, Any]] | None,
*,
model_config: ModelConfig,
**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: str | None,
tools: list[dict[str, Any]] | None,
**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,
content_format: _ChatTemplateContentFormat,
) -> tuple[
list[ConversationMessage],
MultiModalDataDict | None,
MultiModalUUIDDict | None,
]
Source code in vllm/entrypoints/chat_utils.py
parse_chat_messages_futures ¶
parse_chat_messages_futures(
messages: list[ChatCompletionMessageParam],
model_config: ModelConfig,
content_format: _ChatTemplateContentFormat,
) -> tuple[
list[ConversationMessage],
Awaitable[MultiModalDataDict | None],
MultiModalUUIDDict | None,
]
Source code in vllm/entrypoints/chat_utils.py
resolve_chat_template_content_format ¶
resolve_chat_template_content_format(
chat_template: str | None,
tools: list[dict[str, Any]] | None,
given_format: ChatTemplateContentFormatOption,
tokenizer: TokenizerLike | None,
*,
model_config: ModelConfig,
) -> _ChatTemplateContentFormat
Source code in vllm/entrypoints/chat_utils.py
resolve_chat_template_kwargs ¶
resolve_chat_template_kwargs(
tokenizer: PreTrainedTokenizer
| PreTrainedTokenizerFast,
chat_template: str,
chat_template_kwargs: dict[str, Any],
raise_on_unexpected: bool = True,
) -> dict[str, Any]
Source code in vllm/entrypoints/chat_utils.py
resolve_hf_chat_template ¶
resolve_hf_chat_template(
tokenizer: PreTrainedTokenizer
| PreTrainedTokenizerFast,
chat_template: str | None,
tools: list[dict[str, Any]] | None,
*,
model_config: ModelConfig,
) -> str | None
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.