@@ -2856,11 +2856,12 @@ class MTMDChatHandler:
28562856
28572857 def __init__ (
28582858 self ,
2859- clip_model_path : str ,
2859+ mmproj_path : str ,
28602860 verbose : bool = True ,
28612861 use_gpu : bool = True ,
28622862 image_min_tokens : int = - 1 ,
28632863 image_max_tokens : int = - 1 ,
2864+ chat_template_override : Optional [str ] = None ,
28642865 ** kwargs
28652866 ):
28662867
@@ -2872,7 +2873,7 @@ def __init__(
28722873 f"If you are passing model-specific parameters, ensure they are supported by { self .log_prefix } ."
28732874 )
28742875
2875- self .clip_model_path = clip_model_path
2876+ self .mmproj_path = mmproj_path
28762877 self .image_min_tokens = image_min_tokens
28772878 self .image_max_tokens = image_max_tokens
28782879 self .use_gpu = use_gpu
@@ -2883,20 +2884,25 @@ def __init__(
28832884 self .mtmd_ctx : Optional [mtmd_cpp .mtmd_context_p ] = None
28842885 self .extra_template_arguments : dict [str , Any ] = {}
28852886
2886- if not os .path .exists (clip_model_path ):
2887- raise ValueError (f"{ self .log_prefix } (__init__): Clip model path does not exist: { clip_model_path } " )
2887+ if not os .path .exists (mmproj_path ):
2888+ raise ValueError (f"{ self .log_prefix } (__init__): Clip model path does not exist: { mmproj_path } " )
28882889
28892890 # Pre-compile Jinja template
2890- if not hasattr (self , "chat_format" ) or self .chat_format is None :
2891+ if ( not hasattr (self , "chat_format" ) or self .chat_format is None ) and chat_template_override is None :
28912892 self .chat_format = self .CHAT_FORMAT
2893+ elif chat_template_override is not None :
2894+ self .chat_format = chat_template_override
28922895
28932896 self ._chat_format_parser_tags = []
2894- self .chat_template = ImmutableSandboxedEnvironment (
2895- trim_blocks = True ,
2896- lstrip_blocks = True ,
2897- ).from_string (self .chat_format )
2897+ self .change_chat_template (self .chat_format )
28982898
28992899 self ._exit_stack = ExitStack ()
2900+
2901+ def change_chat_template (self , new_template : str ):
2902+ self .chat_template = ImmutableSandboxedEnvironment (
2903+ trim_blocks = True ,
2904+ lstrip_blocks = True
2905+ ).from_string (new_template )
29002906
29012907 def _init_mtmd_context (self , llama_model : llama_core .Llama ):
29022908 """Initialize mtmd context with the llama model."""
@@ -2929,13 +2935,13 @@ def _init_mtmd_context(self, llama_model: llama_core.Llama):
29292935
29302936 # Initialize mtmd context
29312937 self .mtmd_ctx = self ._mtmd_cpp .mtmd_init_from_file (
2932- self .clip_model_path .encode (),
2938+ self .mmproj_path .encode (),
29332939 llama_model .model ,
29342940 self .mctx_params
29352941 )
29362942
29372943 if self .mtmd_ctx is None :
2938- raise ValueError (f"{ self .log_prefix } (_init_mtmd_context): Failed to load mtmd context from: { self .clip_model_path } " )
2944+ raise ValueError (f"{ self .log_prefix } (_init_mtmd_context): Failed to load mtmd context from: { self .mmproj_path } " )
29392945
29402946 # Check if vision is supported
29412947 self .is_support_vision = self ._mtmd_cpp .mtmd_support_vision (self .mtmd_ctx )
@@ -3835,7 +3841,7 @@ def from_pretrained(
38353841 model_path = os .path .join (local_dir , filename )
38363842
38373843 return cls (
3838- clip_model_path = model_path ,
3844+ mmproj_path = model_path ,
38393845 ** kwargs ,
38403846 )
38413847
@@ -3852,21 +3858,20 @@ class GenericMTMDChatHandler(MTMDChatHandler):
38523858
38533859 def __init__ (
38543860 self ,
3855- gguf_metadata : Dict [ str , Any ] ,
3856- clip_model_path : str ,
3861+ chat_format : str ,
3862+ mmproj_path : str ,
38573863 verbose : bool = True ,
38583864 ** kwargs
38593865 ) -> None :
3860- self .model_metadata = gguf_metadata
3861- self .chat_format = self .model_metadata .get ("tokenizer.chat_template" , None )
3866+ self .chat_format = chat_format
38623867
38633868 if verbose :
38643869 print (f"Got chat template from model:\n ```jinja\n { self .chat_format } \n ```" , flush = True )
38653870
38663871 if self .chat_format is None :
38673872 raise ValueError ("Failed to get model chat template automatically." )
38683873
3869- super ().__init__ (clip_model_path = clip_model_path , verbose = verbose , ** kwargs )
3874+ super ().__init__ (mmproj_path = mmproj_path , verbose = verbose , ** kwargs )
38703875
38713876 def __call__ (self , ** kwargs ):
38723877 self ._chat_format_parser_tags = [tag for tag in self .KNOWN_MEDIA_TAGS if tag in self .chat_format ]
0 commit comments