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

unaligned.test.ts 6.1 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  1. import { getEnvBool, getEnvString } from '../../../../src/envars';
  2. import { PromptfooHarmfulCompletionProvider } from '../../../../src/providers/promptfoo';
  3. import {
  4. LLAMA_GUARD_REPLICATE_PROVIDER,
  5. UNALIGNED_PROVIDER_HARM_PLUGINS,
  6. } from '../../../../src/redteam/constants';
  7. import { REDTEAM_MODEL_CATEGORIES } from '../../../../src/redteam/plugins/harmful/constants';
  8. import { getHarmfulTests } from '../../../../src/redteam/plugins/harmful/unaligned';
  9. import type { HarmfulCategory } from '../../../../src/redteam/plugins/harmful/constants';
  10. import type { ApiProvider } from '../../../../src/types';
  11. jest.mock('../../../../src/envars');
  12. describe('harmful plugin', () => {
  13. let mockProvider: ApiProvider;
  14. let mockCallApi: jest.SpyInstance;
  15. beforeEach(() => {
  16. jest.clearAllMocks();
  17. mockProvider = {
  18. callApi: jest.fn(),
  19. id: jest.fn().mockReturnValue('test-provider'),
  20. };
  21. if (mockCallApi) {
  22. mockCallApi.mockRestore();
  23. }
  24. mockCallApi = jest.spyOn(PromptfooHarmfulCompletionProvider.prototype, 'callApi').mockReset();
  25. jest.mocked(getEnvBool).mockReset();
  26. jest.mocked(getEnvString).mockReset();
  27. });
  28. afterEach(() => {
  29. jest.clearAllMocks();
  30. if (mockCallApi) {
  31. mockCallApi.mockRestore();
  32. }
  33. });
  34. describe('getHarmfulTests', () => {
  35. it('should handle unaligned provider plugins with multiple prompts', async () => {
  36. const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
  37. mockCallApi.mockResolvedValueOnce({
  38. output: ['Test harmful output', 'Another test output'],
  39. });
  40. const result = await getHarmfulTests(
  41. {
  42. provider: mockProvider,
  43. purpose: 'test purpose',
  44. injectVar: 'testVar',
  45. n: 2,
  46. delayMs: 0,
  47. },
  48. unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
  49. );
  50. expect(mockCallApi).toHaveBeenCalledTimes(1);
  51. expect(result).toHaveLength(2);
  52. const prompts = result.map((r) => r.vars?.testVar);
  53. expect(prompts).toContain('Test harmful output');
  54. expect(prompts).toContain('Another test output');
  55. });
  56. it('should retry when not enough unique prompts are returned', async () => {
  57. const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
  58. mockCallApi
  59. .mockResolvedValueOnce({ output: ['Test output'] })
  60. .mockResolvedValueOnce({ output: ['Test output'] })
  61. .mockResolvedValueOnce({ output: ['Another test output'] });
  62. const result = await getHarmfulTests(
  63. {
  64. provider: mockProvider,
  65. purpose: 'test purpose',
  66. injectVar: 'testVar',
  67. n: 2,
  68. delayMs: 0,
  69. },
  70. unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
  71. );
  72. expect(mockCallApi).toHaveBeenCalledTimes(3);
  73. expect(result).toHaveLength(2);
  74. const prompts = result.map((r) => r.vars?.testVar);
  75. expect(prompts).toContain('Test output');
  76. expect(prompts).toContain('Another test output');
  77. });
  78. it('should handle empty provider response', async () => {
  79. mockCallApi.mockResolvedValue({ output: [] });
  80. const result = await getHarmfulTests(
  81. {
  82. provider: mockProvider,
  83. purpose: 'test purpose',
  84. injectVar: 'testVar',
  85. n: 1,
  86. delayMs: 0,
  87. },
  88. 'harmful:sex-crime',
  89. );
  90. expect(result).toHaveLength(0);
  91. });
  92. it('should respect delay parameter between API calls', async () => {
  93. const unalignedPlugin = Object.keys(UNALIGNED_PROVIDER_HARM_PLUGINS)[0];
  94. mockCallApi
  95. .mockResolvedValueOnce({ output: ['Test output'] })
  96. .mockResolvedValueOnce({ output: ['Another output'] });
  97. const startTime = Date.now();
  98. await getHarmfulTests(
  99. {
  100. provider: mockProvider,
  101. purpose: 'test purpose',
  102. injectVar: 'testVar',
  103. n: 2,
  104. delayMs: 100,
  105. },
  106. unalignedPlugin as keyof typeof UNALIGNED_PROVIDER_HARM_PLUGINS,
  107. );
  108. expect(Date.now() - startTime).toBeGreaterThanOrEqual(100);
  109. });
  110. it('should handle moderation assertions with OPENAI_API_KEY', async () => {
  111. jest.mocked(getEnvString).mockImplementation((key) => {
  112. if (key === 'OPENAI_API_KEY') {
  113. return 'test-key';
  114. }
  115. return '';
  116. });
  117. mockCallApi.mockResolvedValueOnce({ output: ['Test output'] });
  118. const result = await getHarmfulTests(
  119. {
  120. provider: mockProvider,
  121. purpose: 'test purpose',
  122. injectVar: 'testVar',
  123. n: 1,
  124. delayMs: 0,
  125. },
  126. 'harmful:sex-crime',
  127. );
  128. expect(result).toHaveLength(1);
  129. expect(result[0].vars?.testVar).toBe('Test output');
  130. expect(result[0].assert).toContainEqual(expect.objectContaining({ type: 'moderation' }));
  131. });
  132. it('should handle moderation assertions with REPLICATE_API_KEY', async () => {
  133. const mockCategory: HarmfulCategory = {
  134. key: 'harmful:privacy',
  135. prompt: 'test prompt',
  136. examples: 'example1',
  137. label: 'Privacy violations',
  138. description: 'test description',
  139. };
  140. jest.spyOn(REDTEAM_MODEL_CATEGORIES, 'find').mockReturnValue(mockCategory);
  141. jest.mocked(getEnvString).mockImplementation((key) => {
  142. if (key === 'REPLICATE_API_KEY') {
  143. return 'test-key';
  144. }
  145. return '';
  146. });
  147. const mockOutput = 'Test output';
  148. jest
  149. .spyOn(PromptfooHarmfulCompletionProvider.prototype, 'callApi')
  150. .mockResolvedValue({ output: [mockOutput] });
  151. const result = await getHarmfulTests(
  152. {
  153. provider: mockProvider,
  154. purpose: 'test purpose',
  155. injectVar: 'testVar',
  156. n: 1,
  157. delayMs: 0,
  158. },
  159. 'harmful:sex-crime',
  160. );
  161. expect(result).toHaveLength(1);
  162. expect(result[0].vars?.testVar).toBe('Test output');
  163. expect(result[0].assert).toContainEqual(
  164. expect.objectContaining({
  165. type: 'moderation',
  166. provider: LLAMA_GUARD_REPLICATE_PROVIDER,
  167. }),
  168. );
  169. });
  170. });
  171. });
Tip!

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

Comments

Loading...