Skip to content

Commit a7e5769

Browse files
committed
Bindings: Fix chat template fetching
Make sure to write the metadata key to the buffer and return it. Also, fix error handling when loading a template from the model. Signed-off-by: kingbri <8082010+kingbri1@users.noreply.github.com>
1 parent 1599771 commit a7e5769

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

bindings/binding.cpp

+13
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,20 @@ char* GetModelChatTemplate(const llama_model* model) {
4141
static const char* tokenizerTemplateKey = "tokenizer.chat_template";
4242
const int32_t bufSize = llama_model_meta_val_str(model, tokenizerTemplateKey, nullptr, 0) + 1;
4343

44+
// Return null if template doesn't exist
45+
if (bufSize <= 1) {
46+
return nullptr;
47+
}
48+
4449
const auto buffer = new char[bufSize];
50+
llama_model_meta_val_str(model, tokenizerTemplateKey, buffer, bufSize);
51+
52+
// Additional check to see if the buffer has data
53+
if (buffer[0] == '\0') {
54+
delete[] buffer;
55+
return nullptr;
56+
}
57+
4558
return buffer;
4659
}
4760

bindings/bindings.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -526,9 +526,8 @@ export class Model {
526526
}
527527
} catch (error) {
528528
if (error instanceof Error) {
529-
logger.warn(
530-
`${error.stack} \nAttempting next template method.`,
531-
);
529+
logger.error(error.stack);
530+
logger.warn("Attempting next template method.");
532531
}
533532
}
534533
}
@@ -539,7 +538,8 @@ export class Model {
539538
);
540539
} else {
541540
logger.warn(
542-
"Could not create a prompt template because of the above errors\n " +
541+
"Could not create a prompt template because of the above errors.\n" +
542+
"Chat completions are disabled.\n" +
543543
"YALS will continue loading without the prompt template.\n" +
544544
"Please proofread the template and make sure it's compatible " +
545545
"with huggingface's jinja subset.",
@@ -892,7 +892,7 @@ export class Model {
892892
});
893893

894894
if (templatePtr === null) {
895-
throw new Error("Failed to get model chat template from model");
895+
throw new Error("No chat template found in model");
896896
}
897897

898898
// Copy to owned string

0 commit comments

Comments
 (0)