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

full-eval.js 2.3 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
  1. import fs from 'node:fs';
  2. import promptfoo from '../../dist/src/index.js';
  3. const prompts = [
  4. // Prompts can be raw text...
  5. 'Write a haiku about: {{body}}',
  6. // Or message arrays...
  7. [
  8. {
  9. role: 'system',
  10. content: 'You are speaking to a time traveler. Begin your conversation.',
  11. },
  12. {
  13. role: 'user',
  14. content: '{{body}}',
  15. },
  16. ],
  17. // Or files
  18. 'file://prompt.txt',
  19. // Or functions
  20. (vars) => {
  21. const genres = ['fantasy', 'sci-fi', 'mystery', 'horror'];
  22. const characters = {
  23. fantasy: 'wizard',
  24. 'sci-fi': 'alien',
  25. mystery: 'detective',
  26. horror: 'ghost',
  27. };
  28. const genre = genres[Math.floor(Math.random() * genres.length)];
  29. const character = characters[genre];
  30. return [
  31. {
  32. role: 'system',
  33. content: `You have encountered a ${character} in a ${genre} setting. Engage with the character using the knowledge typical for that genre.`,
  34. },
  35. {
  36. role: 'user',
  37. content: '{{body}}',
  38. },
  39. ];
  40. },
  41. ];
  42. const customFunction = (prompt, context) => {
  43. // Call an LLM API in this function, or any other logic.
  44. console.log(`Prompt: ${prompt}, vars: ${JSON.stringify(context.vars)}`);
  45. return {
  46. output: '<LLM output>',
  47. };
  48. };
  49. customFunction.label = 'My custom function';
  50. const providers = [
  51. // Call the OpenAI API GPT 4o Mini
  52. 'openai:gpt-4o-mini',
  53. // Or use a custom function.
  54. customFunction,
  55. ];
  56. const tests = [
  57. {
  58. vars: {
  59. body: 'Hello world',
  60. },
  61. },
  62. {
  63. vars: {
  64. body: "I'm hungry",
  65. },
  66. assert: [
  67. {
  68. type: 'javascript',
  69. value: (output) => {
  70. // Logic for checking LLM output goes here...
  71. const pass = output.includes('foo bar');
  72. return {
  73. pass,
  74. score: pass ? 1.0 : 0.0,
  75. reason: pass ? 'Output contained substring' : 'Output did not contain substring',
  76. };
  77. },
  78. },
  79. ],
  80. },
  81. ];
  82. (async () => {
  83. const results = await promptfoo.evaluate({
  84. prompts,
  85. providers,
  86. tests,
  87. // Persist results locally so they are visible in the promptfoo viewer
  88. writeLatestResults: true,
  89. });
  90. console.log('RESULTS:');
  91. const resultsString = JSON.stringify(results, null, 2);
  92. console.log(resultsString);
  93. fs.writeFileSync('output.json', resultsString);
  94. console.log('Wrote output.json');
  95. })();
Tip!

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

Comments

Loading...