feat(harness,cli): expose model sampling temperature

Temperature was hardcoded to 0.2 in the API chat body. Add a
temperature_for() helper reading NEUROSPLOIT_TEMPERATURE (clamped to
0.0..=2.0, default 0.2) and a global --temperature CLI flag that sets it
before dispatch, so both run and the REPL honour it. HTTP API path only;
subscription-CLI backends expose no temperature knob.

Closes #49
This commit is contained in:
CyberSecurityUP committed 2026-11-04 20:34:56 -03:00
1 parent 92e8959f4e
commit 14afa7e8ca
2 files changed
+62 -1

No files matched your search

+15
View File
@@ -102,6 +102,12 @@ struct Cli {
/// box). Default: whichever is configured. See tools/laya_shim.py.
#[arg(long = "decision-backend", global = true)]
decision_backend: Option<String>,
/// Sampling temperature for model API calls (0.0..=2.0, default 0.2). Lower
/// is more deterministic/consistent, higher more exploratory (e.g. 0.0 fully
/// deterministic, 0.7 creative). Applies to the HTTP API path only; the
/// subscription-CLI backends (claude/codex/…) expose no temperature knob.
#[arg(long = "temperature", global = true)]
temperature: Option<f32>,
}
#[derive(Subcommand)]
@@ -816,6 +822,15 @@ async fn main() -> anyhow::Result<()> {
Some(ref m) if m == "on" || m == "true" || m == "1" || m == "auto" => std::env::set_var("NEUROSPLOIT_TYPESAFE", "on"),
_ => {}
}
// Sampling temperature → NEUROSPLOIT_TEMPERATURE, read at call time by the
// model client (so both `run` and the REPL honour it). Set before dispatch.
if let Some(t) = cli.temperature {
if (0.0..=2.0).contains(&t) {
std::env::set_var("NEUROSPLOIT_TEMPERATURE", t.to_string());
} else {
eprintln!(" \x1b[33m⚠ --temperature {t} out of range (0.0..=2.0) — ignoring, using default 0.2\x1b[0m");
}
}
// No subcommand → launch the Claude-Code-style interactive session.
let cmd = match cli.cmd.take() {
+47 -1
View File
@@ -190,7 +190,7 @@ impl ChatClient {
let body = serde_json::json!({
"model": m.model,
"max_tokens": max_tokens_for(&m.model),
"temperature": 0.2,
"temperature": temperature_for(),
"messages": [
{"role": "system", "content": system},
{"role": "user", "content": user}
@@ -951,6 +951,23 @@ impl Default for ChatClient {
}
}
/// Sampling temperature for an API chat completion. The default (0.2) keeps the
/// agent consistent and low-"creativity"; raise it for more exploratory runs
/// (0.7) or drop it to 0.0 for deterministic output. Override with
/// `NEUROSPLOIT_TEMPERATURE` (the `--temperature` CLI flag sets this). Values
/// outside the accepted 0.0..=2.0 range are ignored and fall back to the default.
///
/// Only the HTTP API path honours this; the subscription-CLI backends
/// (`chat_claude_stream` / `chat_codex_stream`) drive agentic CLIs that expose
/// no temperature knob.
fn temperature_for() -> f32 {
std::env::var("NEUROSPLOIT_TEMPERATURE")
.ok()
.and_then(|v| v.trim().parse::<f32>().ok())
.filter(|&v| (0.0..=2.0).contains(&v))
.unwrap_or(0.2)
}
/// Default output budget for a chat completion, keyed off the model family.
///
/// Reasoning models (GLM, DeepSeek, Qwen "thinking", the o-series, …) spend a
@@ -1113,6 +1130,35 @@ mod response_tests {
}
}
#[test]
fn temperature_defaults_and_overrides() {
// Env var drives the whole process, so keep this test self-contained and
// restore the prior value to avoid cross-test interference.
let prev = std::env::var("NEUROSPLOIT_TEMPERATURE").ok();
std::env::remove_var("NEUROSPLOIT_TEMPERATURE");
assert_eq!(temperature_for(), 0.2, "default temperature");
std::env::set_var("NEUROSPLOIT_TEMPERATURE", "0.0");
assert_eq!(temperature_for(), 0.0, "deterministic override");
std::env::set_var("NEUROSPLOIT_TEMPERATURE", "0.7");
assert_eq!(temperature_for(), 0.7, "exploratory override");
// Out-of-range and garbage fall back to the default.
std::env::set_var("NEUROSPLOIT_TEMPERATURE", "2.5");
assert_eq!(temperature_for(), 0.2, "above range → default");
std::env::set_var("NEUROSPLOIT_TEMPERATURE", "-1");
assert_eq!(temperature_for(), 0.2, "below range → default");
std::env::set_var("NEUROSPLOIT_TEMPERATURE", "hot");
assert_eq!(temperature_for(), 0.2, "unparseable → default");
match prev {
Some(v) => std::env::set_var("NEUROSPLOIT_TEMPERATURE", v),
None => std::env::remove_var("NEUROSPLOIT_TEMPERATURE"),
}
}
#[test]
fn extracts_plain_string_content() {
let msg = json!({"content": "hello"});