forked from promptfoo/promptfoo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproviders.test.ts
543 lines (475 loc) · 18.4 KB
/
providers.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
import fs from 'fs';
import yaml from 'js-yaml';
import path from 'path';
import cliState from '../src/cliState';
import { loadApiProvider, loadApiProviders } from '../src/providers';
import { HttpProvider } from '../src/providers/http';
import { OpenAiChatCompletionProvider } from '../src/providers/openai/chat';
import { OpenAiEmbeddingProvider } from '../src/providers/openai/embedding';
import { PythonProvider } from '../src/providers/pythonCompletion';
import { ScriptCompletionProvider } from '../src/providers/scriptCompletion';
import { WebSocketProvider } from '../src/providers/websocket';
import type { ProviderOptions } from '../src/types';
jest.mock('fs');
jest.mock('js-yaml');
jest.mock('../src/fetch');
jest.mock('../src/providers/http');
jest.mock('../src/providers/openai/chat');
jest.mock('../src/providers/openai/embedding');
jest.mock('../src/providers/pythonCompletion');
jest.mock('../src/providers/scriptCompletion');
jest.mock('../src/providers/websocket');
describe('loadApiProvider', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.spyOn(process, 'exit').mockImplementation((() => {}) as any);
});
it('should load echo provider', async () => {
const provider = await loadApiProvider('echo');
expect(provider.id()).toBe('echo');
await expect(provider.callApi('test')).resolves.toEqual({ output: 'test' });
});
it('should load file provider from yaml', async () => {
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: 'test-key',
temperature: 0.7,
},
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const provider = await loadApiProvider('file://test.yaml', {
basePath: '/test',
});
expect(fs.readFileSync).toHaveBeenCalledWith(path.join('/test', 'test.yaml'), 'utf8');
expect(yaml.load).toHaveBeenCalledWith('yaml content');
expect(provider).toBeDefined();
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
});
it('should load file provider from json', async () => {
const jsonContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: 'test-key',
},
};
jest.mocked(fs.readFileSync).mockReturnValue(JSON.stringify(jsonContent));
jest.mocked(yaml.load).mockReturnValue(jsonContent);
const provider = await loadApiProvider('file://test.json', {
basePath: '/test',
});
expect(fs.readFileSync).toHaveBeenCalledWith(path.join('/test', 'test.json'), 'utf8');
expect(yaml.load).toHaveBeenCalledWith(JSON.stringify(jsonContent));
expect(provider).toBeDefined();
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
});
it('should load OpenAI chat provider', async () => {
const provider = await loadApiProvider('openai:chat:gpt-4');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load OpenAI chat provider with default model', async () => {
const provider = await loadApiProvider('openai:chat');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4o-mini', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load OpenAI embedding provider', async () => {
const provider = await loadApiProvider('openai:embedding');
expect(OpenAiEmbeddingProvider).toHaveBeenCalledWith(
'text-embedding-3-large',
expect.any(Object),
);
expect(provider).toBeDefined();
});
it('should load DeepSeek provider with default model', async () => {
const provider = await loadApiProvider('deepseek:');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('deepseek-chat', {
config: expect.objectContaining({
apiBaseUrl: 'https://api.deepseek.com/v1',
apiKeyEnvar: 'DEEPSEEK_API_KEY',
}),
});
expect(provider).toBeDefined();
});
it('should load DeepSeek provider with specific model', async () => {
const provider = await loadApiProvider('deepseek:deepseek-coder');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('deepseek-coder', {
config: expect.objectContaining({
apiBaseUrl: 'https://api.deepseek.com/v1',
apiKeyEnvar: 'DEEPSEEK_API_KEY',
}),
});
expect(provider).toBeDefined();
});
it('should load Hyperbolic provider with specific model', async () => {
const provider = await loadApiProvider('hyperbolic:meta-llama/Meta-Llama-3-8B-Instruct-Turbo');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
'meta-llama/Meta-Llama-3-8B-Instruct-Turbo',
{
config: expect.objectContaining({
apiBaseUrl: 'https://api.hyperbolic.xyz/v1',
apiKeyEnvar: 'HYPERBOLIC_API_KEY',
}),
},
);
expect(provider).toBeDefined();
});
it('should load HTTP provider', async () => {
const provider = await loadApiProvider('http://test.com');
expect(HttpProvider).toHaveBeenCalledWith('http://test.com', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load HTTPS provider', async () => {
const provider = await loadApiProvider('https://test.com');
expect(HttpProvider).toHaveBeenCalledWith('https://test.com', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load WebSocket provider', async () => {
const provider = await loadApiProvider('ws://test.com');
expect(WebSocketProvider).toHaveBeenCalledWith('ws://test.com', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load script provider', async () => {
const provider = await loadApiProvider('exec:test.sh');
expect(ScriptCompletionProvider).toHaveBeenCalledWith('test.sh', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load Python provider', async () => {
const provider = await loadApiProvider('python:test.py');
expect(PythonProvider).toHaveBeenCalledWith('test.py', expect.any(Object));
expect(provider).toBeDefined();
});
it('should load Python provider from file path', async () => {
const provider = await loadApiProvider('file://test.py');
expect(PythonProvider).toHaveBeenCalledWith('test.py', expect.any(Object));
expect(provider).toBeDefined();
});
it('should handle unidentified provider', async () => {
await expect(loadApiProvider('unknown:provider')).rejects.toThrow(
'Could not identify provider',
);
});
it('should load JFrog ML provider', async () => {
const provider = await loadApiProvider('jfrog:test-model');
expect(provider).toBeDefined();
});
it('should handle invalid file path for yaml/json config', async () => {
jest.mocked(fs.readFileSync).mockImplementation(() => {
throw new Error('File not found');
});
await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('File not found');
});
it('should handle invalid yaml content', async () => {
jest.mocked(fs.readFileSync).mockReturnValue('invalid: yaml: content:');
jest.mocked(yaml.load).mockReturnValue(null);
await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('Provider config');
});
it('should handle yaml config without id', async () => {
jest.mocked(fs.readFileSync).mockReturnValue('config:\n key: value');
jest.mocked(yaml.load).mockReturnValue({ config: { key: 'value' } });
await expect(loadApiProvider('file://invalid.yaml')).rejects.toThrow('must have an id');
});
it('should handle provider with custom base path', async () => {
const mockProvider = {
id: () => 'python:script.py',
config: {
basePath: '/custom/path',
},
callApi: async (input: string) => ({ output: input }),
};
jest.mocked(PythonProvider).mockImplementation(() => mockProvider as any);
const provider = await loadApiProvider('python:script.py', {
basePath: '/custom/path',
options: {
config: {},
},
});
expect(provider.config.basePath).toBe('/custom/path');
});
it('should handle provider with delay', async () => {
const provider = await loadApiProvider('echo', {
options: {
delay: 1000,
},
});
expect(provider.delay).toBe(1000);
});
it('should handle provider with custom label template', async () => {
process.env.CUSTOM_LABEL = 'my-label';
const provider = await loadApiProvider('echo', {
options: {
label: '{{ env.CUSTOM_LABEL }}',
},
});
expect(provider.label).toBe('my-label');
delete process.env.CUSTOM_LABEL;
});
it('should throw error when file provider array is loaded with loadApiProvider', async () => {
const yamlContent = [
{
id: 'openai:chat:gpt-4',
config: { apiKey: 'test-key1' },
},
{
id: 'anthropic:claude-2',
config: { apiKey: 'test-key2' },
},
];
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
await expect(loadApiProvider('file://test.yaml')).rejects.toThrow(
'Multiple providers found in test.yaml. Use loadApiProviders instead of loadApiProvider.',
);
});
it('should handle file provider with environment variables', async () => {
process.env.OPENAI_API_KEY = 'test-key-from-env';
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: '{{ env.OPENAI_API_KEY }}',
},
env: {
OPENAI_API_KEY: 'override-key',
},
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const provider = await loadApiProvider('file://test.yaml', {
basePath: '/test',
env: { OPENAI_API_KEY: 'final-override-key' },
});
expect(provider).toBeDefined();
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
'gpt-4',
expect.objectContaining({
config: expect.objectContaining({
apiKey: expect.any(String),
}),
env: expect.objectContaining({
OPENAI_API_KEY: 'final-override-key',
}),
}),
);
delete process.env.OPENAI_API_KEY;
});
it('should load multiple providers from yaml file using loadApiProviders', async () => {
const yamlContent = [
{
id: 'openai:chat:gpt-4',
config: { apiKey: 'test-key1' },
},
{
id: 'anthropic:claude-2',
config: { apiKey: 'test-key2' },
},
];
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const providers = await loadApiProviders('file://test.yaml');
expect(providers).toHaveLength(2);
expect(providers[0]).toBeDefined();
expect(providers[1]).toBeDefined();
expect(fs.readFileSync).toHaveBeenCalledWith(expect.stringContaining('test.yaml'), 'utf8');
expect(yaml.load).toHaveBeenCalledWith('yaml content');
});
it('should handle absolute file paths', async () => {
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: { apiKey: 'test-key' },
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const absolutePath = path.resolve('/absolute/path/to/providers.yaml');
const provider = await loadApiProvider(`file://${absolutePath}`);
expect(provider).toBeDefined();
expect(fs.readFileSync).toHaveBeenCalledWith(absolutePath, 'utf8');
});
it('should handle provider with null or undefined config values', async () => {
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: 'test-key',
nullValue: null,
undefinedValue: undefined,
},
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const provider = await loadApiProvider('file://test.yaml');
expect(provider).toBeDefined();
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
'gpt-4',
expect.objectContaining({
config: expect.objectContaining({
apiKey: 'test-key',
}),
}),
);
});
it('should handle provider with undefined options', async () => {
const provider = await loadApiProvider('echo', {
options: undefined,
});
expect(provider).toBeDefined();
});
it('should throw error for invalid providerPaths type', async () => {
// Test with a number, which is an invalid type
await expect(loadApiProviders(42 as any)).rejects.toThrow('Invalid providers list');
// Test with an object that doesn't match any valid format
await expect(loadApiProviders({ foo: 'bar' } as any)).rejects.toThrow('Invalid providers list');
});
it('should handle non-yaml/json file paths', async () => {
// Test with a text file path
await expect(loadApiProviders('file://test.txt')).rejects.toThrow(
/Could not identify provider/,
);
});
});
describe('loadApiProviders', () => {
beforeEach(() => {
jest.resetAllMocks();
cliState.config = undefined;
});
it('should load single provider from string', async () => {
const providers = await loadApiProviders('echo');
expect(providers).toHaveLength(1);
expect(providers[0].id()).toBe('echo');
});
it('should load multiple providers from array of strings', async () => {
const providers = await loadApiProviders(['echo', 'openai:chat:gpt-4']);
expect(providers).toHaveLength(2);
expect(providers[0].id()).toBe('echo');
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
});
it('should load provider from function', async () => {
const customFunction = async (prompt: string) => ({ output: prompt });
const providers = await loadApiProviders(customFunction);
expect(providers).toHaveLength(1);
expect(providers[0].id()).toBe('custom-function');
await expect(providers[0].callApi('test')).resolves.toEqual({ output: 'test' });
});
it('should load provider from function with label', async () => {
const customFunction = async (prompt: string) => ({ output: prompt });
customFunction.label = 'custom-label';
const providers = await loadApiProviders([customFunction]);
expect(providers).toHaveLength(1);
expect(providers[0].id()).toBe('custom-label');
});
it('should load provider from options object', async () => {
const options: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: 'test-key',
},
};
const providers = await loadApiProviders([options]);
expect(providers).toHaveLength(1);
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
});
it('should load provider from options map', async () => {
const providers = await loadApiProviders([
{
'openai:chat:gpt-4': {
config: {
apiKey: 'test-key',
},
},
},
]);
expect(providers).toHaveLength(1);
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith('gpt-4', expect.any(Object));
});
it('should throw error for invalid providers list', async () => {
await expect(loadApiProviders({} as any)).rejects.toThrow('Invalid providers list');
});
it('should handle loadApiProviders with empty array', async () => {
const providers = await loadApiProviders([]);
expect(providers).toHaveLength(0);
});
it('should handle loadApiProviders with mixed provider types', async () => {
const customFunction = async (prompt: string) => ({ output: prompt });
const providers = await loadApiProviders([
'echo',
customFunction,
{ id: 'openai:chat', config: {} },
{ 'openai:completion': { config: {} } },
]);
expect(providers).toHaveLength(4);
});
it('should handle provider with null config', async () => {
const provider = await loadApiProvider('echo', {
options: {
config: null,
},
});
expect(provider).toBeDefined();
});
it('should handle provider with undefined options', async () => {
const provider = await loadApiProvider('echo', {
options: undefined,
});
expect(provider).toBeDefined();
});
it('should handle relative file paths', async () => {
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: { apiKey: 'test-key' },
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const relativePath = 'relative/path/to/providers.yaml';
const providers = await loadApiProviders(`file://${relativePath}`, {
basePath: '/test/base/path',
});
expect(providers).toHaveLength(1);
expect(fs.readFileSync).toHaveBeenCalledWith(
path.join('/test/base/path', relativePath),
'utf8',
);
});
it('should handle absolute file paths in loadApiProviders', async () => {
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: { apiKey: 'test-key' },
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const absolutePath = path.resolve('/absolute/path/to/providers.yaml');
const providers = await loadApiProviders(`file://${absolutePath}`);
expect(providers).toHaveLength(1);
expect(fs.readFileSync).toHaveBeenCalledWith(absolutePath, 'utf8');
expect(yaml.load).toHaveBeenCalledWith('yaml content');
});
it('should use env values from cliState.config', async () => {
// Set up dummy config with env block
cliState.config = {
env: {
TEST_API_KEY: 'test-key-from-cli-state',
OTHER_VAR: 'other-value',
},
};
const yamlContent: ProviderOptions = {
id: 'openai:chat:gpt-4',
config: {
apiKey: '{{ env.TEST_API_KEY }}',
},
};
jest.mocked(fs.readFileSync).mockReturnValue('yaml content');
jest.mocked(yaml.load).mockReturnValue(yamlContent);
const providers = await loadApiProviders('file://test.yaml');
expect(providers).toHaveLength(1);
expect(OpenAiChatCompletionProvider).toHaveBeenCalledWith(
'gpt-4',
expect.objectContaining({
config: expect.objectContaining({
apiKey: expect.any(String),
}),
env: expect.objectContaining({
TEST_API_KEY: 'test-key-from-cli-state',
OTHER_VAR: 'other-value',
}),
}),
);
});
});