Thinking levels: Tab cycles off/low/medium/high, /effort
This commit is contained in:
1 parent
2f1ce2c072
commit
ab2af421a4
9 files changed
+180
-16
No files matched your search
@@ -120,6 +120,18 @@ pub fn body(req: &Request) -> Value {
|
||||
if !tools.is_empty() {
|
||||
b["tools"] = tools.into();
|
||||
}
|
||||
// ponytail: fixed token budgets; newer models may prefer adaptive thinking, add when needed
|
||||
let budget = match req.effort {
|
||||
crate::Effort::Off => 0,
|
||||
crate::Effort::Low => 4_000,
|
||||
crate::Effort::Medium => 10_000,
|
||||
crate::Effort::High => 24_000,
|
||||
};
|
||||
if budget > 0 {
|
||||
b["thinking"] = json!({"type": "enabled", "budget_tokens": budget});
|
||||
// the budget counts against max_tokens, which must leave room for the reply
|
||||
b["max_tokens"] = req.max_tokens.max(budget + 4_096).into();
|
||||
}
|
||||
b
|
||||
}
|
||||
|
||||
@@ -377,6 +389,16 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::ToolSpec;
|
||||
|
||||
#[test]
|
||||
fn effort_sets_thinking_budget() {
|
||||
let mut r = req();
|
||||
assert!(body(&r).get("thinking").is_none());
|
||||
r.effort = crate::Effort::High;
|
||||
let b = body(&r);
|
||||
assert_eq!(b["thinking"]["budget_tokens"], 24_000);
|
||||
assert!(b["max_tokens"].as_u64().unwrap() > 24_000);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn quota_and_models() {
|
||||
let mut h = reqwest::header::HeaderMap::new();
|
||||
@@ -427,6 +449,7 @@ mod tests {
|
||||
input_schema: json!({"type": "object"}),
|
||||
}],
|
||||
max_tokens: 1024,
|
||||
effort: crate::Effort::Off,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,8 +12,6 @@ pub struct Codex {
|
||||
auth: Auth,
|
||||
base_url: String,
|
||||
http: reqwest::Client,
|
||||
/// "none" | "minimal" | "low" | "medium" | "high"
|
||||
pub reasoning_effort: Option<String>,
|
||||
}
|
||||
|
||||
impl Codex {
|
||||
@@ -22,7 +20,6 @@ impl Codex {
|
||||
auth,
|
||||
base_url: "https://chatgpt.com/backend-api/codex".into(),
|
||||
http: reqwest::Client::new(),
|
||||
reasoning_effort: Some("medium".into()),
|
||||
}
|
||||
}
|
||||
pub fn login_default() -> Result<Self> {
|
||||
@@ -42,7 +39,7 @@ fn split_id(id: &str) -> (&str, Option<&str>) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn body(req: &Request, effort: Option<&str>) -> Value {
|
||||
pub fn body(req: &Request) -> Value {
|
||||
let mut input = vec![];
|
||||
for m in &req.messages {
|
||||
for b in &m.content {
|
||||
@@ -109,9 +106,12 @@ pub fn body(req: &Request, effort: Option<&str>) -> Value {
|
||||
"text": {"verbosity": "low"},
|
||||
"include": ["reasoning.encrypted_content"],
|
||||
});
|
||||
if let Some(e) = effort {
|
||||
b["reasoning"] = json!({"effort": e, "summary": "auto"});
|
||||
}
|
||||
// Off has no Codex equivalent (see Effort::levels); send the lowest level instead
|
||||
let e = match req.effort {
|
||||
crate::Effort::Off | crate::Effort::Low => "low",
|
||||
e => e.name(),
|
||||
};
|
||||
b["reasoning"] = json!({"effort": e, "summary": "auto"});
|
||||
b
|
||||
}
|
||||
|
||||
@@ -368,7 +368,7 @@ impl Provider for Codex {
|
||||
.header("OpenAI-Beta", "responses=experimental")
|
||||
.header("accept", "text/event-stream")
|
||||
.header("user-agent", concat!("hx/", env!("CARGO_PKG_VERSION")))
|
||||
.json(&body(&req, self.reasoning_effort.as_deref()))
|
||||
.json(&body(&req))
|
||||
.send()
|
||||
.await
|
||||
.map_err(|e| Error::Http(e.to_string()))?;
|
||||
@@ -528,8 +528,9 @@ mod tests {
|
||||
input_schema: json!({"type":"object"}),
|
||||
}],
|
||||
max_tokens: 100,
|
||||
effort: crate::Effort::Low,
|
||||
};
|
||||
let b = body(&req, Some("low"));
|
||||
let b = body(&req);
|
||||
let inp = b["input"].as_array().unwrap();
|
||||
assert_eq!(inp[0]["content"][0]["type"], "input_text");
|
||||
assert_eq!(inp[1]["type"], "reasoning");
|
||||
|
||||
@@ -138,6 +138,99 @@ pub enum StopReason {
|
||||
Other,
|
||||
}
|
||||
|
||||
/// How hard the model thinks before answering.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "lowercase")]
|
||||
pub enum Effort {
|
||||
Off,
|
||||
Low,
|
||||
#[default]
|
||||
Medium,
|
||||
High,
|
||||
}
|
||||
|
||||
impl Effort {
|
||||
pub const ALL: [Effort; 4] = [Effort::Off, Effort::Low, Effort::Medium, Effort::High];
|
||||
|
||||
/// Start level per provider: Claude thinks only when asked (token cost), Codex always reasons.
|
||||
pub fn default_for(k: auth::Kind) -> Self {
|
||||
match k {
|
||||
auth::Kind::Claude => Effort::Off,
|
||||
auth::Kind::Codex => Effort::Medium,
|
||||
}
|
||||
}
|
||||
|
||||
/// Levels the provider supports (Codex models can't switch reasoning off).
|
||||
pub fn levels(k: auth::Kind) -> &'static [Effort] {
|
||||
match k {
|
||||
auth::Kind::Claude => &Self::ALL,
|
||||
auth::Kind::Codex => &Self::ALL[1..],
|
||||
}
|
||||
}
|
||||
|
||||
/// Next level in `levels(k)`, wrapping; a level the provider lacks maps to its first.
|
||||
pub fn next(self, k: auth::Kind) -> Self {
|
||||
let l = Self::levels(k);
|
||||
l.iter()
|
||||
.position(|&e| e == self)
|
||||
.map_or(l[0], |i| l[(i + 1) % l.len()])
|
||||
}
|
||||
|
||||
pub fn name(self) -> &'static str {
|
||||
match self {
|
||||
Effort::Off => "off",
|
||||
Effort::Low => "low",
|
||||
Effort::Medium => "medium",
|
||||
Effort::High => "high",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod effort_tests {
|
||||
use super::*;
|
||||
use auth::Kind;
|
||||
|
||||
#[test]
|
||||
fn cycle_wraps_and_codex_skips_off() {
|
||||
let mut e = Effort::Off;
|
||||
let seen: Vec<_> = (0..5)
|
||||
.map(|_| {
|
||||
e = e.next(Kind::Claude);
|
||||
e
|
||||
})
|
||||
.collect();
|
||||
assert_eq!(
|
||||
seen,
|
||||
[
|
||||
Effort::Low,
|
||||
Effort::Medium,
|
||||
Effort::High,
|
||||
Effort::Off,
|
||||
Effort::Low
|
||||
]
|
||||
);
|
||||
assert_eq!(Effort::High.next(Kind::Codex), Effort::Low);
|
||||
assert_eq!(
|
||||
Effort::Off.next(Kind::Codex),
|
||||
Effort::Low,
|
||||
"unsupported level"
|
||||
);
|
||||
assert_eq!("high".parse::<Effort>(), Ok(Effort::High));
|
||||
assert!("max".parse::<Effort>().is_err());
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Effort {
|
||||
type Err = String;
|
||||
fn from_str(s: &str) -> std::result::Result<Self, String> {
|
||||
Self::ALL
|
||||
.into_iter()
|
||||
.find(|e| e.name() == s)
|
||||
.ok_or_else(|| format!("unknown level {s:?} (off, low, medium, high)"))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
|
||||
pub struct Request {
|
||||
pub model: String,
|
||||
@@ -145,6 +238,7 @@ pub struct Request {
|
||||
pub messages: Vec<Message>,
|
||||
pub tools: Vec<ToolSpec>,
|
||||
pub max_tokens: u32,
|
||||
pub effort: Effort,
|
||||
}
|
||||
|
||||
/// Incremental stream events. Providers must emit `Done` last with the full assembled message.
|
||||
|
||||
Reference in new issue
Block a user