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

providers.ts 17 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
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
  1. import fs from 'fs';
  2. import path from 'path';
  3. import invariant from 'tiny-invariant';
  4. import yaml from 'js-yaml';
  5. import logger from './logger';
  6. import {
  7. OpenAiAssistantProvider,
  8. OpenAiCompletionProvider,
  9. OpenAiChatCompletionProvider,
  10. OpenAiEmbeddingProvider,
  11. OpenAiImageProvider,
  12. OpenAiModerationProvider,
  13. } from './providers/openai';
  14. import { AnthropicCompletionProvider, AnthropicMessagesProvider } from './providers/anthropic';
  15. import { ReplicateModerationProvider, ReplicateProvider } from './providers/replicate';
  16. import {
  17. LocalAiCompletionProvider,
  18. LocalAiChatProvider,
  19. LocalAiEmbeddingProvider,
  20. } from './providers/localai';
  21. import { PalmChatProvider } from './providers/palm';
  22. import { LlamaProvider } from './providers/llama';
  23. import {
  24. OllamaEmbeddingProvider,
  25. OllamaCompletionProvider,
  26. OllamaChatProvider,
  27. } from './providers/ollama';
  28. import { VertexChatProvider } from './providers/vertex';
  29. import { MistralChatCompletionProvider } from './providers/mistral';
  30. import { WebhookProvider } from './providers/webhook';
  31. import { ScriptCompletionProvider } from './providers/scriptCompletion';
  32. import {
  33. AzureOpenAiAssistantProvider,
  34. AzureOpenAiChatCompletionProvider,
  35. AzureOpenAiCompletionProvider,
  36. AzureOpenAiEmbeddingProvider,
  37. } from './providers/azureopenai';
  38. import {
  39. HuggingfaceFeatureExtractionProvider,
  40. HuggingfaceSentenceSimilarityProvider,
  41. HuggingfaceTextClassificationProvider,
  42. HuggingfaceTextGenerationProvider,
  43. HuggingfaceTokenExtractionProvider,
  44. } from './providers/huggingface';
  45. import { AwsBedrockCompletionProvider, AwsBedrockEmbeddingProvider } from './providers/bedrock';
  46. import { PythonProvider } from './providers/pythonCompletion';
  47. import { CohereChatCompletionProvider } from './providers/cohere';
  48. import * as CloudflareAiProviders from './providers/cloudflare-ai';
  49. import { BAMChatProvider, BAMEmbeddingProvider } from './providers/bam';
  50. import { PortkeyChatCompletionProvider } from './providers/portkey';
  51. import { HttpProvider } from './providers/http';
  52. import { importModule } from './esm';
  53. import type {
  54. ApiProvider,
  55. EnvOverrides,
  56. ProviderOptions,
  57. ProviderOptionsMap,
  58. TestSuiteConfig,
  59. } from './types';
  60. export async function loadApiProviders(
  61. providerPaths: TestSuiteConfig['providers'],
  62. options: {
  63. basePath?: string;
  64. env?: EnvOverrides;
  65. } = {},
  66. ): Promise<ApiProvider[]> {
  67. const { basePath, env } = options;
  68. if (typeof providerPaths === 'string') {
  69. return [await loadApiProvider(providerPaths, { basePath, env })];
  70. } else if (typeof providerPaths === 'function') {
  71. return [
  72. {
  73. id: () => 'custom-function',
  74. callApi: providerPaths,
  75. },
  76. ];
  77. } else if (Array.isArray(providerPaths)) {
  78. return Promise.all(
  79. providerPaths.map((provider, idx) => {
  80. if (typeof provider === 'string') {
  81. return loadApiProvider(provider, { basePath, env });
  82. } else if (typeof provider === 'function') {
  83. return {
  84. id: () => `custom-function-${idx}`,
  85. callApi: provider,
  86. };
  87. } else if (provider.id) {
  88. // List of ProviderConfig objects
  89. return loadApiProvider((provider as ProviderOptions).id!, {
  90. options: provider,
  91. basePath,
  92. env,
  93. });
  94. } else {
  95. // List of { id: string, config: ProviderConfig } objects
  96. const id = Object.keys(provider)[0];
  97. const providerObject = (provider as ProviderOptionsMap)[id];
  98. const context = { ...providerObject, id: providerObject.id || id };
  99. return loadApiProvider(id, { options: context, basePath, env });
  100. }
  101. }),
  102. );
  103. }
  104. throw new Error('Invalid providers list');
  105. }
  106. // FIXME(ian): Make loadApiProvider handle all the different provider types (string, ProviderOptions, ApiProvider, etc), rather than the callers.
  107. export async function loadApiProvider(
  108. providerPath: string,
  109. context: {
  110. options?: ProviderOptions;
  111. basePath?: string;
  112. env?: EnvOverrides;
  113. } = {},
  114. ): Promise<ApiProvider> {
  115. const { options = {}, basePath, env } = context;
  116. const providerOptions: ProviderOptions = {
  117. // Hack(ian): Override id with label. This makes it so that debug and display info, which rely on id, will use the label instead.
  118. id: options.label || options.id,
  119. config: {
  120. ...options.config,
  121. basePath,
  122. },
  123. env,
  124. };
  125. let ret: ApiProvider;
  126. if (
  127. providerPath.startsWith('file://') &&
  128. (providerPath.endsWith('.yaml') || providerPath.endsWith('.yml'))
  129. ) {
  130. const filePath = providerPath.slice('file://'.length);
  131. const yamlContent = yaml.load(fs.readFileSync(filePath, 'utf8')) as ProviderOptions;
  132. invariant(yamlContent, `Provider config ${filePath} is undefined`);
  133. invariant(yamlContent.id, `Provider config ${filePath} must have an id`);
  134. logger.info(`Loaded provider ${yamlContent.id} from ${filePath}`);
  135. ret = await loadApiProvider(yamlContent.id, { ...context, options: yamlContent });
  136. } else if (providerPath === 'echo') {
  137. ret = {
  138. id: () => 'echo',
  139. callApi: async (input) => ({ output: input }),
  140. };
  141. } else if (providerPath.startsWith('exec:')) {
  142. // Load script module
  143. const scriptPath = providerPath.split(':')[1];
  144. ret = new ScriptCompletionProvider(scriptPath, providerOptions);
  145. } else if (providerPath.startsWith('python:')) {
  146. const scriptPath = providerPath.split(':')[1];
  147. ret = new PythonProvider(scriptPath, providerOptions);
  148. } else if (providerPath.startsWith('openai:')) {
  149. // Load OpenAI module
  150. const splits = providerPath.split(':');
  151. const modelType = splits[1];
  152. const modelName = splits.slice(2).join(':');
  153. if (modelType === 'chat') {
  154. ret = new OpenAiChatCompletionProvider(modelName || 'gpt-3.5-turbo', providerOptions);
  155. } else if (modelType === 'embedding' || modelType === 'embeddings') {
  156. ret = new OpenAiEmbeddingProvider(modelName || 'text-embedding-3-large', providerOptions);
  157. } else if (modelType === 'completion') {
  158. ret = new OpenAiCompletionProvider(modelName || 'gpt-3.5-turbo-instruct', providerOptions);
  159. } else if (modelType === 'moderation') {
  160. ret = new OpenAiModerationProvider(modelName || 'text-moderation-latest', providerOptions);
  161. } else if (OpenAiChatCompletionProvider.OPENAI_CHAT_MODEL_NAMES.includes(modelType)) {
  162. ret = new OpenAiChatCompletionProvider(modelType, providerOptions);
  163. } else if (OpenAiCompletionProvider.OPENAI_COMPLETION_MODEL_NAMES.includes(modelType)) {
  164. ret = new OpenAiCompletionProvider(modelType, providerOptions);
  165. } else if (modelType === 'assistant') {
  166. ret = new OpenAiAssistantProvider(modelName, providerOptions);
  167. } else if (modelType === 'image') {
  168. ret = new OpenAiImageProvider(modelName, providerOptions);
  169. } else {
  170. throw new Error(
  171. `Unknown OpenAI model type: ${modelType}. Use one of the following providers: openai:chat:<model name>, openai:completion:<model name>, openai:embeddings:<model name>, openai:image:<model name>`,
  172. );
  173. }
  174. } else if (providerPath.startsWith('azureopenai:')) {
  175. // Load Azure OpenAI module
  176. const splits = providerPath.split(':');
  177. const modelType = splits[1];
  178. const deploymentName = splits[2];
  179. if (modelType === 'chat') {
  180. ret = new AzureOpenAiChatCompletionProvider(deploymentName, providerOptions);
  181. } else if (modelType === 'assistant') {
  182. ret = new AzureOpenAiAssistantProvider(deploymentName, providerOptions);
  183. } else if (modelType === 'embedding' || modelType === 'embeddings') {
  184. ret = new AzureOpenAiEmbeddingProvider(
  185. deploymentName || 'text-embedding-ada-002',
  186. providerOptions,
  187. );
  188. } else if (modelType === 'completion') {
  189. ret = new AzureOpenAiCompletionProvider(deploymentName, providerOptions);
  190. } else {
  191. throw new Error(
  192. `Unknown Azure OpenAI model type: ${modelType}. Use one of the following providers: azureopenai:chat:<model name>, azureopenai:assistant:<assistant id>, azureopenai:completion:<model name>`,
  193. );
  194. }
  195. } else if (providerPath.startsWith('openrouter:')) {
  196. const splits = providerPath.split(':');
  197. const modelName = splits.slice(1).join(':');
  198. ret = new OpenAiChatCompletionProvider(modelName, {
  199. ...providerOptions,
  200. config: {
  201. ...providerOptions.config,
  202. apiBaseUrl: 'https://openrouter.ai/api/v1',
  203. apiKeyEnvar: 'OPENROUTER_API_KEY',
  204. },
  205. });
  206. } else if (providerPath.startsWith('portkey:')) {
  207. const splits = providerPath.split(':');
  208. const modelName = splits.slice(1).join(':');
  209. ret = new PortkeyChatCompletionProvider(modelName, providerOptions);
  210. } else if (providerPath.startsWith('anthropic:')) {
  211. const splits = providerPath.split(':');
  212. const modelType = splits[1];
  213. const modelName = splits[2];
  214. if (modelType === 'messages') {
  215. ret = new AnthropicMessagesProvider(modelName, providerOptions);
  216. } else if (modelType === 'completion') {
  217. ret = new AnthropicCompletionProvider(modelName, providerOptions);
  218. } else if (AnthropicCompletionProvider.ANTHROPIC_COMPLETION_MODELS.includes(modelType)) {
  219. ret = new AnthropicCompletionProvider(modelType, providerOptions);
  220. } else {
  221. throw new Error(
  222. `Unknown Anthropic model type: ${modelType}. Use one of the following providers: anthropic:completion:<model name>`,
  223. );
  224. }
  225. } else if (providerPath.startsWith('bedrock:')) {
  226. const splits = providerPath.split(':');
  227. const modelType = splits[1];
  228. const modelName = splits.slice(2).join(':');
  229. if (modelType === 'completion') {
  230. // Backwards compatibility: `completion` used to be required
  231. ret = new AwsBedrockCompletionProvider(modelName, providerOptions);
  232. } else if (modelType === 'embeddings' || modelType === 'embedding') {
  233. ret = new AwsBedrockEmbeddingProvider(modelName, providerOptions);
  234. } else {
  235. ret = new AwsBedrockCompletionProvider(
  236. `${modelType}${modelName ? `:${modelName}` : ''}`,
  237. providerOptions,
  238. );
  239. }
  240. } else if (providerPath.startsWith('huggingface:') || providerPath.startsWith('hf:')) {
  241. const splits = providerPath.split(':');
  242. if (splits.length < 3) {
  243. throw new Error(
  244. `Invalid Huggingface provider path: ${providerPath}. Use one of the following providers: huggingface:feature-extraction:<model name>, huggingface:text-generation:<model name>, huggingface:text-classification:<model name>, huggingface:token-classification:<model name>`,
  245. );
  246. }
  247. const modelName = splits.slice(2).join(':');
  248. if (splits[1] === 'feature-extraction') {
  249. ret = new HuggingfaceFeatureExtractionProvider(modelName, providerOptions);
  250. } else if (splits[1] === 'sentence-similarity') {
  251. ret = new HuggingfaceSentenceSimilarityProvider(modelName, providerOptions);
  252. } else if (splits[1] === 'text-generation') {
  253. ret = new HuggingfaceTextGenerationProvider(modelName, providerOptions);
  254. } else if (splits[1] === 'text-classification') {
  255. ret = new HuggingfaceTextClassificationProvider(modelName, providerOptions);
  256. } else if (splits[1] === 'token-classification') {
  257. ret = new HuggingfaceTokenExtractionProvider(modelName, providerOptions);
  258. } else {
  259. throw new Error(
  260. `Invalid Huggingface provider path: ${providerPath}. Use one of the following providers: huggingface:feature-extraction:<model name>, huggingface:text-generation:<model name>, huggingface:text-classification:<model name>, huggingface:token-classification:<model name>`,
  261. );
  262. }
  263. } else if (providerPath.startsWith('replicate:')) {
  264. const splits = providerPath.split(':');
  265. const modelType = splits[1];
  266. const modelName = splits.slice(2).join(':');
  267. if (modelType === 'moderation') {
  268. ret = new ReplicateModerationProvider(modelName, providerOptions);
  269. } else {
  270. // By default, there is no model type.
  271. ret = new ReplicateProvider(modelType + ':' + modelName, providerOptions);
  272. }
  273. } else if (providerPath.startsWith('bam:')) {
  274. const splits = providerPath.split(':');
  275. const modelType = splits[1];
  276. const modelName = splits.slice(2).join(':');
  277. if (modelType === 'chat') {
  278. ret = new BAMChatProvider(modelName || 'ibm/granite-13b-chat-v2', providerOptions);
  279. } else {
  280. throw new Error(
  281. `Invalid BAM provider: ${providerPath}. Use one of the following providers: bam:chat:<model name>`,
  282. );
  283. }
  284. } else if (providerPath.startsWith('cloudflare-ai:')) {
  285. // Load Cloudflare AI
  286. const splits = providerPath.split(':');
  287. const modelType = splits[1];
  288. const deploymentName = splits[2];
  289. if (modelType === 'chat') {
  290. ret = new CloudflareAiProviders.CloudflareAiChatCompletionProvider(
  291. deploymentName,
  292. providerOptions,
  293. );
  294. } else if (modelType === 'embedding' || modelType === 'embeddings') {
  295. ret = new CloudflareAiProviders.CloudflareAiEmbeddingProvider(
  296. deploymentName,
  297. providerOptions,
  298. );
  299. } else if (modelType === 'completion') {
  300. ret = new CloudflareAiProviders.CloudflareAiCompletionProvider(
  301. deploymentName,
  302. providerOptions,
  303. );
  304. } else {
  305. throw new Error(
  306. `Unknown Cloudflare AI model type: ${modelType}. Use one of the following providers: cloudflare-ai:chat:<model name>, cloudflare-ai:completion:<model name>, cloudflare-ai:embedding:`,
  307. );
  308. }
  309. } else if (providerPath.startsWith('webhook:')) {
  310. const webhookUrl = providerPath.substring('webhook:'.length);
  311. ret = new WebhookProvider(webhookUrl, providerOptions);
  312. } else if (providerPath === 'llama' || providerPath.startsWith('llama:')) {
  313. const modelName = providerPath.split(':')[1];
  314. ret = new LlamaProvider(modelName, providerOptions);
  315. } else if (
  316. providerPath.startsWith('ollama:embeddings:') ||
  317. providerPath.startsWith('ollama:embedding:')
  318. ) {
  319. const modelName = providerPath.split(':')[2];
  320. ret = new OllamaEmbeddingProvider(modelName, providerOptions);
  321. } else if (providerPath.startsWith('ollama:')) {
  322. const splits = providerPath.split(':');
  323. const firstPart = splits[1];
  324. if (firstPart === 'chat') {
  325. const modelName = splits.slice(2).join(':');
  326. ret = new OllamaChatProvider(modelName, providerOptions);
  327. } else if (firstPart === 'completion') {
  328. const modelName = splits.slice(2).join(':');
  329. ret = new OllamaCompletionProvider(modelName, providerOptions);
  330. } else {
  331. // Default to completion provider
  332. const modelName = splits.slice(1).join(':');
  333. ret = new OllamaCompletionProvider(modelName, providerOptions);
  334. }
  335. } else if (providerPath.startsWith('palm:') || providerPath.startsWith('google:')) {
  336. const modelName = providerPath.split(':')[1];
  337. ret = new PalmChatProvider(modelName, providerOptions);
  338. } else if (providerPath.startsWith('vertex')) {
  339. const modelName = providerPath.split(':')[1];
  340. ret = new VertexChatProvider(modelName, providerOptions);
  341. } else if (providerPath.startsWith('mistral:')) {
  342. const modelName = providerPath.split(':')[1];
  343. ret = new MistralChatCompletionProvider(modelName, providerOptions);
  344. } else if (providerPath.startsWith('cohere:')) {
  345. const modelName = providerPath.split(':')[1];
  346. ret = new CohereChatCompletionProvider(modelName, providerOptions);
  347. } else if (providerPath.startsWith('localai:')) {
  348. const splits = providerPath.split(':');
  349. const modelType = splits[1];
  350. const modelName = splits[2];
  351. if (modelType === 'chat') {
  352. ret = new LocalAiChatProvider(modelName, providerOptions);
  353. } else if (modelType === 'completion') {
  354. ret = new LocalAiCompletionProvider(modelName, providerOptions);
  355. } else if (modelType === 'embedding' || modelType === 'embeddings') {
  356. ret = new LocalAiEmbeddingProvider(modelName, providerOptions);
  357. } else {
  358. ret = new LocalAiChatProvider(modelType, providerOptions);
  359. }
  360. } else if (providerPath.startsWith('http:') || providerPath.startsWith('https:')) {
  361. ret = new HttpProvider(providerPath, providerOptions);
  362. } else if (providerPath === 'promptfoo:redteam:iterative') {
  363. const RedteamIterativeProvider = (await import(path.join(__dirname, './redteam/iterative')))
  364. .default;
  365. ret = new RedteamIterativeProvider(providerOptions);
  366. } else {
  367. if (providerPath.startsWith('file://')) {
  368. providerPath = providerPath.slice('file://'.length);
  369. }
  370. // Load custom module
  371. const modulePath = path.join(basePath || process.cwd(), providerPath);
  372. const CustomApiProvider = await importModule(modulePath);
  373. ret = new CustomApiProvider(options);
  374. }
  375. ret.transform = options.transform;
  376. ret.delay = options.delay;
  377. return ret;
  378. }
  379. export default {
  380. OpenAiCompletionProvider,
  381. OpenAiChatCompletionProvider,
  382. OpenAiAssistantProvider,
  383. AnthropicCompletionProvider,
  384. ReplicateProvider,
  385. LocalAiCompletionProvider,
  386. LocalAiChatProvider,
  387. BAMChatProvider,
  388. BAMEmbeddingProvider,
  389. loadApiProvider,
  390. };
Tip!

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

Comments

Loading...