-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathollama.js
More file actions
executable file
·271 lines (238 loc) · 8.78 KB
/
Copy pathollama.js
File metadata and controls
executable file
·271 lines (238 loc) · 8.78 KB
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
#!/usr/bin/env node
// Combined Ollama API provider and grader for promptfoo
//
// Auto-detects mode based on prompt format:
// - Grader mode: Prompt is JSON array [{role, content}, ...]
// - Provider mode: Prompt is plain text
//
// Makes HTTP requests to a local Ollama server
// Configurable via options: host (default localhost), port (default 11434), endpoint (default /api/v1/chat), model (default llama2)
const http = require('http');
const fs = require('fs');
const path = require('path');
const prompt = process.argv[2];
const options = process.argv[3];
const context = process.argv[4];
// Parse options to extract host, port, endpoint, model, and documentRoot
let host = 'localhost'; // Default local host
let port = 11434; // Default Ollama port
let endpoint = '/api/chat'; // Default Ollama endpoint
let model = 'llama2'; // Default model
let documentRoot = null; // Optional document root for file rendering
if (options && options !== '{}') {
try {
const optionsObj = JSON.parse(options);
if (optionsObj.config) {
if (optionsObj.config.host) {
host = optionsObj.config.host;
}
if (optionsObj.config.port) {
port = optionsObj.config.port;
}
if (optionsObj.config.endpoint) {
endpoint = optionsObj.config.endpoint;
}
if (optionsObj.config.model) {
model = optionsObj.config.model;
}
if (optionsObj.config.documentRoot) {
documentRoot = optionsObj.config.documentRoot;
}
}
} catch (e) {
// If JSON parsing fails, use defaults
}
}
// Detect mode: if prompt looks like a JSON array, use grader mode
let isGraderMode = false;
try {
const parsed = JSON.parse(prompt);
if (Array.isArray(parsed) && parsed.length > 0 && parsed[0].role) {
isGraderMode = true;
}
} catch (e) {
// Not JSON, so provider mode
}
// Helper function to render file content with nested @{filename} references
function renderFile(filePath, relativeRoot, visited = new Set()) {
const absolutePath = path.resolve(relativeRoot, filePath);
const normalizedPath = path.normalize(absolutePath);
// Detect circular references
if (visited.has(normalizedPath)) {
throw new Error(`Circular reference detected: ${filePath} from ${relativeRoot} (attempted path: ${normalizedPath})`);
}
visited.add(normalizedPath);
try {
if (!fs.existsSync(normalizedPath)) {
throw new Error(`No file named ${filePath} near ${relativeRoot} (attempted path: ${normalizedPath})`);
}
let content = fs.readFileSync(normalizedPath, 'utf8');
const fileDir = path.dirname(normalizedPath);
// Find and replace nested @{filename} references
const fileRefPattern = /@([^\s\n]+)/g;
let match;
const replacements = [];
while ((match = fileRefPattern.exec(content)) !== null) {
const refFileName = match[1];
replacements.push({
fullMatch: match[0],
fileName: refFileName,
index: match.index
});
}
// Process replacements in reverse order to maintain indices
for (let i = replacements.length - 1; i >= 0; i--) {
const replacement = replacements[i];
try {
const nestedContent = renderFile(replacement.fileName, fileDir, visited);
content = content.substring(0, replacement.index) + nestedContent + content.substring(replacement.index + replacement.fullMatch.length);
} catch (e) {
throw new Error(`Failed to render ${filePath} from ${relativeRoot} -> ${e.message}`);
}
}
return content;
} catch (e) {
if (e.message.startsWith('Failed to render')) {
throw e;
}
throw new Error(`Failed to render ${filePath} from ${relativeRoot}: ${e.message}`);
}
}
// Helper function to render a prompt string by expanding @{filename} references
function renderPrompt(promptText, documentRoot) {
// Find all @{filename} references in the prompt
// Match @filename where filename contains path separators and file extensions
// Stops at whitespace or sentence-ending punctuation
const fileRefPattern = /@([\w./_-]*\.[a-zA-Z0-9]+)/g;
let match;
const replacements = [];
while ((match = fileRefPattern.exec(promptText)) !== null) {
const refFileName = match[1];
replacements.push({
fullMatch: match[0],
fileName: refFileName,
index: match.index
});
}
// Process replacements in reverse order to maintain indices
let result = promptText;
for (let i = replacements.length - 1; i >= 0; i--) {
const replacement = replacements[i];
try {
// If reference contains .windsurf or other absolute-ish paths, resolve from cwd
// Otherwise (simple relative paths like ../SKILL.md), resolve from documentRoot
const resolveRoot = replacement.fileName.includes('.windsurf') || replacement.fileName.includes('evals') ? '.' : documentRoot;
const fileContent = renderFile(replacement.fileName, resolveRoot);
result = result.substring(0, replacement.index) + fileContent + result.substring(replacement.index + replacement.fullMatch.length);
} catch (e) {
throw new Error(`Failed to render ${replacement.fileName}: ${e.message}`);
}
}
return result;
}
// Helper function to make HTTP request to Ollama
function callOllama(input) {
const messages = [{
role: 'user',
content: input
}];
return new Promise((resolve, reject) => {
const payloadObj = {
model: model,
messages: messages,
stream: false
};
const payload = JSON.stringify(payloadObj);
const requestOptions = {
hostname: host,
port: port,
path: endpoint.startsWith('/') ? endpoint : '/' + endpoint,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(payload)
}
};
const req = http.request(requestOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
if (res.statusCode !== 200) {
reject(new Error(`Ollama API error: ${res.statusCode} - ${data}`));
return;
}
try {
const response = JSON.parse(data);
// Handle both native Ollama format and OpenAI-compatible format
let content;
if (response.message && response.message.content) {
// Native Ollama format
content = response.message.content;
} else if (response.choices && response.choices[0] && response.choices[0].message && response.choices[0].message.content) {
// OpenAI-compatible format (/v1/chat/completions)
content = response.choices[0].message.content;
} else {
reject(new Error(`Unexpected Ollama response format: ${JSON.stringify(response)}. Expected message.content or choices[0].message.content field.`));
return;
}
resolve(content);
} catch (e) {
reject(new Error(`Failed to parse Ollama response: ${e.message}`));
}
});
});
req.on('error', (e) => {
reject(new Error(`Failed to connect to Ollama at ${host}:${port}${endpoint}: ${e.message}`));
});
req.write(payload);
req.end();
});
}
async function main() {
try {
if (isGraderMode) {
// ===== GRADER MODE =====
// Parse the JSON chat message array that promptfoo sends to graders
let systemMsg, userMsg;
try {
const parsed = JSON.parse(prompt);
const systemMessage = parsed.find(m => m.role === 'system');
const userMessage = parsed.find(m => m.role === 'user');
if (systemMessage && userMessage) {
systemMsg = systemMessage.content;
userMsg = userMessage.content;
} else {
throw new Error('Missing system or user message');
}
} catch (e) {
// Fallback: treat the whole thing as a user message
systemMsg = 'You are an evaluator. Respond with only valid JSON: {"pass": bool, "score": 0.0-1.0, "reason": "string"}';
userMsg = prompt;
}
// Merge system prompt into user message for model compatibility
// Some models (like Mistral) don't support system role in their Jinja templates
const mergedUserMsg = systemMsg + '\n\n' + userMsg;
let output = await callOllama(mergedUserMsg);
const jsonMatch = output.match(/```json\s*([\s\S]*?)\s*```/);
if (jsonMatch) {
output = jsonMatch[1].trim();
}
console.log(output);
} else {
// ===== PROVIDER MODE =====
let userPrompt = prompt;
// Render file references if documentRoot is provided
if (documentRoot) {
userPrompt = renderPrompt(prompt, documentRoot);
}
const output = await callOllama(userPrompt);
console.log(output);
}
} catch (error) {
console.error(error.message);
process.exit(1);
}
}
main();