forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.groq.test.ts
324 lines (278 loc) · 10.7 KB
/
providers.groq.test.ts
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { clearCache, disableCache, enableCache } from '../src/cache';
import { GroqProvider } from '../src/providers/groq';
import { maybeLoadFromExternalFile } from '../src/util';
jest.mock('groq-sdk', () => {
return {
__esModule: true,
default: jest.fn().mockImplementation(() => ({
chat: {
completions: {
create: jest.fn(),
},
},
})),
};
});
jest.mock('../src/logger');
jest.mock('../src/util', () => ({
maybeLoadFromExternalFile: jest.fn(),
renderVarsInObject: jest.fn(),
}));
describe('Groq', () => {
afterEach(async () => {
jest.clearAllMocks();
await clearCache();
});
describe('GroqProvider', () => {
const provider = new GroqProvider('mixtral-8x7b-32768');
it('should initialize with correct model name', () => {
expect(provider.getModelName()).toBe('mixtral-8x7b-32768');
});
it('should return correct id', () => {
expect(provider.id()).toBe('groq:mixtral-8x7b-32768');
});
it('should return correct string representation', () => {
expect(provider.toString()).toBe('[Groq Provider mixtral-8x7b-32768]');
});
describe('callApi', () => {
it('should call Groq API and return output with correct structure', async () => {
const mockResponse = {
choices: [{ message: { content: 'Test output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
const result = await provider.callApi('Test prompt');
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
messages: expect.arrayContaining([
expect.objectContaining({
role: 'system',
content: 'You are a helpful assistant.',
}),
expect.objectContaining({
role: 'user',
content: 'Test prompt',
}),
]),
model: 'mixtral-8x7b-32768',
temperature: 0.7,
max_tokens: 1000,
top_p: 1,
tool_choice: 'auto',
}),
);
expect(result).toEqual({
output: 'Test output',
tokenUsage: {
total: 10,
prompt: 5,
completion: 5,
},
});
});
it('should use cache by default', async () => {
const mockResponse = {
choices: [{ message: { content: 'Cached output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
await provider.callApi('Test prompt');
const cachedResult = await provider.callApi('Test prompt');
expect(mockCreate).toHaveBeenCalledTimes(1);
expect(cachedResult).toEqual({
output: 'Cached output',
tokenUsage: {
total: 10,
prompt: 5,
completion: 5,
cached: 10,
},
});
});
it('should not use cache if caching is disabled', async () => {
const mockResponse = {
choices: [{ message: { content: 'Fresh output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
disableCache();
const result1 = await provider.callApi('Test prompt');
const result2 = await provider.callApi('Test prompt');
expect(mockCreate).toHaveBeenCalledTimes(2);
expect(result1).toEqual(result2);
expect(result1.tokenUsage).not.toHaveProperty('cached');
enableCache();
});
it('should handle API errors', async () => {
const mockError = new Error('API Error') as any;
mockError.name = 'APIError';
mockError.status = 400;
const mockCreate = jest.fn().mockRejectedValue(mockError);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
const result = await provider.callApi('Test prompt');
expect(result).toEqual({
error: 'API call error: 400 APIError: API Error',
});
});
it('should handle non-API errors', async () => {
const mockError = new Error('Unknown error');
const mockCreate = jest.fn().mockRejectedValue(mockError);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
const result = await provider.callApi('Test prompt');
expect(result).toEqual({
error: 'API call error: Error: Unknown error',
});
});
it('should pass custom configuration options including tools and tool_choice', async () => {
const tools: {
type: 'function';
function: {
name: string;
description?: string | undefined;
parameters?: Record<string, any> | undefined;
};
}[] = [{ type: 'function', function: { name: 'test_function' } }];
jest.mocked(maybeLoadFromExternalFile).mockReturnValue(tools);
const customProvider = new GroqProvider('llama3-groq-8b-8192-tool-use-preview', {
config: {
temperature: 0.7,
max_tokens: 100,
top_p: 0.9,
tools,
tool_choice: 'auto',
},
});
const mockCreate = jest.fn().mockResolvedValue({
choices: [{ message: { content: 'Custom output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
});
(customProvider as any).groq = { chat: { completions: { create: mockCreate } } };
await customProvider.callApi('Test prompt');
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
temperature: 0.7,
max_tokens: 100,
top_p: 0.9,
tools,
tool_choice: 'auto',
messages: expect.any(Array),
model: 'llama3-groq-8b-8192-tool-use-preview',
}),
);
});
it('should handle tool calls and function callbacks', async () => {
const mockResponse = {
choices: [
{
message: {
content: null,
tool_calls: [
{
id: 'call_123',
type: 'function',
function: { name: 'test_function', arguments: '{"arg": "value"}' },
},
],
},
},
],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
const mockCallback = jest.fn().mockResolvedValue('Function result');
const customProvider = new GroqProvider('llama3-groq-8b-8192-tool-use-preview', {
config: {
functionToolCallbacks: {
test_function: mockCallback,
},
},
});
(customProvider as any).groq = { chat: { completions: { create: mockCreate } } };
const result = await customProvider.callApi('Test prompt');
expect(mockCallback).toHaveBeenCalledWith('{"arg": "value"}');
expect(result.output).toContain(
'[{"id":"call_123","type":"function","function":{"name":"test_function","arguments":"{\\"arg\\": \\"value\\"}"}}]',
);
expect(result.output).toContain('[Function Result: Function result]');
});
it('should use custom system prompt', async () => {
const customProvider = new GroqProvider('llama3-groq-8b-8192-tool-use-preview', {
config: {
systemPrompt: 'Custom system prompt',
},
});
const mockResponse = {
choices: [{ message: { content: 'Test output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(customProvider as any).groq = { chat: { completions: { create: mockCreate } } };
await customProvider.callApi('Test prompt');
expect(mockCreate).toHaveBeenCalledWith(
expect.objectContaining({
messages: expect.arrayContaining([
expect.objectContaining({
role: 'system',
content: 'Custom system prompt',
}),
]),
}),
);
});
it('should handle empty response', async () => {
const mockResponse = {
choices: [{ message: { content: '' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
const result = await provider.callApi('Test prompt');
expect(result.output).toBe('');
});
it('should handle unexpected API response structure', async () => {
const mockResponse = {};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(provider as any).groq = { chat: { completions: { create: mockCreate } } };
await expect(provider.callApi('Test prompt')).resolves.toEqual({
error: 'API call error: Error: Invalid response from Groq API',
});
});
it('should use maybeLoadFromExternalFile for tools configuration', async () => {
const customProvider = new GroqProvider('llama3-groq-8b-8192-tool-use-preview', {
config: {
tools: [
{
type: 'function',
function: {
name: 'external_tool',
description: 'An external tool',
},
},
],
},
});
const mockResponse = {
choices: [{ message: { content: 'Test output' } }],
usage: { total_tokens: 10, prompt_tokens: 5, completion_tokens: 5 },
};
const mockCreate = jest.fn().mockResolvedValue(mockResponse);
(customProvider as any).groq = { chat: { completions: { create: mockCreate } } };
await customProvider.callApi('Test prompt');
expect(maybeLoadFromExternalFile).toHaveBeenCalledWith(
expect.arrayContaining([
expect.objectContaining({
type: 'function',
function: {
name: 'external_tool',
description: 'An external tool',
},
}),
]),
);
});
});
});
});