improvement: added settings.json support for dynamic llama.cpp flags

This commit is contained in:
Frederico @ VilaRosa02 2025-08-26 09:53:43 +00:00
parent 1d3d6b6584
commit 119ad98cdf
3 changed files with 93 additions and 2 deletions

View File

@ -6,8 +6,8 @@ LLM_FLAGS=--no-display-prompt -no-cnv --simple-io
SHELL=/bin/bash -x
agent_message.txt: llm_chat.php consolidated_system_message.txt user_message.txt
php $^ | $(LLM_BIN) -m $(LLM_MODEL) -f /dev/stdin $(LLM_FLAGS) 2> /dev/null | sed -e 's/\[end of text\]//' | tee $@
agent_message.txt: llm_chat.php consolidated_system_message.txt user_message.txt settings.json settings_to_flags.php
php $^ | $(LLM_BIN) -m $(LLM_MODEL) -f /dev/stdin $(LLM_FLAGS) `php settings_to_flags.php` 2> /dev/null | sed -e 's/\[end of text\]//' | tee $@
consolidated_system_message.txt: $(wildcard *.sys)
cat $^ > $@

1
settings.json Normal file
View File

@ -0,0 +1 @@
{}

90
settings_to_flags.php Executable file
View File

@ -0,0 +1,90 @@
#!/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);