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

guardrails.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
337
338
339
340
341
342
343
344
345
346
347
348
  1. import { fetchWithCache } from '../src/cache';
  2. import guardrails, { type AdaptiveRequest } from '../src/guardrails';
  3. jest.mock('../src/cache', () => ({
  4. fetchWithCache: jest.fn(),
  5. }));
  6. describe('guardrails', () => {
  7. const mockFetchResponse = {
  8. data: {
  9. model: 'test-model',
  10. results: [
  11. {
  12. categories: {
  13. test_category: true,
  14. },
  15. category_scores: {
  16. test_category: 0.95,
  17. },
  18. flagged: true,
  19. },
  20. ],
  21. },
  22. cached: false,
  23. status: 200,
  24. statusText: 'OK',
  25. };
  26. beforeEach(() => {
  27. jest.clearAllMocks();
  28. jest.mocked(fetchWithCache).mockResolvedValue(mockFetchResponse);
  29. });
  30. describe('guard', () => {
  31. it('should make a request to the guard endpoint', async () => {
  32. const input = 'test input';
  33. await guardrails.guard(input);
  34. expect(fetchWithCache).toHaveBeenCalledWith(
  35. expect.stringContaining('/v1/guard'),
  36. {
  37. method: 'POST',
  38. headers: {
  39. 'Content-Type': 'application/json',
  40. },
  41. body: JSON.stringify({ input }),
  42. },
  43. undefined,
  44. 'json',
  45. );
  46. });
  47. it('should return the parsed guard result', async () => {
  48. const result = await guardrails.guard('test input');
  49. expect(result).toEqual(mockFetchResponse.data);
  50. });
  51. it('should handle API errors', async () => {
  52. const errorMessage = 'API Error';
  53. jest.mocked(fetchWithCache).mockRejectedValue(new Error(errorMessage));
  54. await expect(guardrails.guard('test input')).rejects.toThrow(errorMessage);
  55. });
  56. it('should handle empty API response', async () => {
  57. jest.mocked(fetchWithCache).mockResolvedValue({
  58. data: null,
  59. cached: false,
  60. status: 200,
  61. statusText: 'OK',
  62. });
  63. await expect(guardrails.guard('test input')).rejects.toThrow('No data returned from API');
  64. });
  65. });
  66. describe('pii', () => {
  67. const mockPiiResponse = {
  68. data: {
  69. model: 'test-model',
  70. results: [
  71. {
  72. categories: {
  73. pii: true,
  74. },
  75. category_scores: {
  76. pii: 1,
  77. },
  78. flagged: true,
  79. payload: {
  80. pii: [
  81. {
  82. entity_type: 'EMAIL',
  83. start: 0,
  84. end: 17,
  85. pii: 'test@example.com',
  86. },
  87. ],
  88. },
  89. },
  90. ],
  91. },
  92. cached: false,
  93. status: 200,
  94. statusText: 'OK',
  95. };
  96. beforeEach(() => {
  97. jest.mocked(fetchWithCache).mockResolvedValue(mockPiiResponse);
  98. });
  99. it('should make a request to the pii endpoint', async () => {
  100. const input = 'test@example.com';
  101. await guardrails.pii(input);
  102. expect(fetchWithCache).toHaveBeenCalledWith(
  103. expect.stringContaining('/v1/pii'),
  104. {
  105. method: 'POST',
  106. headers: {
  107. 'Content-Type': 'application/json',
  108. },
  109. body: JSON.stringify({ input }),
  110. },
  111. undefined,
  112. 'json',
  113. );
  114. });
  115. it('should return the parsed PII result', async () => {
  116. const result = await guardrails.pii('test@example.com');
  117. expect(result).toEqual(mockPiiResponse.data);
  118. expect(result.results[0].payload?.pii?.[0].entity_type).toBe('EMAIL');
  119. });
  120. it('should handle API errors', async () => {
  121. const errorMessage = 'API Error';
  122. jest.mocked(fetchWithCache).mockRejectedValue(new Error(errorMessage));
  123. await expect(guardrails.pii('test input')).rejects.toThrow(errorMessage);
  124. });
  125. });
  126. describe('harm', () => {
  127. const mockHarmResponse = {
  128. data: {
  129. model: 'test-model',
  130. results: [
  131. {
  132. categories: {
  133. hate: true,
  134. violent_crimes: false,
  135. },
  136. category_scores: {
  137. hate: 0.95,
  138. violent_crimes: 0.1,
  139. },
  140. flagged: true,
  141. },
  142. ],
  143. },
  144. cached: false,
  145. status: 200,
  146. statusText: 'OK',
  147. };
  148. beforeEach(() => {
  149. jest.mocked(fetchWithCache).mockResolvedValue(mockHarmResponse);
  150. });
  151. it('should make a request to the harm endpoint', async () => {
  152. const input = 'test input';
  153. await guardrails.harm(input);
  154. expect(fetchWithCache).toHaveBeenCalledWith(
  155. expect.stringContaining('/v1/harm'),
  156. {
  157. method: 'POST',
  158. headers: {
  159. 'Content-Type': 'application/json',
  160. },
  161. body: JSON.stringify({ input }),
  162. },
  163. undefined,
  164. 'json',
  165. );
  166. });
  167. it('should return the parsed harm result', async () => {
  168. const result = await guardrails.harm('test input');
  169. expect(result).toEqual(mockHarmResponse.data);
  170. expect(result.results[0].categories.hate).toBe(true);
  171. expect(result.results[0].category_scores.hate).toBe(0.95);
  172. });
  173. it('should handle API errors', async () => {
  174. const errorMessage = 'API Error';
  175. jest.mocked(fetchWithCache).mockRejectedValue(new Error(errorMessage));
  176. await expect(guardrails.harm('test input')).rejects.toThrow(errorMessage);
  177. });
  178. });
  179. describe('response structure', () => {
  180. it('should have correct guard result structure', async () => {
  181. const result = await guardrails.guard('test input');
  182. expect(result).toHaveProperty('model');
  183. expect(result).toHaveProperty('results');
  184. expect(Array.isArray(result.results)).toBe(true);
  185. expect(result.results[0]).toHaveProperty('categories');
  186. expect(result.results[0]).toHaveProperty('category_scores');
  187. expect(result.results[0]).toHaveProperty('flagged');
  188. expect(typeof result.results[0].flagged).toBe('boolean');
  189. });
  190. it('should have correct PII result structure with payload', async () => {
  191. jest.mocked(fetchWithCache).mockResolvedValue({
  192. data: {
  193. model: 'test-model',
  194. results: [
  195. {
  196. categories: {
  197. pii: true,
  198. },
  199. category_scores: {
  200. pii: 1,
  201. },
  202. flagged: true,
  203. payload: {
  204. pii: [
  205. {
  206. entity_type: 'EMAIL',
  207. start: 0,
  208. end: 17,
  209. pii: 'test@example.com',
  210. },
  211. ],
  212. },
  213. },
  214. ],
  215. },
  216. cached: false,
  217. status: 200,
  218. statusText: 'OK',
  219. });
  220. const result = await guardrails.pii('test input');
  221. expect(result).toHaveProperty('model');
  222. expect(result).toHaveProperty('results');
  223. expect(Array.isArray(result.results)).toBe(true);
  224. expect(result.results[0]).toHaveProperty('payload');
  225. expect(Array.isArray(result.results[0].payload?.pii)).toBe(true);
  226. const piiEntity = result.results[0].payload?.pii?.[0];
  227. expect(piiEntity).toBeDefined();
  228. expect(piiEntity).toEqual(
  229. expect.objectContaining({
  230. entity_type: expect.any(String),
  231. start: expect.any(Number),
  232. end: expect.any(Number),
  233. pii: expect.any(String),
  234. }),
  235. );
  236. });
  237. });
  238. describe('adaptive function', () => {
  239. it('should call fetchWithCache with correct parameters', async () => {
  240. const mockResponse = {
  241. data: {
  242. model: 'promptfoo-adaptive-prompt',
  243. adaptedPrompt: 'Adapted test input',
  244. modifications: [
  245. {
  246. type: 'substitution',
  247. reason: 'Policy compliance',
  248. original: 'test input',
  249. modified: 'Adapted test input',
  250. },
  251. ],
  252. },
  253. cached: false,
  254. status: 200,
  255. statusText: 'OK',
  256. };
  257. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  258. const request: AdaptiveRequest = {
  259. prompt: 'test input',
  260. policies: ['No harmful content'],
  261. };
  262. const result = await guardrails.adaptive(request);
  263. expect(fetchWithCache).toHaveBeenCalledWith(
  264. 'https://api.promptfoo.app/v1/adaptive',
  265. {
  266. method: 'POST',
  267. headers: {
  268. 'Content-Type': 'application/json',
  269. },
  270. body: JSON.stringify({
  271. prompt: 'test input',
  272. policies: ['No harmful content'],
  273. }),
  274. },
  275. undefined,
  276. 'json',
  277. );
  278. expect(result).toEqual(mockResponse.data);
  279. });
  280. it('should handle missing policies parameter', async () => {
  281. const mockResponse = {
  282. data: {
  283. model: 'promptfoo-adaptive-prompt',
  284. adaptedPrompt: 'Adapted test input',
  285. modifications: [],
  286. },
  287. cached: false,
  288. status: 200,
  289. statusText: 'OK',
  290. };
  291. jest.mocked(fetchWithCache).mockResolvedValue(mockResponse);
  292. const request: AdaptiveRequest = {
  293. prompt: 'test input',
  294. };
  295. const result = await guardrails.adaptive(request);
  296. expect(fetchWithCache).toHaveBeenCalledWith(
  297. 'https://api.promptfoo.app/v1/adaptive',
  298. {
  299. method: 'POST',
  300. headers: {
  301. 'Content-Type': 'application/json',
  302. },
  303. body: JSON.stringify({
  304. prompt: 'test input',
  305. policies: [],
  306. }),
  307. },
  308. undefined,
  309. 'json',
  310. );
  311. expect(result).toEqual(mockResponse.data);
  312. });
  313. });
  314. });
Tip!

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

Comments

Loading...