Skip to content

Commit ba63f7d

Browse files
committed
refactor: make MTMD chat template handling generic
1 parent e4d5b5e commit ba63f7d

1 file changed

Lines changed: 37 additions & 43 deletions

File tree

llama_cpp/llama_chat_format.py

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -3266,15 +3266,49 @@ def from_pretrained(
32663266

32673267

32683268
class MTMDChatHandler(Llava15ChatHandler):
3269+
DEFAULT_SYSTEM_MESSAGE = None
3270+
32693271
def _get_chat_template(self, llama_model: llama.Llama) -> str:
3270-
return self.CHAT_FORMAT
3272+
chat_template = llama_model.metadata.get("tokenizer.chat_template")
3273+
if not isinstance(chat_template, str) or chat_template == "":
3274+
raise ValueError(
3275+
f"{self.__class__.__name__} requires tokenizer.chat_template metadata"
3276+
)
3277+
return chat_template
32713278

32723279
def _get_template_messages(
32733280
self,
32743281
messages: List[llama_types.ChatCompletionRequestMessage],
32753282
media_marker: str,
32763283
) -> List[Any]:
3277-
return messages
3284+
return [
3285+
self._convert_message_for_template(message, media_marker)
3286+
for message in messages
3287+
]
3288+
3289+
@classmethod
3290+
def _convert_message_for_template(
3291+
cls,
3292+
message: llama_types.ChatCompletionRequestMessage,
3293+
media_marker: str,
3294+
) -> Dict[str, Any]:
3295+
message_dict = dict(message)
3296+
content = message_dict.get("content")
3297+
if isinstance(content, list):
3298+
message_dict["content"] = [
3299+
cls._convert_content_part_for_template(part, media_marker)
3300+
for part in content
3301+
]
3302+
return message_dict
3303+
3304+
@staticmethod
3305+
def _convert_content_part_for_template(
3306+
part: Any,
3307+
media_marker: str,
3308+
) -> Any:
3309+
if isinstance(part, dict) and part.get("type") == "image_url":
3310+
return {"type": "text", "text": media_marker}
3311+
return part
32783312

32793313
@staticmethod
32803314
def _decode_token_piece(piece: Any) -> str:
@@ -3558,47 +3592,7 @@ def raise_exception(message: str):
35583592

35593593

35603594
class Gemma4ChatHandler(MTMDChatHandler):
3561-
DEFAULT_SYSTEM_MESSAGE = None
3562-
3563-
def _get_chat_template(self, llama_model: llama.Llama) -> str:
3564-
chat_template = llama_model.metadata.get("tokenizer.chat_template")
3565-
if not isinstance(chat_template, str) or chat_template == "":
3566-
raise ValueError("Gemma4ChatHandler requires tokenizer.chat_template metadata")
3567-
return chat_template
3568-
3569-
def _get_template_messages(
3570-
self,
3571-
messages: List[llama_types.ChatCompletionRequestMessage],
3572-
media_marker: str,
3573-
) -> List[Any]:
3574-
return [
3575-
self._convert_message_for_template(message, media_marker)
3576-
for message in messages
3577-
]
3578-
3579-
@classmethod
3580-
def _convert_message_for_template(
3581-
cls,
3582-
message: llama_types.ChatCompletionRequestMessage,
3583-
media_marker: str,
3584-
) -> Dict[str, Any]:
3585-
message_dict = dict(message)
3586-
content = message_dict.get("content")
3587-
if isinstance(content, list):
3588-
message_dict["content"] = [
3589-
cls._convert_content_part_for_template(part, media_marker)
3590-
for part in content
3591-
]
3592-
return message_dict
3593-
3594-
@staticmethod
3595-
def _convert_content_part_for_template(
3596-
part: Any,
3597-
media_marker: str,
3598-
) -> Any:
3599-
if isinstance(part, dict) and part.get("type") == "image_url":
3600-
return {"type": "text", "text": media_marker}
3601-
return part
3595+
pass
36023596

36033597

36043598
class ObsidianChatHandler(Llava15ChatHandler):

0 commit comments

Comments
 (0)