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

answerRelevance.test.ts 5.0 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
  1. import { handleAnswerRelevance } from '../../src/assertions/answerRelevance';
  2. import { matchesAnswerRelevance } from '../../src/matchers';
  3. import invariant from '../../src/util/invariant';
  4. import type { AssertionValueFunctionContext } from '../../src/types';
  5. jest.mock('../../src/matchers');
  6. jest.mock('../../src/util/invariant');
  7. describe('handleAnswerRelevance', () => {
  8. beforeEach(() => {
  9. jest.resetAllMocks();
  10. jest.mocked(invariant).mockImplementation((condition, message) => {
  11. if (!condition) {
  12. throw new Error(typeof message === 'function' ? message() : message);
  13. }
  14. });
  15. });
  16. it('should call matchesAnswerRelevance with correct parameters', async () => {
  17. const mockMatchesAnswerRelevance = jest.mocked(matchesAnswerRelevance);
  18. mockMatchesAnswerRelevance.mockResolvedValue({
  19. pass: true,
  20. score: 0.8,
  21. reason: 'test reason',
  22. });
  23. const result = await handleAnswerRelevance({
  24. assertion: {
  25. type: 'answer-relevance',
  26. threshold: 0.7,
  27. },
  28. output: 'test output',
  29. prompt: 'test prompt',
  30. test: {
  31. vars: {},
  32. options: {},
  33. },
  34. baseType: 'answer-relevance',
  35. context: {} as AssertionValueFunctionContext,
  36. inverse: false,
  37. outputString: 'test output',
  38. providerResponse: {
  39. output: 'test output',
  40. tokenUsage: {},
  41. },
  42. });
  43. expect(mockMatchesAnswerRelevance).toHaveBeenCalledWith('test prompt', 'test output', 0.7, {});
  44. expect(result).toEqual({
  45. assertion: {
  46. type: 'answer-relevance',
  47. threshold: 0.7,
  48. },
  49. pass: true,
  50. score: 0.8,
  51. reason: 'test reason',
  52. });
  53. });
  54. it('should use query from vars if available', async () => {
  55. const mockMatchesAnswerRelevance = jest.mocked(matchesAnswerRelevance);
  56. mockMatchesAnswerRelevance.mockResolvedValue({
  57. pass: true,
  58. score: 0.8,
  59. reason: 'test reason',
  60. });
  61. const result = await handleAnswerRelevance({
  62. assertion: {
  63. type: 'answer-relevance',
  64. threshold: 0.7,
  65. },
  66. output: 'test output',
  67. prompt: 'test prompt',
  68. test: {
  69. vars: {
  70. query: 'test query',
  71. },
  72. options: {},
  73. },
  74. baseType: 'answer-relevance',
  75. context: {} as AssertionValueFunctionContext,
  76. inverse: false,
  77. outputString: 'test output',
  78. providerResponse: {
  79. output: 'test output',
  80. tokenUsage: {},
  81. },
  82. });
  83. expect(mockMatchesAnswerRelevance).toHaveBeenCalledWith('test query', 'test output', 0.7, {});
  84. expect(result).toEqual({
  85. assertion: {
  86. type: 'answer-relevance',
  87. threshold: 0.7,
  88. },
  89. pass: true,
  90. score: 0.8,
  91. reason: 'test reason',
  92. });
  93. });
  94. it('should throw error if output is not string', async () => {
  95. await expect(
  96. handleAnswerRelevance({
  97. assertion: {
  98. type: 'answer-relevance',
  99. },
  100. output: {},
  101. prompt: 'test prompt',
  102. test: {
  103. vars: {},
  104. options: {},
  105. },
  106. baseType: 'answer-relevance',
  107. context: {} as AssertionValueFunctionContext,
  108. inverse: false,
  109. outputString: 'test output',
  110. providerResponse: {
  111. output: 'test output',
  112. tokenUsage: {},
  113. },
  114. }),
  115. ).rejects.toThrow('answer-relevance assertion type must evaluate a string output');
  116. });
  117. it('should throw error if prompt is missing', async () => {
  118. await expect(
  119. handleAnswerRelevance({
  120. assertion: {
  121. type: 'answer-relevance',
  122. },
  123. output: 'test output',
  124. prompt: '',
  125. test: {
  126. vars: {},
  127. options: {},
  128. },
  129. baseType: 'answer-relevance',
  130. context: {} as AssertionValueFunctionContext,
  131. inverse: false,
  132. outputString: 'test output',
  133. providerResponse: {
  134. output: 'test output',
  135. tokenUsage: {},
  136. },
  137. }),
  138. ).rejects.toThrow('answer-relevance assertion type must have a prompt');
  139. });
  140. it('should use default threshold of 0 if not specified', async () => {
  141. const mockMatchesAnswerRelevance = jest.mocked(matchesAnswerRelevance);
  142. mockMatchesAnswerRelevance.mockResolvedValue({
  143. pass: true,
  144. score: 0.8,
  145. reason: 'test reason',
  146. });
  147. const result = await handleAnswerRelevance({
  148. assertion: {
  149. type: 'answer-relevance',
  150. },
  151. output: 'test output',
  152. prompt: 'test prompt',
  153. test: {
  154. vars: {},
  155. options: {},
  156. },
  157. baseType: 'answer-relevance',
  158. context: {} as AssertionValueFunctionContext,
  159. inverse: false,
  160. outputString: 'test output',
  161. providerResponse: {
  162. output: 'test output',
  163. tokenUsage: {},
  164. },
  165. });
  166. expect(mockMatchesAnswerRelevance).toHaveBeenCalledWith('test prompt', 'test output', 0, {});
  167. expect(result).toEqual({
  168. assertion: {
  169. type: 'answer-relevance',
  170. },
  171. pass: true,
  172. score: 0.8,
  173. reason: 'test reason',
  174. });
  175. });
  176. });
Tip!

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

Comments

Loading...