You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

IReadOnlyListExtensions.cs 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. using System;
  2. using System.Buffers;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Text;
  6. using LLama.Native;
  7. namespace LLama.Extensions
  8. {
  9. internal static class IReadOnlyListExtensions
  10. {
  11. /// <summary>
  12. /// Find the index of `item` in `list`
  13. /// </summary>
  14. /// <typeparam name="T"></typeparam>
  15. /// <param name="list">list to search</param>
  16. /// <param name="item">item to search for</param>
  17. /// <returns></returns>
  18. public static int? IndexOf<T>(this IReadOnlyList<T> list, T item)
  19. where T : IEquatable<T>
  20. {
  21. for (var i = 0; i < list.Count; i++)
  22. {
  23. if (list[i].Equals(item))
  24. return i;
  25. }
  26. return null;
  27. }
  28. /// <summary>
  29. /// Check if the given set of tokens ends with any of the given strings
  30. /// </summary>
  31. /// <param name="tokens">Tokens to check</param>
  32. /// <param name="queries">Strings to search for</param>
  33. /// <param name="model">Model to use to convert tokens into bytes</param>
  34. /// <param name="encoding">Encoding to use to convert bytes into characters</param>
  35. /// <returns></returns>
  36. [Obsolete("Use an Antiprompt processor instead")]
  37. internal static bool TokensEndsWithAnyString<TTokens, TQueries>(this TTokens tokens, TQueries? queries, SafeLlamaModelHandle model, Encoding encoding)
  38. where TTokens : IReadOnlyList<int>
  39. where TQueries : IReadOnlyList<string>
  40. {
  41. if (queries == null || queries.Count == 0 || tokens.Count == 0)
  42. return false;
  43. // Find the length of the longest query
  44. var longest = 0;
  45. foreach (var candidate in queries)
  46. longest = Math.Max(longest, candidate.Length);
  47. // Rent an array to detokenize into
  48. var builderArray = ArrayPool<char>.Shared.Rent(longest);
  49. try
  50. {
  51. // Convert as many tokens as possible into the builderArray
  52. var characters = model.TokensToSpan(tokens, builderArray.AsSpan(0, longest), encoding);
  53. // Check every query to see if it's present
  54. foreach (var query in queries)
  55. if (characters.EndsWith(query.AsSpan()))
  56. return true;
  57. return false;
  58. }
  59. finally
  60. {
  61. ArrayPool<char>.Shared.Return(builderArray);
  62. }
  63. }
  64. /// <summary>
  65. /// Check if the given set of tokens ends with any of the given strings
  66. /// </summary>
  67. /// <param name="tokens">Tokens to check</param>
  68. /// <param name="queries">Strings to search for</param>
  69. /// <param name="model">Model to use to convert tokens into bytes</param>
  70. /// <param name="encoding">Encoding to use to convert bytes into characters</param>
  71. /// <returns></returns>
  72. [Obsolete("Use an Antiprompt processor instead")]
  73. internal static bool TokensEndsWithAnyString<TTokens>(this TTokens tokens, IList<string>? queries, SafeLlamaModelHandle model, Encoding encoding)
  74. where TTokens : IReadOnlyList<int>
  75. {
  76. if (queries == null || queries.Count == 0 || tokens.Count == 0)
  77. return false;
  78. return tokens.TokensEndsWithAnyString(new ReadonlyWrapper<string>(queries), model, encoding);
  79. }
  80. private readonly struct ReadonlyWrapper<T>
  81. : IReadOnlyList<T>
  82. {
  83. private readonly IList<T> _list;
  84. public int Count => _list.Count;
  85. public T this[int index] => _list[index];
  86. public ReadonlyWrapper(IList<T> list)
  87. {
  88. _list = list;
  89. }
  90. public IEnumerator<T> GetEnumerator()
  91. {
  92. return _list.GetEnumerator();
  93. }
  94. IEnumerator IEnumerable.GetEnumerator()
  95. {
  96. return ((IEnumerable)_list).GetEnumerator();
  97. }
  98. }
  99. }
  100. }