Skip to content

Commit f734971

Browse files
pcuencajulien-c
andauthored
Explicit check for existence of chat_template (#1725)
`conversational` does not always imply chat_template. Example: https://huggingface.co/facebook/blenderbot-400M-distill/blob/main/README.md In our internal codebase we auto-set `conversational` if a chat template exists, but the opposite is not always true. The model above has an explicit `conversational` tag, but no chat template. See #1722 --------- Co-authored-by: Julien Chaumond <julien@huggingface.co>
1 parent b3a6468 commit f734971

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

packages/tasks/src/model-data.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ export interface ModelData {
4444
quant_method?: string;
4545
};
4646
tokenizer_config?: TokenizerConfig;
47+
processor_config?: {
48+
chat_template?: string;
49+
};
50+
chat_template_jinja?: string;
4751
adapter_transformers?: {
4852
model_name?: string;
4953
model_class?: string;

packages/tasks/src/model-libraries-snippets.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,11 @@ export const terratorch = (model: ModelData): string[] => [
14761476
model = BACKBONE_REGISTRY.build("${model.id}")`,
14771477
];
14781478

1479+
const hasChatTemplate = (model: ModelData): boolean =>
1480+
model.config?.tokenizer_config?.chat_template !== undefined ||
1481+
model.config?.processor_config?.chat_template !== undefined ||
1482+
model.config?.chat_template_jinja !== undefined;
1483+
14791484
export const transformers = (model: ModelData): string[] => {
14801485
const info = model.transformersInfo;
14811486
if (!info) {
@@ -1498,7 +1503,7 @@ export const transformers = (model: ModelData): string[] => {
14981503
`${processorVarName} = ${info.processor}.from_pretrained("${model.id}"` + remote_code_snippet + ")",
14991504
`model = ${info.auto_model}.from_pretrained("${model.id}"` + remote_code_snippet + ")"
15001505
);
1501-
if (model.tags.includes("conversational")) {
1506+
if (model.tags.includes("conversational") && hasChatTemplate(model)) {
15021507
if (model.tags.includes("image-text-to-text")) {
15031508
autoSnippet.push(
15041509
"messages = [",

0 commit comments

Comments
 (0)