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

context-recall.test.ts 4.7 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
  1. import { matchesContextRecall } from '../../src/matchers';
  2. import { DefaultGradingProvider } from '../../src/providers/openai/defaults';
  3. describe('matchesContextRecall', () => {
  4. beforeEach(() => {
  5. jest.clearAllMocks();
  6. jest.resetAllMocks();
  7. jest.spyOn(DefaultGradingProvider, 'callApi').mockReset();
  8. jest.spyOn(DefaultGradingProvider, 'callApi').mockResolvedValue({
  9. output: 'foo [Attributed]\nbar [Not attributed]\nbaz [Attributed]\n',
  10. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  11. });
  12. });
  13. afterEach(() => {
  14. jest.restoreAllMocks();
  15. });
  16. it('should pass when the recall score is above the threshold', async () => {
  17. const context = 'Context text';
  18. const groundTruth = 'Ground truth text';
  19. const threshold = 0.5;
  20. const mockCallApi = jest.fn().mockImplementation(() => {
  21. return Promise.resolve({
  22. output: 'foo [Attributed]\nbar [Not attributed]\nbaz [Attributed]\n',
  23. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  24. });
  25. });
  26. jest.spyOn(DefaultGradingProvider, 'callApi').mockImplementation(mockCallApi);
  27. await expect(matchesContextRecall(context, groundTruth, threshold)).resolves.toEqual({
  28. pass: true,
  29. reason: 'Recall 0.67 is >= 0.5',
  30. score: expect.closeTo(0.67, 0.01),
  31. tokensUsed: {
  32. total: expect.any(Number),
  33. prompt: expect.any(Number),
  34. completion: expect.any(Number),
  35. cached: expect.any(Number),
  36. completionDetails: expect.any(Object),
  37. numRequests: 0,
  38. },
  39. metadata: {
  40. sentenceAttributions: expect.arrayContaining([
  41. expect.objectContaining({
  42. sentence: expect.any(String),
  43. attributed: expect.any(Boolean),
  44. }),
  45. ]),
  46. totalSentences: 3,
  47. attributedSentences: 2,
  48. score: expect.closeTo(0.67, 0.01),
  49. },
  50. });
  51. });
  52. it('should fail when the recall score is below the threshold', async () => {
  53. const context = 'Context text';
  54. const groundTruth = 'Ground truth text';
  55. const threshold = 0.9;
  56. const mockCallApi = jest.fn().mockImplementation(() => {
  57. return Promise.resolve({
  58. output: 'foo [Attributed]\nbar [Not attributed]\nbaz [Attributed]',
  59. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  60. });
  61. });
  62. jest.spyOn(DefaultGradingProvider, 'callApi').mockImplementation(mockCallApi);
  63. await expect(matchesContextRecall(context, groundTruth, threshold)).resolves.toEqual({
  64. pass: false,
  65. reason: 'Recall 0.67 is < 0.9',
  66. score: expect.closeTo(0.67, 0.01),
  67. tokensUsed: {
  68. total: expect.any(Number),
  69. prompt: expect.any(Number),
  70. completion: expect.any(Number),
  71. cached: expect.any(Number),
  72. completionDetails: expect.any(Object),
  73. numRequests: 0,
  74. },
  75. metadata: {
  76. sentenceAttributions: expect.arrayContaining([
  77. expect.objectContaining({
  78. sentence: expect.any(String),
  79. attributed: expect.any(Boolean),
  80. }),
  81. ]),
  82. totalSentences: 3,
  83. attributedSentences: 2,
  84. score: expect.closeTo(0.67, 0.01),
  85. },
  86. });
  87. });
  88. it('should return detailed metadata with sentence attributions', async () => {
  89. const context = 'The capital of France is Paris. It has the Eiffel Tower.';
  90. const groundTruth =
  91. 'Paris is the capital of France. The Eiffel Tower is located there. London is the capital of UK.';
  92. const threshold = 0.6;
  93. const mockCallApi = jest.fn().mockImplementation(() => {
  94. return Promise.resolve({
  95. output:
  96. 'Paris is the capital of France. [Attributed]\nThe Eiffel Tower is located there. [Attributed]\nLondon is the capital of UK. [Not attributed]',
  97. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  98. });
  99. });
  100. jest.spyOn(DefaultGradingProvider, 'callApi').mockImplementation(mockCallApi);
  101. const result = await matchesContextRecall(context, groundTruth, threshold);
  102. expect(result.metadata).toBeDefined();
  103. expect(result.metadata?.sentenceAttributions).toHaveLength(3);
  104. expect(result.metadata?.sentenceAttributions[0]).toEqual({
  105. sentence: 'Paris is the capital of France',
  106. attributed: true,
  107. });
  108. expect(result.metadata?.sentenceAttributions[1]).toEqual({
  109. sentence: 'The Eiffel Tower is located there',
  110. attributed: true,
  111. });
  112. expect(result.metadata?.sentenceAttributions[2]).toEqual({
  113. sentence: 'London is the capital of UK',
  114. attributed: false,
  115. });
  116. expect(result.metadata?.totalSentences).toBe(3);
  117. expect(result.metadata?.attributedSentences).toBe(2);
  118. expect(result.metadata?.score).toBeCloseTo(0.67, 2);
  119. expect(result.pass).toBe(true);
  120. });
  121. });
Tip!

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

Comments

Loading...