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

contextFaithfulness.test.ts 8.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
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
  1. import { handleContextFaithfulness } from '../../src/assertions/contextFaithfulness';
  2. import * as contextUtils from '../../src/assertions/contextUtils';
  3. import * as matchers from '../../src/matchers';
  4. jest.mock('../../src/matchers');
  5. jest.mock('../../src/assertions/contextUtils');
  6. describe('handleContextFaithfulness', () => {
  7. beforeEach(() => {
  8. jest.clearAllMocks();
  9. });
  10. it('should pass when context faithfulness is above threshold', async () => {
  11. const mockResult = { pass: true, score: 0.9, reason: 'Content is faithful to context' };
  12. jest.mocked(matchers.matchesContextFaithfulness).mockResolvedValue(mockResult);
  13. jest.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
  14. const result = await handleContextFaithfulness({
  15. assertion: {
  16. type: 'context-faithfulness',
  17. threshold: 0.7,
  18. },
  19. test: {
  20. vars: {
  21. query: 'What is the capital of France?',
  22. context: 'Paris is the capital of France.',
  23. },
  24. options: {},
  25. },
  26. output: 'The capital of France is Paris.',
  27. prompt: 'test prompt',
  28. baseType: 'context-faithfulness',
  29. context: {
  30. prompt: 'test prompt',
  31. vars: {
  32. query: 'What is the capital of France?',
  33. context: 'Paris is the capital of France.',
  34. },
  35. test: {
  36. vars: {
  37. query: 'What is the capital of France?',
  38. context: 'Paris is the capital of France.',
  39. },
  40. options: {},
  41. },
  42. logProbs: null,
  43. tokenUsage: null,
  44. cached: false,
  45. provider: null,
  46. providerResponse: null,
  47. },
  48. inverse: false,
  49. outputString: 'The capital of France is Paris.',
  50. providerResponse: null,
  51. } as any);
  52. expect(result.pass).toBe(true);
  53. expect(result.score).toBe(0.9);
  54. expect(result.reason).toBe('Content is faithful to context');
  55. expect(result.metadata).toBeDefined();
  56. expect(result.metadata!.context).toBe('test context');
  57. expect(matchers.matchesContextFaithfulness).toHaveBeenCalledWith(
  58. 'What is the capital of France?',
  59. 'The capital of France is Paris.',
  60. 'test context',
  61. 0.7,
  62. {},
  63. );
  64. });
  65. it('should fail when context faithfulness is below threshold', async () => {
  66. const mockResult = { pass: false, score: 0.3, reason: 'Content contains hallucinations' };
  67. jest.mocked(matchers.matchesContextFaithfulness).mockResolvedValue(mockResult);
  68. jest.mocked(contextUtils.resolveContext).mockResolvedValue('test context');
  69. const result = await handleContextFaithfulness({
  70. assertion: {
  71. type: 'context-faithfulness',
  72. threshold: 0.7,
  73. },
  74. test: {
  75. vars: {
  76. query: 'What is the capital of France?',
  77. context: 'Paris is the capital of France.',
  78. },
  79. options: {},
  80. },
  81. output: 'The capital of France is Paris and it has a population of 50 million.',
  82. prompt: 'test prompt',
  83. baseType: 'context-faithfulness',
  84. context: {
  85. prompt: 'test prompt',
  86. vars: {
  87. query: 'What is the capital of France?',
  88. context: 'Paris is the capital of France.',
  89. },
  90. test: {
  91. vars: {
  92. query: 'What is the capital of France?',
  93. context: 'Paris is the capital of France.',
  94. },
  95. options: {},
  96. },
  97. logProbs: null,
  98. tokenUsage: null,
  99. cached: false,
  100. provider: null,
  101. providerResponse: null,
  102. },
  103. inverse: false,
  104. outputString: 'The capital of France is Paris and it has a population of 50 million.',
  105. providerResponse: null,
  106. } as any);
  107. expect(result.pass).toBe(false);
  108. expect(result.score).toBe(0.3);
  109. expect(result.reason).toBe('Content contains hallucinations');
  110. expect(result.metadata).toBeDefined();
  111. expect(result.metadata!.context).toBe('test context');
  112. expect(matchers.matchesContextFaithfulness).toHaveBeenCalledWith(
  113. 'What is the capital of France?',
  114. 'The capital of France is Paris and it has a population of 50 million.',
  115. 'test context',
  116. 0.7,
  117. {},
  118. );
  119. });
  120. it('should throw error when test.vars is undefined', async () => {
  121. await expect(
  122. handleContextFaithfulness({
  123. assertion: { type: 'context-faithfulness' },
  124. test: {
  125. vars: undefined,
  126. options: {},
  127. },
  128. output: 'test output',
  129. prompt: 'test prompt',
  130. baseType: 'context-faithfulness',
  131. context: {
  132. prompt: 'test prompt',
  133. vars: {},
  134. test: { vars: undefined, options: {} },
  135. logProbs: null,
  136. tokenUsage: null,
  137. cached: false,
  138. provider: null,
  139. providerResponse: null,
  140. },
  141. inverse: false,
  142. outputString: 'test output',
  143. providerResponse: null,
  144. } as any),
  145. ).rejects.toThrow('context-faithfulness assertion requires a test with variables');
  146. });
  147. it('should throw error when query is not a string', async () => {
  148. await expect(
  149. handleContextFaithfulness({
  150. assertion: { type: 'context-faithfulness' },
  151. test: {
  152. vars: {
  153. query: 123,
  154. },
  155. options: {},
  156. },
  157. output: 'test output',
  158. prompt: 'test prompt',
  159. baseType: 'context-faithfulness',
  160. context: {
  161. prompt: 'test prompt',
  162. vars: { query: 123 },
  163. test: { vars: { query: 123 }, options: {} },
  164. logProbs: null,
  165. tokenUsage: null,
  166. cached: false,
  167. provider: null,
  168. providerResponse: null,
  169. },
  170. inverse: false,
  171. outputString: 'test output',
  172. providerResponse: null,
  173. } as any),
  174. ).rejects.toThrow(
  175. 'context-faithfulness assertion requires a "query" variable with the user question',
  176. );
  177. });
  178. it('should throw error when output is not a string', async () => {
  179. await expect(
  180. handleContextFaithfulness({
  181. assertion: { type: 'context-faithfulness' },
  182. test: {
  183. vars: {
  184. query: 'test query',
  185. },
  186. options: {},
  187. },
  188. output: 123,
  189. prompt: 'test prompt',
  190. baseType: 'context-faithfulness',
  191. context: {
  192. prompt: 'test prompt',
  193. vars: { query: 'test query' },
  194. test: { vars: { query: 'test query' }, options: {} },
  195. logProbs: null,
  196. tokenUsage: null,
  197. cached: false,
  198. provider: null,
  199. providerResponse: null,
  200. },
  201. inverse: false,
  202. outputString: '123',
  203. providerResponse: null,
  204. } as any),
  205. ).rejects.toThrow('context-faithfulness assertion requires string output from the provider');
  206. });
  207. it('should use contextTransform to extract context', async () => {
  208. const mockResult = { pass: true, score: 1, reason: 'ok' };
  209. jest.mocked(matchers.matchesContextFaithfulness).mockResolvedValue(mockResult);
  210. jest.mocked(contextUtils.resolveContext).mockResolvedValue('from-transform');
  211. const params = {
  212. assertion: {
  213. type: 'context-faithfulness' as const,
  214. contextTransform: 'output.context',
  215. },
  216. test: {
  217. vars: {
  218. query: 'test query',
  219. },
  220. options: {},
  221. },
  222. output: 'raw',
  223. prompt: 'prompt text',
  224. baseType: 'context-faithfulness' as const,
  225. context: {
  226. prompt: 'prompt text',
  227. vars: {},
  228. test: {},
  229. logProbs: null,
  230. tokenUsage: null,
  231. cached: false,
  232. provider: null,
  233. providerResponse: null,
  234. },
  235. inverse: false,
  236. outputString: 'raw',
  237. providerResponse: null,
  238. } as any;
  239. const result = await handleContextFaithfulness(params);
  240. expect(contextUtils.resolveContext).toHaveBeenCalledWith(
  241. params.assertion,
  242. params.test,
  243. 'raw',
  244. 'prompt text',
  245. undefined,
  246. null,
  247. );
  248. expect(matchers.matchesContextFaithfulness).toHaveBeenCalledWith(
  249. 'test query',
  250. 'raw',
  251. 'from-transform',
  252. 0,
  253. {},
  254. );
  255. expect(result.metadata).toBeDefined();
  256. expect(result.metadata!.context).toBe('from-transform');
  257. });
  258. });
Tip!

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

Comments

Loading...