Skip to content

Commit

Permalink
Adds an exception for missing templates.
Browse files Browse the repository at this point in the history
  • Loading branch information
phil-scott-78 committed Jan 3, 2025
1 parent b733820 commit 45a8a19
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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

0 comments on commit 45a8a19

Please sign in to comment.