Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

util.test.ts 3.2 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  1. import {
  2. removePrefix,
  3. normalizeApostrophes,
  4. isEmptyResponse,
  5. isBasicRefusal,
  6. } from '../../src/redteam/util';
  7. describe('removePrefix', () => {
  8. it('should remove a simple prefix', () => {
  9. expect(removePrefix('Prompt: Hello world', 'Prompt')).toBe('Hello world');
  10. });
  11. it('should be case insensitive', () => {
  12. expect(removePrefix('PROMPT: Hello world', 'prompt')).toBe('Hello world');
  13. });
  14. it('should remove asterisks from the prefix', () => {
  15. expect(removePrefix('**Prompt:** Hello world', 'Prompt')).toBe('Hello world');
  16. });
  17. it('should handle multiple asterisks', () => {
  18. expect(removePrefix('***Prompt:*** Hello world', 'Prompt')).toBe('Hello world');
  19. });
  20. it('should return the same string if prefix is not found', () => {
  21. expect(removePrefix('Hello world', 'Prefix')).toBe('Hello world');
  22. });
  23. it('should handle empty strings', () => {
  24. expect(removePrefix('', 'Prefix')).toBe('');
  25. });
  26. it('should handle prefix that is the entire string', () => {
  27. expect(removePrefix('Prompt:', 'Prompt')).toBe('');
  28. });
  29. });
  30. describe('normalizeApostrophes', () => {
  31. it('should normalize different types of apostrophes', () => {
  32. expect(normalizeApostrophes("I'm")).toBe("I'm");
  33. expect(normalizeApostrophes('I′m')).toBe("I'm");
  34. expect(normalizeApostrophes("I'm")).toBe("I'm");
  35. expect(normalizeApostrophes("I'm")).toBe("I'm");
  36. });
  37. it('should handle strings without apostrophes', () => {
  38. expect(normalizeApostrophes('Hello world')).toBe('Hello world');
  39. });
  40. });
  41. describe('isEmptyResponse', () => {
  42. it('should return true for empty responses', () => {
  43. expect(isEmptyResponse('')).toBe(true);
  44. expect(isEmptyResponse(' ')).toBe(true);
  45. expect(isEmptyResponse('{}')).toBe(true);
  46. expect(isEmptyResponse(' {} ')).toBe(true);
  47. });
  48. it('should return false for non-empty responses', () => {
  49. expect(isEmptyResponse('Hello')).toBe(false);
  50. expect(isEmptyResponse('{"key": "value"}')).toBe(false);
  51. });
  52. });
  53. describe('isBasicRefusal', () => {
  54. it('should detect refusal prefixes', () => {
  55. expect(isBasicRefusal('I am sorry, I cannot help with that')).toBe(true);
  56. expect(isBasicRefusal("I'm not able to assist with this request")).toBe(true);
  57. expect(isBasicRefusal('I apologize, but I cannot do that')).toBe(true);
  58. });
  59. it('should detect refusal substrings', () => {
  60. expect(isBasicRefusal('As an AI language model, I cannot help with that')).toBe(true);
  61. expect(isBasicRefusal('I cannot assist with that request')).toBe(true);
  62. expect(isBasicRefusal('That would not be appropriate')).toBe(true);
  63. });
  64. it('should normalize apostrophes in responses', () => {
  65. expect(isBasicRefusal("I′m sorry, I can't help")).toBe(true);
  66. expect(isBasicRefusal("I'm unable to assist")).toBe(true);
  67. });
  68. it('should handle case insensitivity', () => {
  69. expect(isBasicRefusal('I AM SORRY, I CANNOT HELP')).toBe(true);
  70. expect(isBasicRefusal('as an ai language model')).toBe(true);
  71. });
  72. it('should return false for non-refusal responses', () => {
  73. expect(isBasicRefusal('I will help you with that')).toBe(false);
  74. expect(isBasicRefusal('Here is the information you requested')).toBe(false);
  75. expect(isBasicRefusal('The answer is 42')).toBe(false);
  76. });
  77. });
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...