@@ -42,9 +42,9 @@ impl HfTokenizerConfigJsonFormatter {
4242 pub fn new ( config : ChatTemplate , mixins : ContextMixins ) -> anyhow:: Result < Self > {
4343 let mut env = JinjaEnvironment :: default ( ) . env ( ) ;
4444
45- let chat_template = config. chat_template . as_ref ( ) . ok_or ( anyhow:: anyhow!(
46- "chat_template field is required in the tokenizer_config.json file"
47- ) ) ?;
45+ // let chat_template = config.chat_template.as_ref().ok_or(anyhow::anyhow!(
46+ // "chat_template field is required in the tokenizer_config.json file"
47+ // ))?;
4848
4949 // add pycompat
5050 // todo: should we use this: minijinja_contrib::add_to_environment(&mut env);
@@ -57,40 +57,43 @@ impl HfTokenizerConfigJsonFormatter {
5757
5858 let mut supports_add_generation_prompt = None ;
5959
60- match & chat_template. 0 {
61- Either :: Left ( x) => {
62- if x. contains ( "add_generation_prompt" ) {
63- tracing:: debug!( "Chat template contains `add_generation_prompt` key. This model supports add_generation_prompt." ) ;
64- supports_add_generation_prompt = Some ( true ) ;
60+ if let Some ( chat_template) = config. chat_template . as_ref ( ) {
61+ match & chat_template. 0 {
62+ Either :: Left ( x) => {
63+ if x. contains ( "add_generation_prompt" ) {
64+ tracing:: debug!( "Chat template contains `add_generation_prompt` key. This model supports add_generation_prompt." ) ;
65+ supports_add_generation_prompt = Some ( true ) ;
66+ }
67+ env. add_template_owned ( "default" , x. to_string ( ) ) ?;
68+ env. add_template_owned ( "tool_use" , x. to_string ( ) ) ?;
6569 }
66- env. add_template_owned ( "default" , x. to_string ( ) ) ?;
67- env. add_template_owned ( "tool_use" , x. to_string ( ) ) ?;
68- }
69- Either :: Right ( map) => {
70- for t in map {
71- for ( k, v) in t. iter ( ) {
72- if v. contains ( "add_generation_prompt" ) {
73- match supports_add_generation_prompt {
74- Some ( true ) | None => {
75- tracing:: debug!( "Chat template contains `add_generation_prompt` key. This model supports add_generation_prompt." ) ;
76- supports_add_generation_prompt = Some ( true ) ;
77- }
78- Some ( false ) => {
79- tracing:: warn!( "Not all templates contain `add_generation_prompt` key. This model does not support add_generation_prompt." ) ;
70+ Either :: Right ( map) => {
71+ for t in map {
72+ for ( k, v) in t. iter ( ) {
73+ if v. contains ( "add_generation_prompt" ) {
74+ match supports_add_generation_prompt {
75+ Some ( true ) | None => {
76+ tracing:: debug!( "Chat template contains `add_generation_prompt` key. This model supports add_generation_prompt." ) ;
77+ supports_add_generation_prompt = Some ( true ) ;
78+ }
79+ Some ( false ) => {
80+ tracing:: warn!( "Not all templates contain `add_generation_prompt` key. This model does not support add_generation_prompt." ) ;
81+ }
8082 }
83+ } else {
84+ supports_add_generation_prompt = Some ( false ) ;
8185 }
82- } else {
83- supports_add_generation_prompt = Some ( false ) ;
86+ env. add_template_owned ( k. to_string ( ) , v. to_string ( ) ) ?;
8487 }
85- env. add_template_owned ( k. to_string ( ) , v. to_string ( ) ) ?;
8688 }
87- }
88- if env . templates ( ) . count ( ) == 0 {
89- anyhow :: bail! ( "Chat template does not contain a `tool_use` or `default` key. Please ensure it contains at least a `default` key, although `tool_use` should be specified for using tools." ) ;
89+ if env . templates ( ) . count ( ) == 0 {
90+ anyhow :: bail! ( "Chat template does not contain a `tool_use` or `default` key. Please ensure it contains at least a `default` key, although `tool_use` should be specified for using tools." ) ;
91+ }
9092 }
9193 }
9294 }
9395
96+
9497 Ok ( HfTokenizerConfigJsonFormatter {
9598 env,
9699 config,
0 commit comments