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

llmRubric.test.ts 10 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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
  1. import { handleLlmRubric } from '../../src/assertions/llmRubric';
  2. import { matchesLlmRubric } from '../../src/matchers';
  3. import type { Assertion, AssertionParams, GradingResult } from '../../src/types';
  4. jest.mock('../../src/matchers');
  5. describe('handleLlmRubric', () => {
  6. beforeEach(() => {
  7. jest.resetAllMocks();
  8. });
  9. const mockMatchesLlmRubric = jest.mocked(matchesLlmRubric);
  10. const defaultParams: AssertionParams = {
  11. assertion: {
  12. type: 'llm-rubric',
  13. value: 'test rubric',
  14. } as Assertion,
  15. baseType: 'llm-rubric',
  16. context: {
  17. prompt: 'test prompt',
  18. vars: {},
  19. test: {
  20. vars: {},
  21. },
  22. logProbs: undefined,
  23. provider: undefined,
  24. providerResponse: undefined,
  25. },
  26. inverse: false,
  27. output: 'test output',
  28. outputString: 'test output string',
  29. test: {
  30. vars: {},
  31. },
  32. providerResponse: {},
  33. };
  34. it('should handle string rendered value', async () => {
  35. const params = {
  36. ...defaultParams,
  37. renderedValue: 'test rendered value',
  38. };
  39. const expectedResult: GradingResult = {
  40. pass: true,
  41. score: 1,
  42. reason: 'test reason',
  43. };
  44. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  45. const result = await handleLlmRubric(params);
  46. expect(result).toEqual(expectedResult);
  47. expect(mockMatchesLlmRubric).toHaveBeenCalledWith(
  48. 'test rendered value',
  49. 'test output string',
  50. undefined,
  51. {},
  52. params.assertion,
  53. );
  54. });
  55. it('should handle object rendered value', async () => {
  56. const params = {
  57. ...defaultParams,
  58. renderedValue: { test: 'value' },
  59. };
  60. const expectedResult: GradingResult = {
  61. pass: true,
  62. score: 1,
  63. reason: 'test reason',
  64. };
  65. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  66. const result = await handleLlmRubric(params);
  67. expect(result).toEqual(expectedResult);
  68. expect(mockMatchesLlmRubric).toHaveBeenCalledWith(
  69. { test: 'value' },
  70. 'test output string',
  71. undefined,
  72. {},
  73. params.assertion,
  74. );
  75. });
  76. it('should handle undefined rendered value', async () => {
  77. const params = {
  78. ...defaultParams,
  79. renderedValue: undefined,
  80. };
  81. const expectedResult: GradingResult = {
  82. pass: true,
  83. score: 1,
  84. reason: 'test reason',
  85. };
  86. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  87. const result = await handleLlmRubric(params);
  88. expect(result).toEqual(expectedResult);
  89. expect(mockMatchesLlmRubric).toHaveBeenCalledWith(
  90. '',
  91. 'test output string',
  92. undefined,
  93. {},
  94. params.assertion,
  95. );
  96. });
  97. it('should stringify object rubricPrompt', async () => {
  98. const params: AssertionParams = {
  99. ...defaultParams,
  100. test: {
  101. vars: {},
  102. // Using a valid structure for rubricPrompt as per type definition
  103. options: {
  104. rubricPrompt: [{ role: 'system', content: 'test prompt' }],
  105. },
  106. },
  107. };
  108. const expectedResult: GradingResult = {
  109. pass: true,
  110. score: 1,
  111. reason: 'test reason',
  112. };
  113. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  114. const result = await handleLlmRubric(params);
  115. expect(result).toEqual(expectedResult);
  116. expect(params.test.options?.rubricPrompt).toBe(
  117. JSON.stringify([{ role: 'system', content: 'test prompt' }]),
  118. );
  119. });
  120. it('should use assertion.value if present, otherwise use test.options.rubricPrompt', async () => {
  121. const params: AssertionParams = {
  122. ...defaultParams,
  123. assertion: {
  124. type: 'llm-rubric',
  125. value: undefined,
  126. } as Assertion,
  127. test: {
  128. vars: {},
  129. options: {
  130. rubricPrompt: 'rubric from options',
  131. },
  132. },
  133. renderedValue: undefined,
  134. };
  135. const expectedResult: GradingResult = {
  136. pass: true,
  137. score: 2,
  138. reason: 'used options rubric',
  139. };
  140. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  141. const result = await handleLlmRubric(params);
  142. expect(result).toEqual(expectedResult);
  143. expect(params.assertion.value).toBe('rubric from options');
  144. });
  145. it('should not overwrite assertion.value if already set', async () => {
  146. const params: AssertionParams = {
  147. ...defaultParams,
  148. assertion: {
  149. type: 'llm-rubric',
  150. value: 'already set',
  151. } as Assertion,
  152. test: {
  153. vars: {},
  154. options: {
  155. rubricPrompt: 'rubric from options',
  156. },
  157. },
  158. renderedValue: undefined,
  159. };
  160. const expectedResult: GradingResult = {
  161. pass: false,
  162. score: 0,
  163. reason: 'assertion.value was set',
  164. };
  165. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  166. const result = await handleLlmRubric(params);
  167. expect(result).toEqual(expectedResult);
  168. expect(params.assertion.value).toBe('already set');
  169. });
  170. it('should throw error for invalid rendered value type', async () => {
  171. // purposely passing an invalid type
  172. const params = {
  173. ...defaultParams,
  174. renderedValue: 123 as unknown as string,
  175. };
  176. await expect(() => handleLlmRubric(params)).toThrow(
  177. 'Invariant failed: "llm-rubric" assertion type must have a string or object value',
  178. );
  179. });
  180. it('should stringify rubricPrompt if it is an object (not stringified yet)', async () => {
  181. const rubricPromptObj = [{ role: 'user', content: 'bar' }];
  182. const params: AssertionParams = {
  183. ...defaultParams,
  184. test: {
  185. vars: {},
  186. options: {
  187. rubricPrompt: rubricPromptObj,
  188. },
  189. },
  190. renderedValue: undefined,
  191. };
  192. const expectedResult: GradingResult = {
  193. pass: true,
  194. score: 1,
  195. reason: 'rubricPrompt object stringified',
  196. };
  197. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  198. await handleLlmRubric(params);
  199. expect(params.test.options?.rubricPrompt).toBe(JSON.stringify(rubricPromptObj));
  200. });
  201. it('should not re-stringify rubricPrompt if it is already a string', async () => {
  202. const rubricPromptStr = '[{"role":"system","content":"already stringified"}]';
  203. const params: AssertionParams = {
  204. ...defaultParams,
  205. test: {
  206. vars: {},
  207. options: {
  208. rubricPrompt: rubricPromptStr,
  209. },
  210. },
  211. renderedValue: undefined,
  212. };
  213. const expectedResult: GradingResult = {
  214. pass: true,
  215. score: 1,
  216. reason: 'rubricPrompt already stringified',
  217. };
  218. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  219. await handleLlmRubric(params);
  220. expect(params.test.options?.rubricPrompt).toBe(rubricPromptStr);
  221. });
  222. it('should work if test.options is undefined', async () => {
  223. const params: AssertionParams = {
  224. ...defaultParams,
  225. test: {
  226. vars: {},
  227. // options is undefined
  228. },
  229. renderedValue: undefined,
  230. };
  231. const expectedResult: GradingResult = {
  232. pass: true,
  233. score: 1,
  234. reason: 'no options',
  235. };
  236. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  237. const result = await handleLlmRubric(params);
  238. expect(result).toEqual(expectedResult);
  239. });
  240. // Additional edge case: rubricPrompt is an empty object
  241. it('should stringify rubricPrompt if it is an empty object', async () => {
  242. // rubricPrompt as empty array of objects (valid for type)
  243. const rubricPromptObj: { role: string; content: string }[] = [];
  244. const params: AssertionParams = {
  245. ...defaultParams,
  246. test: {
  247. vars: {},
  248. options: {
  249. rubricPrompt: rubricPromptObj,
  250. },
  251. },
  252. renderedValue: undefined,
  253. };
  254. const expectedResult: GradingResult = {
  255. pass: true,
  256. score: 1,
  257. reason: 'rubricPrompt empty object stringified',
  258. };
  259. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  260. await handleLlmRubric(params);
  261. expect(params.test.options?.rubricPrompt).toBe(JSON.stringify(rubricPromptObj));
  262. });
  263. // Additional: assertion.value and test.options.rubricPrompt are both undefined
  264. it('should set assertion.value to undefined if both assertion.value and test.options.rubricPrompt are undefined', async () => {
  265. const params: AssertionParams = {
  266. ...defaultParams,
  267. assertion: {
  268. type: 'llm-rubric',
  269. value: undefined,
  270. } as Assertion,
  271. test: {
  272. vars: {},
  273. // options is undefined
  274. },
  275. renderedValue: undefined,
  276. };
  277. const expectedResult: GradingResult = {
  278. pass: true,
  279. score: 3,
  280. reason: 'assertion.value and rubricPrompt undefined',
  281. };
  282. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  283. const result = await handleLlmRubric(params);
  284. expect(result).toEqual(expectedResult);
  285. expect(params.assertion.value).toBeUndefined();
  286. });
  287. // New test: rubricPrompt is a plain object (not an array), should stringify as object
  288. it('should stringify rubricPrompt if it is a plain object (not an array)', async () => {
  289. const rubricPromptObj: any = { foo: 'bar', baz: 3 };
  290. const params: AssertionParams = {
  291. ...defaultParams,
  292. test: {
  293. vars: {},
  294. options: {
  295. rubricPrompt: rubricPromptObj,
  296. },
  297. },
  298. renderedValue: undefined,
  299. };
  300. const expectedResult: GradingResult = {
  301. pass: true,
  302. score: 1,
  303. reason: 'rubricPrompt plain object stringified',
  304. };
  305. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  306. await handleLlmRubric(params);
  307. expect(params.test.options?.rubricPrompt).toBe(JSON.stringify(rubricPromptObj));
  308. });
  309. // New test: renderedValue is an array (should be allowed, since typeof [] === 'object')
  310. it('should handle renderedValue as an array', async () => {
  311. const params: AssertionParams = {
  312. ...defaultParams,
  313. renderedValue: ['foo', 'bar'] as unknown as string,
  314. };
  315. const expectedResult: GradingResult = {
  316. pass: true,
  317. score: 4,
  318. reason: 'renderedValue is array',
  319. };
  320. mockMatchesLlmRubric.mockResolvedValue(expectedResult);
  321. const result = await handleLlmRubric(params);
  322. expect(result).toEqual(expectedResult);
  323. expect(mockMatchesLlmRubric).toHaveBeenCalledWith(
  324. ['foo', 'bar'],
  325. 'test output string',
  326. undefined,
  327. {},
  328. params.assertion,
  329. );
  330. });
  331. });
Tip!

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

Comments

Loading...