Skip to content

Added 'strict' parameter for template retrieval #1124

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions LLama/Exceptions/RuntimeError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ public MissingTemplateException(string message)
}
}

/// <summary>
/// `llama_decode` return a non-zero status code
/// </summary>
public class TemplateNotFoundException
: RuntimeError
{
/// <inheritdoc />
public TemplateNotFoundException(string name)
: base($"llama_model_chat_template failed: Tried to retrieve template '{name}' but it couldn't be found.\n" +
$"This might mean that the model was exported incorrectly, or that this is a base model that contains no templates.\n" +
$"This exception can be disabled by passing 'strict=false' as a parameter when retrieving the template.")
{
}
}

/// <summary>
/// `llama_get_logits_ith` returned null, indicating that the index was invalid
/// </summary>
Expand Down
16 changes: 9 additions & 7 deletions LLama/LLamaTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,19 +105,21 @@ public bool AddAssistant
/// <summary>
/// Construct a new template, using the default model template
/// </summary>
/// <param name="model"></param>
/// <param name="name"></param>
public LLamaTemplate(SafeLlamaModelHandle model, string? name = null)
: this(model.GetTemplate(name))
/// <param name="model">The native handle of the loaded model.</param>
/// <param name="name">The name of the template, in case there are many or differently named. Set to 'null' for the default behaviour of finding an appropriate match.</param>
/// <param name="strict">Setting this to true will cause the call to throw if no valid templates are found.</param>
public LLamaTemplate(SafeLlamaModelHandle model, string? name = null, bool strict = true)
: this(model.GetTemplate(name, strict))
{
}

/// <summary>
/// Construct a new template, using the default model template
/// </summary>
/// <param name="weights"></param>
public LLamaTemplate(LLamaWeights weights)
: this(weights.NativeHandle)
/// <param name="weights">The handle of the loaded model's weights.</param>
/// <param name="strict">Setting this to true will cause the call to throw if no valid templates are found.</param>
public LLamaTemplate(LLamaWeights weights, bool strict = true)
: this(weights.NativeHandle, strict: strict)
{
}

Expand Down
13 changes: 10 additions & 3 deletions LLama/Native/SafeLlamaModelHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -603,15 +603,22 @@ internal IReadOnlyDictionary<string, string> ReadMetadata()
/// Get the default chat template. Returns nullptr if not available
/// If name is NULL, returns the default chat template
/// </summary>
/// <param name="name"></param>
/// <param name="name">The name of the template, in case there are many or differently named. Set to 'null' for the default behaviour of finding an appropriate match.</param>
/// <param name="strict">Setting this to true will cause the call to throw if no valid templates are found.</param>
/// <returns></returns>
public string? GetTemplate(string? name = null)
public string? GetTemplate(string? name = null, bool strict = true)
{
unsafe
{
var bytesPtr = llama_model_chat_template(this, name);
if (bytesPtr == null)
return null;
{
if (strict)
throw new TemplateNotFoundException(name ?? "default template");
else
return null;

}

// Find null terminator
var spanBytes = new Span<byte>(bytesPtr, int.MaxValue);
Expand Down
Loading