promptfoo 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/LICENSE +19 -0
  2. package/README.md +353 -0
  3. package/dist/__mocks__/esm.d.ts +2 -0
  4. package/dist/__mocks__/esm.d.ts.map +1 -0
  5. package/dist/__mocks__/esm.js +4 -0
  6. package/dist/__mocks__/esm.js.map +1 -0
  7. package/dist/esm.d.ts +2 -0
  8. package/dist/esm.d.ts.map +1 -0
  9. package/dist/esm.js +9 -0
  10. package/dist/esm.js.map +1 -0
  11. package/dist/evaluator.d.ts +3 -0
  12. package/dist/evaluator.d.ts.map +1 -0
  13. package/dist/evaluator.js +162 -0
  14. package/dist/evaluator.js.map +1 -0
  15. package/dist/index.d.ts +7 -0
  16. package/dist/index.d.ts.map +1 -0
  17. package/dist/index.js +29 -0
  18. package/dist/index.js.map +1 -0
  19. package/dist/logger.d.ts +11 -0
  20. package/dist/logger.d.ts.map +1 -0
  21. package/dist/logger.js +38 -0
  22. package/dist/logger.js.map +1 -0
  23. package/dist/main.d.ts +3 -0
  24. package/dist/main.d.ts.map +1 -0
  25. package/dist/main.js +90 -0
  26. package/dist/main.js.map +1 -0
  27. package/dist/providers.d.ts +21 -0
  28. package/dist/providers.d.ts.map +1 -0
  29. package/dist/providers.js +145 -0
  30. package/dist/providers.js.map +1 -0
  31. package/dist/tableOutput.html +55 -0
  32. package/dist/types.d.ts +55 -0
  33. package/dist/types.d.ts.map +1 -0
  34. package/dist/types.js +2 -0
  35. package/dist/types.js.map +1 -0
  36. package/dist/util.d.ts +6 -0
  37. package/dist/util.d.ts.map +1 -0
  38. package/dist/util.js +62 -0
  39. package/dist/util.js.map +1 -0
  40. package/package.json +55 -0
  41. package/src/__mocks__/esm.ts +3 -0
  42. package/src/esm.ts +10 -0
  43. package/src/evaluator.ts +203 -0
  44. package/src/index.ts +35 -0
  45. package/src/logger.ts +38 -0
  46. package/src/main.ts +108 -0
  47. package/src/providers.ts +170 -0
  48. package/src/tableOutput.html +55 -0
  49. package/src/types.ts +63 -0
  50. package/src/util.ts +67 -0
package/src/util.ts ADDED
@@ -0,0 +1,67 @@
1
+ import * as fs from 'fs';
2
+
3
+ import yaml from 'js-yaml';
4
+ import nunjucks from 'nunjucks';
5
+ import { parse as parsePath } from 'path';
6
+ import { CsvRow } from './types.js';
7
+ import { parse as parseCsv } from 'csv-parse/sync';
8
+ import { stringify } from 'csv-stringify/sync';
9
+
10
+ import { getDirectory } from './esm.js';
11
+
12
+ import type { EvaluateSummary } from './types.js';
13
+
14
+ const PROMPT_DELIMITER = '---';
15
+
16
+ function parseJson(json: string): any | undefined {
17
+ try {
18
+ return JSON.parse(json);
19
+ } catch (err) {
20
+ return undefined;
21
+ }
22
+ }
23
+
24
+ export function readPrompts(promptPaths: string[]): string[] {
25
+ let promptContents = promptPaths.map((path) => fs.readFileSync(path, 'utf-8'));
26
+ if (promptContents.length === 1) {
27
+ promptContents = promptContents[0].split(PROMPT_DELIMITER).map((p) => p.trim());
28
+ }
29
+ return promptContents;
30
+ }
31
+
32
+ export function readVars(varsPath: string): CsvRow[] {
33
+ const fileExtension = parsePath(varsPath).ext.slice(1);
34
+ let rows: CsvRow[] = [];
35
+
36
+ if (fileExtension === 'csv') {
37
+ rows = parseCsv(fs.readFileSync(varsPath, 'utf-8'), { columns: true });
38
+ } else if (fileExtension === 'json') {
39
+ rows = parseJson(fs.readFileSync(varsPath, 'utf-8'));
40
+ } else if (fileExtension === 'yaml' || fileExtension === 'yml') {
41
+ rows = yaml.load(fs.readFileSync(varsPath, 'utf-8')) as unknown as any;
42
+ }
43
+
44
+ return rows;
45
+ }
46
+
47
+ export function writeOutput(outputPath: string, summary: EvaluateSummary): void {
48
+ const outputExtension = outputPath.split('.').pop()?.toLowerCase();
49
+
50
+ if (outputExtension === 'csv' || outputExtension === 'txt') {
51
+ const csvOutput = stringify(summary.table);
52
+ fs.writeFileSync(outputPath, csvOutput);
53
+ } else if (outputExtension === 'json') {
54
+ fs.writeFileSync(outputPath, JSON.stringify(summary, null, 2));
55
+ } else if (outputExtension === 'yaml' || outputExtension === 'yml') {
56
+ fs.writeFileSync(outputPath, yaml.dump(summary));
57
+ } else if (outputExtension === 'html') {
58
+ const template = fs.readFileSync(`${getDirectory()}/tableOutput.html`, 'utf-8');
59
+ const htmlOutput = nunjucks.renderString(template, {
60
+ table: summary.table,
61
+ results: summary.results,
62
+ });
63
+ fs.writeFileSync(outputPath, htmlOutput);
64
+ } else {
65
+ throw new Error('Unsupported output file format. Use CSV, JSON, or YAML.');
66
+ }
67
+ }