promptfoo 0.2.2 → 0.4.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.
- package/README.md +55 -17
- package/dist/evaluator.d.ts +1 -1
- package/dist/evaluator.d.ts.map +1 -1
- package/dist/evaluator.js +213 -150
- package/dist/evaluator.js.map +1 -1
- package/dist/main.js +51 -10
- package/dist/main.js.map +1 -1
- package/dist/prompts.d.ts +2 -0
- package/dist/prompts.d.ts.map +1 -0
- package/dist/prompts.js +21 -0
- package/dist/prompts.js.map +1 -0
- package/dist/providers.d.ts +1 -0
- package/dist/providers.d.ts.map +1 -1
- package/dist/providers.js +11 -5
- package/dist/providers.js.map +1 -1
- package/dist/tableOutput.html +5 -8
- package/dist/types.d.ts +27 -8
- package/dist/types.d.ts.map +1 -1
- package/dist/util.d.ts +5 -0
- package/dist/util.d.ts.map +1 -1
- package/dist/util.js +50 -1
- package/dist/util.js.map +1 -1
- package/dist/web/client/assets/index-710f1308.css +1 -0
- package/dist/web/client/assets/index-900b20c0.js +172 -0
- package/dist/web/client/favicon.ico +0 -0
- package/dist/web/client/index.html +15 -0
- package/dist/web/client/logo.svg +30 -0
- package/dist/web/server.d.ts +2 -0
- package/dist/web/server.d.ts.map +1 -0
- package/dist/web/server.js +74 -0
- package/dist/web/server.js.map +1 -0
- package/package.json +14 -3
- package/src/evaluator.ts +271 -174
- package/src/main.ts +52 -10
- package/src/prompts.ts +20 -0
- package/src/providers.ts +33 -15
- package/src/tableOutput.html +5 -8
- package/src/types.ts +32 -7
- package/src/util.ts +60 -1
- package/src/web/client/.eslintrc.cjs +14 -0
- package/src/web/client/index.html +13 -0
- package/src/web/client/package.json +37 -0
- package/src/web/client/public/favicon.ico +0 -0
- package/src/web/client/public/logo.svg +30 -0
- package/src/web/client/src/App.css +0 -0
- package/src/web/client/src/App.tsx +43 -0
- package/src/web/client/src/Logo.css +13 -0
- package/src/web/client/src/Logo.tsx +11 -0
- package/src/web/client/src/NavBar.css +3 -0
- package/src/web/client/src/NavBar.tsx +11 -0
- package/src/web/client/src/ResultsTable.css +133 -0
- package/src/web/client/src/ResultsTable.tsx +261 -0
- package/src/web/client/src/ResultsView.tsx +110 -0
- package/src/web/client/src/index.css +35 -0
- package/src/web/client/src/main.tsx +10 -0
- package/src/web/client/src/store.ts +13 -0
- package/src/web/client/src/types.ts +14 -0
- package/src/web/client/src/vite-env.d.ts +1 -0
- package/src/web/client/tsconfig.json +24 -0
- package/src/web/client/tsconfig.node.json +10 -0
- package/src/web/client/vite.config.ts +7 -0
- package/src/web/server.ts +96 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import fs from 'fs';
|
|
2
|
+
import path from 'node:path';
|
|
3
|
+
import readline from 'node:readline';
|
|
4
|
+
import http from 'node:http';
|
|
5
|
+
|
|
6
|
+
import debounce from 'debounce';
|
|
7
|
+
import open from 'open';
|
|
8
|
+
import express from 'express';
|
|
9
|
+
import cors from 'cors';
|
|
10
|
+
import { Server as SocketIOServer } from 'socket.io';
|
|
11
|
+
|
|
12
|
+
import promptfoo from '../index.js';
|
|
13
|
+
import logger from '../logger.js';
|
|
14
|
+
import { getDirectory } from '../esm.js';
|
|
15
|
+
import { getLatestResultsPath } from '../util.js';
|
|
16
|
+
|
|
17
|
+
import type { Request, Response } from 'express';
|
|
18
|
+
|
|
19
|
+
export function init(port = 15500) {
|
|
20
|
+
const app = express();
|
|
21
|
+
|
|
22
|
+
const staticDir = path.join(getDirectory(), 'web', 'client');
|
|
23
|
+
|
|
24
|
+
app.use(cors());
|
|
25
|
+
app.use(express.json());
|
|
26
|
+
app.use(express.static(staticDir));
|
|
27
|
+
|
|
28
|
+
const httpServer = http.createServer(app);
|
|
29
|
+
const io = new SocketIOServer(httpServer, {
|
|
30
|
+
cors: {
|
|
31
|
+
origin: '*',
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
interface EvaluateRequestBody {
|
|
36
|
+
provider: string;
|
|
37
|
+
options: {
|
|
38
|
+
prompts: string[];
|
|
39
|
+
vars: Record<string, string>[];
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
app.post('/evaluate', async (req: Request, res: Response) => {
|
|
44
|
+
try {
|
|
45
|
+
const { provider, options } = req.body as EvaluateRequestBody;
|
|
46
|
+
const summary = await promptfoo.evaluate(provider, options);
|
|
47
|
+
res.json(summary);
|
|
48
|
+
} catch (error) {
|
|
49
|
+
res.status(500).json({ message: 'Error evaluating prompts' });
|
|
50
|
+
}
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const latestJsonPath = getLatestResultsPath();
|
|
54
|
+
const readLatestJson = () => {
|
|
55
|
+
const data = fs.readFileSync(latestJsonPath, 'utf8');
|
|
56
|
+
const jsonData = JSON.parse(data);
|
|
57
|
+
return jsonData.table;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
io.on('connection', (socket) => {
|
|
61
|
+
// Send the initial table data when a client connects
|
|
62
|
+
socket.emit('init', { table: readLatestJson() });
|
|
63
|
+
|
|
64
|
+
// Watch for changes to latest.json and emit the update event
|
|
65
|
+
fs.watch(
|
|
66
|
+
latestJsonPath,
|
|
67
|
+
debounce((event: string) => {
|
|
68
|
+
if (event === 'change') {
|
|
69
|
+
socket.emit('update', { table: readLatestJson() });
|
|
70
|
+
}
|
|
71
|
+
}, 250),
|
|
72
|
+
);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
httpServer.listen(port, () => {
|
|
76
|
+
const url = `http://localhost:${port}`;
|
|
77
|
+
logger.info(`Server listening at ${url}`);
|
|
78
|
+
|
|
79
|
+
const rl = readline.createInterface({
|
|
80
|
+
input: process.stdin,
|
|
81
|
+
output: process.stdout,
|
|
82
|
+
});
|
|
83
|
+
rl.question('Do you want to open the browser to the URL? (y/N): ', async (answer) => {
|
|
84
|
+
if (answer.toLowerCase().startsWith('y')) {
|
|
85
|
+
try {
|
|
86
|
+
await open(url);
|
|
87
|
+
logger.info(`Opening browser to: ${url}`);
|
|
88
|
+
} catch (err) {
|
|
89
|
+
logger.error(`Failed to open browser: ${String(err)}`);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
rl.close();
|
|
93
|
+
logger.info('Press Ctrl+C to stop the server');
|
|
94
|
+
});
|
|
95
|
+
});
|
|
96
|
+
}
|