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.

TokenTests.cs 2.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System.Text;
  2. using LLama.Common;
  3. using LLama.Extensions;
  4. namespace LLama.Unittest;
  5. public sealed class TokenTests
  6. : IDisposable
  7. {
  8. private readonly ModelParams _params;
  9. private readonly LLamaWeights _model;
  10. public TokenTests()
  11. {
  12. _params = new ModelParams(Constants.ModelPath)
  13. {
  14. ContextSize = 2048
  15. };
  16. _model = LLamaWeights.LoadFromFile(_params);
  17. }
  18. public void Dispose()
  19. {
  20. _model.Dispose();
  21. }
  22. [Fact]
  23. public void TokensEndWith()
  24. {
  25. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  26. var result = tokens.TokensEndsWithAnyString(new[]
  27. {
  28. "a fish",
  29. "the mat",
  30. "this is an improbably long query to be using for this method"
  31. }, _model.NativeHandle, Encoding.UTF8);
  32. Assert.True(result);
  33. }
  34. [Fact]
  35. public void TokensEndSubstring()
  36. {
  37. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  38. var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
  39. {
  40. "at",
  41. }, _model.NativeHandle, Encoding.UTF8);
  42. Assert.True(result);
  43. }
  44. [Fact]
  45. public void TokensNotEndWith()
  46. {
  47. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  48. var result = tokens.TokensEndsWithAnyString((IList<string>)new[]
  49. {
  50. "a fish",
  51. "The cat sat on the edge of the ma",
  52. "this is an improbably long query to be using for this method"
  53. }, _model.NativeHandle, Encoding.UTF8);
  54. Assert.False(result);
  55. }
  56. [Fact]
  57. public void TokensNotEndWithNothing()
  58. {
  59. var tokens = _model.NativeHandle.Tokenize("The cat sat on the edge of the mat", false, true, Encoding.UTF8);
  60. var result = tokens.TokensEndsWithAnyString((IList<string>)Array.Empty<string>(), _model.NativeHandle, Encoding.UTF8);
  61. Assert.False(result);
  62. }
  63. }