Skip to content

Adds an exception for missing templates. #1034

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
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
19 changes: 19 additions & 0 deletions LLama/Exceptions/RuntimeError.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public LLamaDecodeError(DecodeResult returnCode)
}
}

/// <summary>
/// `llama_decode` return a non-zero status code
/// </summary>
public class MissingTemplateException
: RuntimeError
{
/// <inheritdoc />
public MissingTemplateException()
: base("llama_chat_apply_template failed: template not found")
{
}

/// <inheritdoc />
public MissingTemplateException(string message)
: base($"llama_chat_apply_template failed: template not found for '{message}'")
{
}
}

/// <summary>
/// `llama_get_logits_ith` returned null, indicating that the index was invalid
/// </summary>
Expand Down
14 changes: 14 additions & 0 deletions LLama/LLamaTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using LLama.Exceptions;
using LLama.Native;

namespace LLama;
Expand Down Expand Up @@ -250,6 +251,19 @@ public ReadOnlySpan<byte> Apply()
{
// Run templater and discover true length
var outputLength = ApplyInternal(_nativeChatMessages.AsSpan(0, Count), output);

// if we have a return code of -1, the template was not found.
if (outputLength == -1)
{
if (_customTemplate != null)
{
throw new MissingTemplateException(Encoding.GetString(_customTemplate));
}
else
{
throw new MissingTemplateException();
}
}

// If length was too big for output buffer run it again
if (outputLength > output.Length)
Expand Down
Loading