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
|
- import {
- removePrefix,
- normalizeApostrophes,
- isEmptyResponse,
- isBasicRefusal,
- } from '../../src/redteam/util';
- describe('removePrefix', () => {
- it('should remove a simple prefix', () => {
- expect(removePrefix('Prompt: Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should be case insensitive', () => {
- expect(removePrefix('PROMPT: Hello world', 'prompt')).toBe('Hello world');
- });
- it('should remove asterisks from the prefix', () => {
- expect(removePrefix('**Prompt:** Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should handle multiple asterisks', () => {
- expect(removePrefix('***Prompt:*** Hello world', 'Prompt')).toBe('Hello world');
- });
- it('should return the same string if prefix is not found', () => {
- expect(removePrefix('Hello world', 'Prefix')).toBe('Hello world');
- });
- it('should handle empty strings', () => {
- expect(removePrefix('', 'Prefix')).toBe('');
- });
- it('should handle prefix that is the entire string', () => {
- expect(removePrefix('Prompt:', 'Prompt')).toBe('');
- });
- });
- describe('normalizeApostrophes', () => {
- it('should normalize different types of apostrophes', () => {
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- expect(normalizeApostrophes('I′m')).toBe("I'm");
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- expect(normalizeApostrophes("I'm")).toBe("I'm");
- });
- it('should handle strings without apostrophes', () => {
- expect(normalizeApostrophes('Hello world')).toBe('Hello world');
- });
- });
- describe('isEmptyResponse', () => {
- it('should return true for empty responses', () => {
- expect(isEmptyResponse('')).toBe(true);
- expect(isEmptyResponse(' ')).toBe(true);
- expect(isEmptyResponse('{}')).toBe(true);
- expect(isEmptyResponse(' {} ')).toBe(true);
- });
- it('should return false for non-empty responses', () => {
- expect(isEmptyResponse('Hello')).toBe(false);
- expect(isEmptyResponse('{"key": "value"}')).toBe(false);
- });
- });
- describe('isBasicRefusal', () => {
- it('should detect refusal prefixes', () => {
- expect(isBasicRefusal('I am sorry, I cannot help with that')).toBe(true);
- expect(isBasicRefusal("I'm not able to assist with this request")).toBe(true);
- expect(isBasicRefusal('I apologize, but I cannot do that')).toBe(true);
- });
- it('should detect refusal substrings', () => {
- expect(isBasicRefusal('As an AI language model, I cannot help with that')).toBe(true);
- expect(isBasicRefusal('I cannot assist with that request')).toBe(true);
- expect(isBasicRefusal('That would not be appropriate')).toBe(true);
- });
- it('should normalize apostrophes in responses', () => {
- expect(isBasicRefusal("I′m sorry, I can't help")).toBe(true);
- expect(isBasicRefusal("I'm unable to assist")).toBe(true);
- });
- it('should handle case insensitivity', () => {
- expect(isBasicRefusal('I AM SORRY, I CANNOT HELP')).toBe(true);
- expect(isBasicRefusal('as an ai language model')).toBe(true);
- });
- it('should return false for non-refusal responses', () => {
- expect(isBasicRefusal('I will help you with that')).toBe(false);
- expect(isBasicRefusal('Here is the information you requested')).toBe(false);
- expect(isBasicRefusal('The answer is 42')).toBe(false);
- });
- });
|