Skip to content
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/Extensions/IReadOnlyListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ namespace LLama.Extensions
{
internal static class IReadOnlyListExtensions
{
/// <summary>
/// Find the index of `item` in `list`
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list">list to search</param>
/// <param name="item">item to search for</param>
/// <returns></returns>
public static int? IndexOf<T>(this IReadOnlyList<T> list, T item)
where T : IEquatable<T>
{
Expand Down Expand Up @@ -61,6 +68,14 @@ internal static bool TokensEndsWithAnyString<TTokens, TQueries>(this TTokens tok
}
}

/// <summary>
/// Check if the given set of tokens ends with any of the given strings
/// </summary>
/// <param name="tokens">Tokens to check</param>
/// <param name="queries">Strings to search for</param>
/// <param name="model">Model to use to convert tokens into bytes</param>
/// <param name="encoding">Encoding to use to convert bytes into characters</param>
/// <returns></returns>
internal static bool TokensEndsWithAnyString<TTokens>(this TTokens tokens, IList<string>? queries, SafeLlamaModelHandle model, Encoding encoding)
where TTokens : IReadOnlyList<int>
{
Expand Down
10 changes: 10 additions & 0 deletions LLama/LLamaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ public string TokenToString(llama_token token)
return NativeHandle.TokenToString(token, Encoding);
}

/// <summary>
/// Append a single token to a string builder
/// </summary>
/// <param name="token">Token to decode</param>
/// <param name="dest">string builder to append the result to</param>
public void TokenToString(llama_token token, StringBuilder dest)
{
NativeHandle.TokenToString(token, Encoding, dest);
}

/// <inheritdoc />
public void Dispose()
{
Expand Down
18 changes: 4 additions & 14 deletions LLama/LLamaInstructExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using LLama.Extensions;

namespace LLama
{
Expand Down Expand Up @@ -139,21 +140,10 @@ protected override bool PostProcess(IInferenceParams inferenceParams, InferState
extraOutputs = null;
if (_embed_inps.Count <= _consumedTokensCount)
{
if (args.Antiprompts is not null && args.Antiprompts.Count > 0)
if (_last_n_tokens.Items.TokensEndsWithAnyString(args.Antiprompts, Context.NativeHandle.ModelHandle, Context.Encoding))
{
var last_output_builder = new StringBuilder();
foreach (var token in _last_n_tokens)
Context.NativeHandle.TokenToString(token, Context.Encoding, last_output_builder);
var last_output = last_output_builder.ToString();

foreach (var antiprompt in args.Antiprompts)
{
if (last_output.EndsWith(antiprompt))
{
args.WaitForInput = true;
return true;
}
}
args.WaitForInput = true;
return true;
}

if (_pastTokensCount > 0 && args.WaitForInput)
Expand Down