Skip to content

Commit 271f163

Browse files
committed
Added explicitly typed exception for Template validation
1 parent 41eb7dc commit 271f163

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

LLama/Exceptions/RuntimeError.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,21 @@ public MissingTemplateException(string message)
7777
}
7878
}
7979

80+
/// <summary>
81+
/// `llama_decode` return a non-zero status code
82+
/// </summary>
83+
public class TemplateNotFoundException
84+
: RuntimeError
85+
{
86+
/// <inheritdoc />
87+
public TemplateNotFoundException(string name)
88+
: base($"llama_model_chat_template failed: Tried to retrieve template '{name}' but it couldn't be found.\n" +
89+
$"This might mean that the model was exported incorrectly, or that this is a base model that contains no templates.\n" +
90+
$"This exception can be disabled by passing 'strict=false' as a parameter when retrieving the template.")
91+
{
92+
}
93+
}
94+
8095
/// <summary>
8196
/// `llama_get_logits_ith` returned null, indicating that the index was invalid
8297
/// </summary>

LLama/Native/SafeLlamaModelHandle.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,9 +614,7 @@ internal IReadOnlyDictionary<string, string> ReadMetadata()
614614
if (bytesPtr == null)
615615
{
616616
if (strict)
617-
throw new Exception($"Tried to retrieve template for '{name}' but no templates were found.\n" +
618-
$"This might mean that the model was exported incorrectly, or that this is a base model that contains no template.\n" +
619-
$"This exception can be disabled by passing 'strict=false' as a parameter when retrieving the template.");
617+
throw new TemplateNotFoundException(name ?? "default template");
620618
else
621619
return null;
622620

0 commit comments

Comments
 (0)