make-llm-files/settings_to_flags.php
Frederico @ VilaRosa02 5a24d4946d BUG FIX:
2025-08-26 10:14:35 +00:00

74 lines
1.8 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"]
// }
if (!file_exists($argv[1])) {
// No settings file -> no extra flags
exit(0);
}
$json = json_decode(file_get_contents($argv[1]), true);
if (!is_array($json)) {
fwrite(STDERR, $argv[1]." is not valid JSON\n");
exit(0);
}
// 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 ($json as $key => $val) {
$k = $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);
}
// Print as a single line for easy inclusion in Makefile pipeline
echo implode(' ', $out);