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

refusal.test.ts 9.4 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
  1. import { handleIsRefusal } from '../../src/assertions/refusal';
  2. import type {
  3. Assertion,
  4. AssertionValueFunctionContext,
  5. AtomicTestCase,
  6. GradingResult,
  7. } from '../../src/types';
  8. const assertion: Assertion = {
  9. type: 'is-refusal',
  10. };
  11. const defaultContext: AssertionValueFunctionContext = {
  12. prompt: undefined,
  13. vars: {},
  14. test: {} as AtomicTestCase,
  15. logProbs: undefined,
  16. config: {},
  17. provider: undefined,
  18. providerResponse: undefined,
  19. };
  20. describe('is-refusal assertion', () => {
  21. // -------------------------------------------------- Basic Tests ------------------------------------------------------ //
  22. describe('Basic tests', () => {
  23. it('should pass when the output is a refusal message', () => {
  24. const result: GradingResult = handleIsRefusal({
  25. assertion,
  26. output: 'I apologize, but I cannot assist with that request.',
  27. inverse: false,
  28. baseType: 'is-refusal',
  29. context: defaultContext,
  30. outputString: 'I apologize, but I cannot assist with that request.',
  31. providerResponse: {},
  32. test: {} as AtomicTestCase,
  33. });
  34. expect(result).toEqual({
  35. assertion,
  36. pass: true,
  37. score: 1,
  38. reason: 'Assertion passed',
  39. });
  40. });
  41. it('should fail when the output is not a refusal message', () => {
  42. const result: GradingResult = handleIsRefusal({
  43. assertion,
  44. output: "Here's how to accomplish that task...",
  45. inverse: false,
  46. baseType: 'is-refusal',
  47. context: defaultContext,
  48. outputString: "Here's how to accomplish that task...",
  49. providerResponse: {},
  50. test: {} as AtomicTestCase,
  51. });
  52. expect(result).toEqual({
  53. assertion,
  54. pass: false,
  55. score: 0,
  56. reason: 'Expected output to be a refusal',
  57. });
  58. });
  59. it('should pass when inverse=true and output is not a refusal', () => {
  60. const result: GradingResult = handleIsRefusal({
  61. assertion,
  62. output: "Here's how to accomplish that task...",
  63. inverse: true,
  64. baseType: 'is-refusal',
  65. context: defaultContext,
  66. outputString: "Here's how to accomplish that task...",
  67. providerResponse: {},
  68. test: {} as AtomicTestCase,
  69. });
  70. expect(result).toEqual({
  71. assertion,
  72. pass: true,
  73. score: 1,
  74. reason: 'Assertion passed',
  75. });
  76. });
  77. it('should fail when inverse=true and output is a refusal', () => {
  78. const result: GradingResult = handleIsRefusal({
  79. assertion,
  80. output: 'I apologize, but I cannot assist with that request.',
  81. inverse: true,
  82. baseType: 'is-refusal',
  83. context: defaultContext,
  84. outputString: 'I apologize, but I cannot assist with that request.',
  85. providerResponse: {},
  86. test: {} as AtomicTestCase,
  87. });
  88. expect(result).toEqual({
  89. assertion,
  90. pass: false,
  91. score: 0,
  92. reason: 'Expected output to not be a refusal',
  93. });
  94. });
  95. });
  96. // ------------------------------------------ Edge Cases and Special Values ------------------------------------------- //
  97. describe('Edge cases and special values', () => {
  98. it('should handle undefined output', () => {
  99. const result: GradingResult = handleIsRefusal({
  100. assertion,
  101. output: undefined as any,
  102. inverse: false,
  103. baseType: 'is-refusal',
  104. context: defaultContext,
  105. outputString: '',
  106. providerResponse: {},
  107. test: {} as AtomicTestCase,
  108. });
  109. expect(result).toEqual({
  110. assertion,
  111. pass: true,
  112. score: 0,
  113. reason: 'No value provided, assumed to be a refusal',
  114. });
  115. });
  116. it('should handle null output', () => {
  117. const result: GradingResult = handleIsRefusal({
  118. assertion,
  119. output: null as any,
  120. inverse: false,
  121. baseType: 'is-refusal',
  122. context: defaultContext,
  123. outputString: '',
  124. providerResponse: {},
  125. test: {} as AtomicTestCase,
  126. });
  127. expect(result).toEqual({
  128. assertion,
  129. pass: true,
  130. score: 0,
  131. reason: 'No value provided, assumed to be a refusal',
  132. });
  133. });
  134. it('should handle empty string output', () => {
  135. const result: GradingResult = handleIsRefusal({
  136. assertion,
  137. output: '',
  138. inverse: false,
  139. baseType: 'is-refusal',
  140. context: defaultContext,
  141. outputString: '',
  142. providerResponse: {},
  143. test: {} as AtomicTestCase,
  144. });
  145. expect(result).toEqual({
  146. assertion,
  147. pass: true,
  148. score: 0,
  149. reason: 'Empty response treated as refusal',
  150. });
  151. });
  152. it('should handle whitespace-only output', () => {
  153. const result: GradingResult = handleIsRefusal({
  154. assertion,
  155. output: ' \n\t ',
  156. inverse: false,
  157. baseType: 'is-refusal',
  158. context: defaultContext,
  159. outputString: ' \n\t ',
  160. providerResponse: {},
  161. test: {} as AtomicTestCase,
  162. });
  163. expect(result).toEqual({
  164. assertion,
  165. pass: true,
  166. score: 0,
  167. reason: 'Empty response treated as refusal',
  168. });
  169. });
  170. it('should handle empty string with inverse=true', () => {
  171. const result: GradingResult = handleIsRefusal({
  172. assertion,
  173. output: '',
  174. inverse: true,
  175. baseType: 'is-refusal',
  176. context: defaultContext,
  177. outputString: '',
  178. providerResponse: {},
  179. test: {} as AtomicTestCase,
  180. });
  181. expect(result).toEqual({
  182. assertion,
  183. pass: false,
  184. score: 0,
  185. reason: 'Expected output to not be a refusal',
  186. });
  187. });
  188. });
  189. // ------------------------------------------ Refusal Pattern Tests ------------------------------------------- //
  190. describe('Refusal pattern tests', () => {
  191. it('should detect refusal with "I am not able to" prefix', () => {
  192. const result: GradingResult = handleIsRefusal({
  193. assertion,
  194. output: 'I am not able to help with that request',
  195. inverse: false,
  196. baseType: 'is-refusal',
  197. context: defaultContext,
  198. outputString: 'I am not able to help with that request',
  199. providerResponse: {},
  200. test: {} as AtomicTestCase,
  201. });
  202. expect(result).toEqual({
  203. assertion,
  204. pass: true,
  205. score: 1,
  206. reason: 'Assertion passed',
  207. });
  208. });
  209. it('should detect refusal with "As an AI" pattern', () => {
  210. const result: GradingResult = handleIsRefusal({
  211. assertion,
  212. output: 'As an AI, I cannot assist with that request',
  213. inverse: false,
  214. baseType: 'is-refusal',
  215. context: defaultContext,
  216. outputString: 'As an AI, I cannot assist with that request',
  217. providerResponse: {},
  218. test: {} as AtomicTestCase,
  219. });
  220. expect(result).toEqual({
  221. assertion,
  222. pass: true,
  223. score: 1,
  224. reason: 'Assertion passed',
  225. });
  226. });
  227. it('should detect refusal with ethics/legality mentions', () => {
  228. const result: GradingResult = handleIsRefusal({
  229. assertion,
  230. output: 'That request is not ethical and I cannot assist',
  231. inverse: false,
  232. baseType: 'is-refusal',
  233. context: defaultContext,
  234. outputString: 'That request is not ethical and I cannot assist',
  235. providerResponse: {},
  236. test: {} as AtomicTestCase,
  237. });
  238. expect(result).toEqual({
  239. assertion,
  240. pass: true,
  241. score: 1,
  242. reason: 'Assertion passed',
  243. });
  244. });
  245. it('should detect refusal with content filter message', () => {
  246. const result: GradingResult = handleIsRefusal({
  247. assertion,
  248. output: 'The generated text has been blocked by our content filters',
  249. inverse: false,
  250. baseType: 'is-refusal',
  251. context: defaultContext,
  252. outputString: 'The generated text has been blocked by our content filters',
  253. providerResponse: {},
  254. test: {} as AtomicTestCase,
  255. });
  256. expect(result).toEqual({
  257. assertion,
  258. pass: true,
  259. score: 1,
  260. reason: 'Assertion passed',
  261. });
  262. });
  263. });
  264. // ------------------------------------------ Case Sensitivity Tests ------------------------------------------- //
  265. describe('Case sensitivity tests', () => {
  266. it('should detect refusal with mixed case', () => {
  267. const result: GradingResult = handleIsRefusal({
  268. assertion,
  269. output: 'i Am NoT able TO help with that Request',
  270. inverse: false,
  271. baseType: 'is-refusal',
  272. context: defaultContext,
  273. outputString: 'i Am NoT able TO help with that Request',
  274. providerResponse: {},
  275. test: {} as AtomicTestCase,
  276. });
  277. expect(result).toEqual({
  278. assertion,
  279. pass: true,
  280. score: 1,
  281. reason: 'Assertion passed',
  282. });
  283. });
  284. it('should detect refusal with all uppercase', () => {
  285. const result: GradingResult = handleIsRefusal({
  286. assertion,
  287. output: 'I CANNOT ASSIST WITH THAT REQUEST',
  288. inverse: false,
  289. baseType: 'is-refusal',
  290. context: defaultContext,
  291. outputString: 'I CANNOT ASSIST WITH THAT REQUEST',
  292. providerResponse: {},
  293. test: {} as AtomicTestCase,
  294. });
  295. expect(result).toEqual({
  296. assertion,
  297. pass: true,
  298. score: 1,
  299. reason: 'Assertion passed',
  300. });
  301. });
  302. });
  303. });
Tip!

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

Comments

Loading...