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

matchers.test.ts 8.9 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
  1. import { matchesConversationRelevance } from '../../src/external/matchers/deepeval';
  2. import { getDefaultProviders } from '../../src/providers/defaults';
  3. // Mock the text provider
  4. jest.mock('../../src/providers/defaults', () => ({
  5. getDefaultProviders: jest.fn(),
  6. }));
  7. describe('matchesConversationRelevance', () => {
  8. beforeEach(() => {
  9. jest.clearAllMocks();
  10. });
  11. it('should return pass=true for relevant responses', async () => {
  12. const mockProvider = {
  13. id: () => 'mock-provider',
  14. callApi: jest.fn().mockResolvedValue({
  15. output: JSON.stringify({
  16. verdict: 'yes',
  17. }),
  18. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  19. }),
  20. };
  21. jest.mocked(getDefaultProviders).mockResolvedValue({
  22. embeddingProvider: mockProvider,
  23. gradingJsonProvider: mockProvider,
  24. gradingProvider: mockProvider,
  25. llmRubricProvider: mockProvider,
  26. moderationProvider: mockProvider,
  27. suggestionsProvider: mockProvider,
  28. synthesizeProvider: mockProvider,
  29. });
  30. const messages = [
  31. {
  32. input: 'What is the capital of France?',
  33. output: 'The capital of France is Paris.',
  34. },
  35. ];
  36. const result = await matchesConversationRelevance(messages, 0.5);
  37. expect(result.pass).toBe(true);
  38. expect(result.score).toBe(1);
  39. expect(result.tokensUsed).toEqual({
  40. total: 10,
  41. prompt: 5,
  42. completion: 5,
  43. cached: 0,
  44. });
  45. });
  46. it('should return pass=false for irrelevant responses', async () => {
  47. const mockProvider = {
  48. id: () => 'mock-provider',
  49. callApi: jest.fn().mockResolvedValue({
  50. output: JSON.stringify({
  51. verdict: 'no',
  52. reason: 'Response is completely unrelated to the question',
  53. }),
  54. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  55. }),
  56. };
  57. jest.mocked(getDefaultProviders).mockResolvedValue({
  58. embeddingProvider: mockProvider,
  59. gradingJsonProvider: mockProvider,
  60. gradingProvider: mockProvider,
  61. llmRubricProvider: mockProvider,
  62. moderationProvider: mockProvider,
  63. suggestionsProvider: mockProvider,
  64. synthesizeProvider: mockProvider,
  65. });
  66. const messages = [
  67. {
  68. input: 'What is the capital of France?',
  69. output: 'The weather is nice today.',
  70. },
  71. ];
  72. const result = await matchesConversationRelevance(messages, 0.5);
  73. expect(result.pass).toBe(false);
  74. expect(result.score).toBe(0);
  75. expect(result.reason).toBe('Response is completely unrelated to the question');
  76. });
  77. it('should consider conversation context', async () => {
  78. const messages = [
  79. {
  80. input: 'What is the capital of France?',
  81. output: 'The capital of France is Paris.',
  82. },
  83. {
  84. input: 'What is its population?',
  85. output: 'Paris has a population of about 2.2 million people.',
  86. },
  87. ];
  88. const mockProvider = {
  89. id: () => 'mock-provider',
  90. callApi: jest.fn().mockResolvedValue({
  91. output: JSON.stringify({
  92. verdict: 'yes',
  93. reason:
  94. 'Response directly answers the population question with context from previous message about Paris',
  95. }),
  96. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  97. }),
  98. };
  99. jest.mocked(getDefaultProviders).mockResolvedValue({
  100. embeddingProvider: mockProvider,
  101. gradingJsonProvider: mockProvider,
  102. gradingProvider: mockProvider,
  103. llmRubricProvider: mockProvider,
  104. moderationProvider: mockProvider,
  105. suggestionsProvider: mockProvider,
  106. synthesizeProvider: mockProvider,
  107. });
  108. const result = await matchesConversationRelevance(messages, 0.5);
  109. expect(result.pass).toBe(true);
  110. // Check that both messages appear in the prompt
  111. const prompt = mockProvider.callApi.mock.calls[0][0];
  112. expect(prompt).toContain('What is the capital of France?');
  113. expect(prompt).toContain('What is its population?');
  114. });
  115. it('should handle provider errors', async () => {
  116. const mockProvider = {
  117. id: () => 'mock-provider',
  118. callApi: jest.fn().mockResolvedValue({
  119. error: 'API error',
  120. tokenUsage: { total: 5, prompt: 5, completion: 0, cached: 0 },
  121. }),
  122. };
  123. jest.mocked(getDefaultProviders).mockResolvedValue({
  124. embeddingProvider: mockProvider,
  125. gradingJsonProvider: mockProvider,
  126. gradingProvider: mockProvider,
  127. llmRubricProvider: mockProvider,
  128. moderationProvider: mockProvider,
  129. suggestionsProvider: mockProvider,
  130. synthesizeProvider: mockProvider,
  131. });
  132. const messages = [
  133. {
  134. input: 'What is the capital of France?',
  135. output: 'Paris',
  136. },
  137. ];
  138. const result = await matchesConversationRelevance(messages, 0.5);
  139. expect(result.pass).toBe(false);
  140. expect(result.reason).toBe('API error');
  141. expect(result.tokensUsed).toMatchObject({
  142. total: 5,
  143. prompt: 5,
  144. completion: 0,
  145. cached: 0,
  146. });
  147. });
  148. it('should handle malformed provider responses', async () => {
  149. const mockProvider = {
  150. id: () => 'mock-provider',
  151. callApi: jest.fn().mockResolvedValue({
  152. output: 'not json',
  153. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  154. }),
  155. };
  156. jest.mocked(getDefaultProviders).mockResolvedValue({
  157. embeddingProvider: mockProvider,
  158. gradingJsonProvider: mockProvider,
  159. gradingProvider: mockProvider,
  160. llmRubricProvider: mockProvider,
  161. moderationProvider: mockProvider,
  162. suggestionsProvider: mockProvider,
  163. synthesizeProvider: mockProvider,
  164. });
  165. const messages = [
  166. {
  167. input: 'What is the capital of France?',
  168. output: 'Paris',
  169. },
  170. ];
  171. const result = await matchesConversationRelevance(messages, 0.5);
  172. expect(result.pass).toBe(false);
  173. expect(result.reason).toMatch(/Error parsing output/);
  174. });
  175. it('should use custom rubric prompt when provided', async () => {
  176. const mockProvider = {
  177. id: () => 'mock-provider',
  178. callApi: jest.fn().mockResolvedValue({
  179. output: JSON.stringify({
  180. verdict: 'yes',
  181. }),
  182. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  183. }),
  184. };
  185. jest.mocked(getDefaultProviders).mockResolvedValue({
  186. embeddingProvider: mockProvider,
  187. gradingJsonProvider: mockProvider,
  188. gradingProvider: mockProvider,
  189. llmRubricProvider: mockProvider,
  190. moderationProvider: mockProvider,
  191. suggestionsProvider: mockProvider,
  192. synthesizeProvider: mockProvider,
  193. });
  194. const messages = [
  195. {
  196. input: 'What is the capital of France?',
  197. output: 'Paris',
  198. },
  199. ];
  200. const customRubric = 'Custom rubric template with {{ messages | dump(2) }}';
  201. await matchesConversationRelevance(messages, 0.5, { messages }, { rubricPrompt: customRubric });
  202. const prompt = mockProvider.callApi.mock.calls[0][0];
  203. expect(prompt).toBe('Custom rubric template with ' + JSON.stringify(messages, null, 2));
  204. });
  205. it('should handle custom variables', async () => {
  206. const mockProvider = {
  207. id: () => 'mock-provider',
  208. callApi: jest.fn().mockResolvedValue({
  209. output: JSON.stringify({
  210. verdict: 'yes',
  211. }),
  212. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  213. }),
  214. };
  215. jest.mocked(getDefaultProviders).mockResolvedValue({
  216. embeddingProvider: mockProvider,
  217. gradingJsonProvider: mockProvider,
  218. gradingProvider: mockProvider,
  219. llmRubricProvider: mockProvider,
  220. moderationProvider: mockProvider,
  221. suggestionsProvider: mockProvider,
  222. synthesizeProvider: mockProvider,
  223. });
  224. const messages = [
  225. {
  226. input: 'What is the capital of {{country}}?',
  227. output: '{{capital}}',
  228. },
  229. ];
  230. const vars = {
  231. country: 'France',
  232. capital: 'Paris',
  233. };
  234. await matchesConversationRelevance(messages, 0.5, vars);
  235. const prompt = mockProvider.callApi.mock.calls[0][0];
  236. expect(prompt).toContain('What is the capital of France?');
  237. expect(prompt).toContain('Paris');
  238. });
  239. it('should handle markdown-formatted JSON responses', async () => {
  240. const mockProvider = {
  241. id: () => 'mock-provider',
  242. callApi: jest.fn().mockResolvedValue({
  243. output: '```json\n{"verdict": "yes"}\n```',
  244. tokenUsage: { total: 10, prompt: 5, completion: 5, cached: 0 },
  245. }),
  246. };
  247. jest.mocked(getDefaultProviders).mockResolvedValue({
  248. embeddingProvider: mockProvider,
  249. gradingJsonProvider: mockProvider,
  250. gradingProvider: mockProvider,
  251. llmRubricProvider: mockProvider,
  252. moderationProvider: mockProvider,
  253. suggestionsProvider: mockProvider,
  254. synthesizeProvider: mockProvider,
  255. });
  256. const messages = [
  257. {
  258. input: 'What is the capital of France?',
  259. output: 'Paris',
  260. },
  261. ];
  262. const result = await matchesConversationRelevance(messages, 0.5);
  263. expect(result.pass).toBe(true);
  264. expect(result.score).toBe(1);
  265. });
  266. });
Tip!

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

Comments

Loading...