91 lines
2.4 KiB
PHP
Executable File
91 lines
2.4 KiB
PHP
Executable File
#!/usr/bin/php
|
|
<?php
|
|
// Reads settings.json (if present) and prints extra llama.cpp flags.
|
|
// Supports both OpenAI-style and llama.cpp-style names.
|
|
// Example settings.json:
|
|
// {
|
|
// "flags": {
|
|
// "temperature": 0.7,
|
|
// "max_output_tokens": 256,
|
|
// "top_p": 0.9,
|
|
// "repeat_penalty": 1.1,
|
|
// "seed": 42,
|
|
// "mirostat": 2
|
|
// },
|
|
// "raw": ["--no-penalize-nl"]
|
|
// }
|
|
|
|
$path = __DIR__ . '/settings.json';
|
|
if (!file_exists($path)) {
|
|
// No settings file -> no extra flags
|
|
exit(0);
|
|
}
|
|
|
|
$json = json_decode(file_get_contents($path), true);
|
|
if (!is_array($json)) {
|
|
fwrite(STDERR, "settings.json is not valid JSON\n");
|
|
exit(0);
|
|
}
|
|
|
|
$flags = isset($json['flags']) && is_array($json['flags']) ? $json['flags'] : [];
|
|
$raw = isset($json['raw']) && is_array($json['raw']) ? $json['raw'] : [];
|
|
|
|
// Normalize keys: strip leading dashes, kebab→snake, lowercase
|
|
$norm = function(string $k): string {
|
|
$k = ltrim($k, '-');
|
|
$k = strtolower(str_replace('-', '_', $k));
|
|
return $k;
|
|
};
|
|
|
|
// Map high-level keys to llama.cpp flags.
|
|
// Add more as needed.
|
|
$map = [
|
|
'temperature' => '--temp',
|
|
'temp' => '--temp',
|
|
'max_output_tokens' => '-n',
|
|
'n_predict' => '-n',
|
|
'top_p' => '--top-p',
|
|
'top_k' => '--top-k',
|
|
'repeat_penalty' => '--repeat-penalty',
|
|
'presence_penalty' => '--presence-penalty',
|
|
'frequency_penalty' => '--frequency-penalty',
|
|
'seed' => '--seed',
|
|
'ctx' => '-c',
|
|
'context_size' => '-c',
|
|
'mirostat' => '--mirostat',
|
|
'mirostat_tau' => '--mirostat-tau',
|
|
'mirostat_eta' => '--mirostat-eta',
|
|
];
|
|
|
|
$out = [];
|
|
|
|
// Structured flags
|
|
foreach ($flags as $key => $val) {
|
|
$k = $norm($key);
|
|
if (!isset($map[$k])) {
|
|
// Unknown key: ignore silently (or push to raw if you prefer)
|
|
continue;
|
|
}
|
|
$flag = $map[$k];
|
|
|
|
// Boolean flags: include only when true
|
|
if (is_bool($val)) {
|
|
if ($val) $out[] = $flag;
|
|
continue;
|
|
}
|
|
|
|
// Everything else: flag + value
|
|
// Use escapeshellarg for safety
|
|
$out[] = $flag . ' ' . escapeshellarg((string)$val);
|
|
}
|
|
|
|
// Raw passthrough flags (assumed already valid for llama.cpp)
|
|
foreach ($raw as $r) {
|
|
if (is_string($r) && strlen(trim($r)) > 0) {
|
|
$out[] = $r;
|
|
}
|
|
}
|
|
|
|
// Print as a single line for easy inclusion in Makefile pipeline
|
|
echo implode(' ', $out);
|