Skip to content

Commit 45a8a19

Browse files
committed
Adds an exception for missing templates.
1 parent b733820 commit 45a8a19

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

LLama/Exceptions/RuntimeError.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ public LLamaDecodeError(DecodeResult returnCode)
5858
}
5959
}
6060

61+
/// <summary>
62+
/// `llama_decode` return a non-zero status code
63+
/// </summary>
64+
public class MissingTemplateException
65+
: RuntimeError
66+
{
67+
/// <inheritdoc />
68+
public MissingTemplateException()
69+
: base("llama_chat_apply_template failed: template not found")
70+
{
71+
}
72+
73+
/// <inheritdoc />
74+
public MissingTemplateException(string message)
75+
: base($"llama_chat_apply_template failed: template not found for '{message}'")
76+
{
77+
}
78+
}
79+
6180
/// <summary>
6281
/// `llama_get_logits_ith` returned null, indicating that the index was invalid
6382
/// </summary>

LLama/LLamaTemplate.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Diagnostics;
44
using System.Text;
5+
using LLama.Exceptions;
56
using LLama.Native;
67

78
namespace LLama;
@@ -250,6 +251,19 @@ public ReadOnlySpan<byte> Apply()
250251
{
251252
// Run templater and discover true length
252253
var outputLength = ApplyInternal(_nativeChatMessages.AsSpan(0, Count), output);
254+
255+
// if we have a return code of -1, the template was not found.
256+
if (outputLength == -1)
257+
{
258+
if (_customTemplate != null)
259+
{
260+
throw new MissingTemplateException(Encoding.GetString(_customTemplate));
261+
}
262+
else
263+
{
264+
throw new MissingTemplateException();
265+
}
266+
}
253267

254268
// If length was too big for output buffer run it again
255269
if (outputLength > output.Length)

0 commit comments

Comments
 (0)