@@ -7,5 +7,50 @@ Documentation: https://huggingface.co/docs/hub/en/ollama
77## Chat template converter  
88
99``` ts 
10- const   
10+ import  { convertJinjaToGoTemplate  } from  " @huggingface/ollama-utils"  ;
11+ 
12+ const   MODEL_INFO_URL =  " https://huggingface.co/api/models/bartowski/Llama-3.2-3B-Instruct-GGUF?expand[]=gguf"  ;
13+ const   modelInfo =  await  (await  fetch (MODEL_INFO_URL )).json ();
14+ console .log (modelInfo );
15+ /** 
16+  * { 
17+  *   gguf: { 
18+  *     chat_template: "here is the Jinja chat template", 
19+  *     bos_token: "...", 
20+  *     eos_token: "...", 
21+  *     [...] 
22+  *   } 
23+  * } 
24+  */  
25+ const   convertedTemplate =  convertJinjaToGoTemplate (modelInfo .gguf );
26+ if  (convertedTemplate ) {
27+   console .log (convertedTemplate .ollama );
28+   /** 
29+    * { 
30+    *   template: "this is the converted template, compatible with Ollama", 
31+    *   tokens: [... list of special tokens], 
32+    *   params: { 
33+    *     stop: [... list of stop tokens or stop words] 
34+    *   } 
35+    * } 
36+    */  
37+ } else  {
38+   console .error (" Conversion is not successful"  );
39+ }
1140``` 
41+ 
42+ ## How can I add a custom template?  
43+ 
44+ Most templates will be converted automatically. You can debug the output template using:
45+ -  This space to retrieve the converted template: https://huggingface.co/spaces/ngxson/debug_ollama_manifest 
46+ -  And this space to apply the Go template into a list of messages: https://huggingface.co/spaces/ngxson/ollama_template_test 
47+ 
48+ Please only add a new template only when the conversion above is not successful. Cases that are acceptable to add a custom handler:
49+ -  The covnerted template is wrong
50+ -  The Jinja template is not compatible with ` @huggingface/jinja ` 
51+ -  The Jinja template is not "linear"
52+ 
53+ To add a new custom handler:
54+ 1 .  Edit the list of ` CUSTOM_TEMPLATE_MAPPING `  inside ` chat-template.ts ` 
55+ 2 .  Add a new test case in ` chat-template.spec.ts ` 
56+ 3 .  hen push your change into new PR.
0 commit comments