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

similarity.test.ts 6.2 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
  1. import { matchesSimilarity } from '../../src/matchers';
  2. import { DefaultEmbeddingProvider } from '../../src/providers/openai/defaults';
  3. import { OpenAiEmbeddingProvider } from '../../src/providers/openai/embedding';
  4. import type { OpenAiChatCompletionProvider } from '../../src/providers/openai/chat';
  5. import type { GradingConfig } from '../../src/types';
  6. describe('matchesSimilarity', () => {
  7. beforeEach(() => {
  8. jest.spyOn(DefaultEmbeddingProvider, 'callEmbeddingApi').mockImplementation((text) => {
  9. if (text === 'Expected output' || text === 'Sample output') {
  10. return Promise.resolve({
  11. embedding: [1, 0, 0],
  12. tokenUsage: { total: 5, prompt: 2, completion: 3 },
  13. });
  14. } else if (text === 'Different output') {
  15. return Promise.resolve({
  16. embedding: [0, 1, 0],
  17. tokenUsage: { total: 5, prompt: 2, completion: 3 },
  18. });
  19. }
  20. return Promise.reject(new Error('Unexpected input'));
  21. });
  22. });
  23. afterEach(() => {
  24. jest.restoreAllMocks();
  25. });
  26. it('should pass when similarity is above the threshold', async () => {
  27. const expected = 'Expected output';
  28. const output = 'Sample output';
  29. const threshold = 0.5;
  30. await expect(matchesSimilarity(expected, output, threshold)).resolves.toEqual({
  31. pass: true,
  32. reason: 'Similarity 1.00 is greater than threshold 0.5',
  33. score: 1,
  34. tokensUsed: {
  35. total: expect.any(Number),
  36. prompt: expect.any(Number),
  37. completion: expect.any(Number),
  38. cached: expect.any(Number),
  39. completionDetails: expect.any(Object),
  40. },
  41. });
  42. });
  43. it('should fail when similarity is below the threshold', async () => {
  44. const expected = 'Expected output';
  45. const output = 'Different output';
  46. const threshold = 0.9;
  47. await expect(matchesSimilarity(expected, output, threshold)).resolves.toEqual({
  48. pass: false,
  49. reason: 'Similarity 0.00 is less than threshold 0.9',
  50. score: 0,
  51. tokensUsed: {
  52. total: expect.any(Number),
  53. prompt: expect.any(Number),
  54. completion: expect.any(Number),
  55. cached: expect.any(Number),
  56. completionDetails: expect.any(Object),
  57. },
  58. });
  59. });
  60. it('should fail when inverted similarity is above the threshold', async () => {
  61. const expected = 'Expected output';
  62. const output = 'Sample output';
  63. const threshold = 0.5;
  64. await expect(
  65. matchesSimilarity(expected, output, threshold, true /* invert */),
  66. ).resolves.toEqual({
  67. pass: false,
  68. reason: 'Similarity 1.00 is greater than threshold 0.5',
  69. score: 0,
  70. tokensUsed: {
  71. total: expect.any(Number),
  72. prompt: expect.any(Number),
  73. completion: expect.any(Number),
  74. cached: expect.any(Number),
  75. completionDetails: expect.any(Object),
  76. },
  77. });
  78. });
  79. it('should pass when inverted similarity is below the threshold', async () => {
  80. const expected = 'Expected output';
  81. const output = 'Different output';
  82. const threshold = 0.9;
  83. await expect(
  84. matchesSimilarity(expected, output, threshold, true /* invert */),
  85. ).resolves.toEqual({
  86. pass: true,
  87. reason: 'Similarity 0.00 is less than threshold 0.9',
  88. score: 1,
  89. tokensUsed: {
  90. total: expect.any(Number),
  91. prompt: expect.any(Number),
  92. completion: expect.any(Number),
  93. cached: expect.any(Number),
  94. completionDetails: expect.any(Object),
  95. },
  96. });
  97. });
  98. it('should use the overridden similarity grading config', async () => {
  99. const expected = 'Expected output';
  100. const output = 'Sample output';
  101. const threshold = 0.5;
  102. const grading: GradingConfig = {
  103. provider: {
  104. id: 'openai:embedding:text-embedding-ada-9999999',
  105. config: {
  106. apiKey: 'abc123',
  107. temperature: 3.1415926,
  108. },
  109. },
  110. };
  111. const mockCallApi = jest.spyOn(OpenAiEmbeddingProvider.prototype, 'callEmbeddingApi');
  112. mockCallApi.mockImplementation(function (this: OpenAiChatCompletionProvider) {
  113. expect(this.config.temperature).toBe(3.1415926);
  114. expect(this.getApiKey()).toBe('abc123');
  115. return Promise.resolve({
  116. embedding: [1, 0, 0],
  117. tokenUsage: { total: 5, prompt: 2, completion: 3 },
  118. });
  119. });
  120. await expect(matchesSimilarity(expected, output, threshold, false, grading)).resolves.toEqual({
  121. pass: true,
  122. reason: 'Similarity 1.00 is greater than threshold 0.5',
  123. score: 1,
  124. tokensUsed: {
  125. total: expect.any(Number),
  126. prompt: expect.any(Number),
  127. completion: expect.any(Number),
  128. cached: expect.any(Number),
  129. completionDetails: expect.any(Object),
  130. },
  131. });
  132. expect(mockCallApi).toHaveBeenCalledWith('Expected output');
  133. mockCallApi.mockRestore();
  134. });
  135. it('should throw an error when API call fails', async () => {
  136. const expected = 'Expected output';
  137. const output = 'Sample output';
  138. const threshold = 0.5;
  139. const grading: GradingConfig = {
  140. provider: {
  141. id: 'openai:embedding:text-embedding-ada-9999999',
  142. config: {
  143. apiKey: 'abc123',
  144. temperature: 3.1415926,
  145. },
  146. },
  147. };
  148. jest
  149. .spyOn(OpenAiEmbeddingProvider.prototype, 'callEmbeddingApi')
  150. .mockRejectedValueOnce(new Error('API call failed'));
  151. await expect(async () => {
  152. await matchesSimilarity(expected, output, threshold, false, grading);
  153. }).rejects.toThrow('API call failed');
  154. });
  155. it('should use Nunjucks templating when PROMPTFOO_DISABLE_TEMPLATING is set', async () => {
  156. process.env.PROMPTFOO_DISABLE_TEMPLATING = 'true';
  157. const expected = 'Expected {{ var }}';
  158. const output = 'Output {{ var }}';
  159. const threshold = 0.8;
  160. const grading: GradingConfig = {
  161. provider: DefaultEmbeddingProvider,
  162. };
  163. jest.spyOn(DefaultEmbeddingProvider, 'callEmbeddingApi').mockResolvedValue({
  164. embedding: [1, 2, 3],
  165. tokenUsage: { total: 10, prompt: 5, completion: 5 },
  166. });
  167. await matchesSimilarity(expected, output, threshold, false, grading);
  168. expect(DefaultEmbeddingProvider.callEmbeddingApi).toHaveBeenCalledWith('Expected {{ var }}');
  169. expect(DefaultEmbeddingProvider.callEmbeddingApi).toHaveBeenCalledWith('Output {{ var }}');
  170. process.env.PROMPTFOO_DISABLE_TEMPLATING = undefined;
  171. });
  172. });
Tip!

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

Comments

Loading...