Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions crates/openhuman-core/src/config/ops/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ pub struct LocalAiSettingsPatch {
pub chat_model_id: Option<String>,
pub usage_embeddings: Option<bool>,
pub api_key: Option<String>,
/// See [`crate::config::schema::LocalAiConfig::native_tool_calling`].
pub native_tool_calling: Option<bool>,
}

/// Updates the model-related settings in the configuration.
Expand Down Expand Up @@ -442,6 +444,10 @@ pub async fn apply_local_ai_settings(
if let Some(v) = update.usage_embeddings {
config.local_ai.usage.embeddings = v;
}
if let Some(v) = update.native_tool_calling {
config.local_ai.native_tool_calling = v;
log::info!("[config][local_ai] native_tool_calling={v}");
}
if let Some(api_key) = update.api_key {
let trimmed = api_key.trim();
config.local_ai.api_key = if trimmed.is_empty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,7 @@ async fn apply_local_ai_settings_updates_lm_studio_provider_fields() {
chat_model_id: Some(" local-chat ".into()),
usage_embeddings: Some(true),
api_key: None,
native_tool_calling: Some(true),
};

let outcome = apply_local_ai_settings(&mut cfg, patch)
Expand All @@ -560,6 +561,7 @@ async fn apply_local_ai_settings_updates_lm_studio_provider_fields() {
assert_eq!(cfg.local_ai.model_id, "local-default");
assert_eq!(cfg.local_ai.chat_model_id, "local-chat");
assert!(cfg.local_ai.usage.embeddings);
assert!(cfg.local_ai.native_tool_calling);
assert_eq!(outcome.value["config"]["local_ai"]["provider"], "lm_studio");

let clear_and_fallback = LocalAiSettingsPatch {
Expand Down
13 changes: 13 additions & 0 deletions crates/openhuman-core/src/config/schema/local_ai.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ pub struct LocalAiConfig {
/// All default to `false` (cloud path).
#[serde(default)]
pub usage: LocalAiUsage,
/// Send native `tools` to the local runtime and read its structured tool
/// calls, instead of prompting the JSON-in-tag text protocol.
///
/// Off by default: many models on Ollama / LM Studio reject `tools` or
/// answer them badly. Turn it on for an endpoint whose model calls tools
/// natively, typically a hosted OpenAI-compatible gateway behind
/// `local-openai`. Such a gateway may also parse `<tool_call>` text in the
/// model's own template and hand the JSON-in-tag body back as the tool's
/// *name*, so the text protocol is not a safe fallback there.
#[serde(default)]
pub native_tool_calling: bool,
Comment thread
senamakel marked this conversation as resolved.
Resolved
}

impl std::fmt::Debug for LocalAiConfig {
Expand All @@ -118,6 +129,7 @@ impl std::fmt::Debug for LocalAiConfig {
.field("voice_llm_cleanup_enabled", &self.voice_llm_cleanup_enabled)
.field("num_ctx", &self.num_ctx)
.field("usage", &self.usage)
.field("native_tool_calling", &self.native_tool_calling)
.finish()
}
}
Expand Down Expand Up @@ -202,6 +214,7 @@ impl Default for LocalAiConfig {
voice_llm_cleanup_enabled: default_voice_llm_cleanup_enabled(),
num_ctx: None,
usage: LocalAiUsage::default(),
native_tool_calling: false,
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ pub(super) fn handle_update_local_ai_settings(
chat_model_id: update.chat_model_id,
usage_embeddings: update.usage_embeddings,
api_key: update.api_key,
native_tool_calling: update.native_tool_calling,
};
to_json(config_rpc::load_and_apply_local_ai_settings(&ctx, patch).await?)
})
Expand Down
1 change: 1 addition & 0 deletions crates/openhuman-core/src/config/schemas/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ pub(super) struct LocalAiSettingsUpdate {
pub(super) chat_model_id: Option<String>,
pub(super) usage_embeddings: Option<bool>,
pub(super) api_key: Option<String>,
pub(super) native_tool_calling: Option<bool>,
}

#[derive(Debug, Deserialize)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,10 @@ pub(super) fn lookup(function: &str) -> Option<ControllerSchema> {
"usage_embeddings",
"Use the local model for embedding generation (when runtime_enabled).",
),
optional_bool(
"native_tool_calling",
"Send native tools to the local runtime instead of the text tool protocol. For endpoints whose model calls tools natively.",
),
],
outputs: vec![json_output("snapshot", "Updated config snapshot.")],
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use super::*;
use crate::inference::provider::factory::access_gates::{
enforce_local_only_endpoint, is_loopback_endpoint, is_ollama_cloud_model, LocalOnlyRequestGuard,
};
use tinyinference_llm::providers::openai::build_local_runtime_chat_model;
use tinyinference_llm::providers::openai::build_local_runtime_chat_model_with_native_tools;
use tinyinference_llm::providers::openai::AuthStyle as CompatAuthStyle;

/// Local OpenAI-compatible runtimes (Ollama / LM Studio / MLX / OMLX /
Expand Down Expand Up @@ -153,7 +153,7 @@ fn try_create_local_runtime_chat_model_from_string_with_identity(
"[chat-factory] local runtime resolved slug={}",
resolved.kind.as_str()
);
let chat = build_local_runtime_chat_model(
let chat = build_local_runtime_chat_model_with_native_tools(
resolved.kind.as_str(),
&resolved.endpoint,
&resolved.api_key,
Expand All @@ -162,6 +162,7 @@ fn try_create_local_runtime_chat_model_from_string_with_identity(
&config.temperature_unsupported_models,
resolved.temperature,
resolved.num_ctx,
config.local_ai.native_tool_calling,
);
let chat: Arc<dyn ChatModel<()>> = Arc::new(LocalOnlyRequestGuard {
inner: chat,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,3 +228,36 @@ async fn non_openrouter_openai_compatible_endpoints_get_no_baked_provider_option
);
}
}

/// The model the turn actually runs must carry the same choice as session
/// assembly (`TurnModelSource::supports_native_tool_calling`): a profile with
/// `tool_calling = false` makes the harness strip `tools` off the wire and
/// fall back to the text protocol, whatever the prompt says.
#[tokio::test]
async fn local_openai_model_calls_tools_natively_only_when_opted_in() {
let _guard = crate::inference::inference_test_guard_async().await;
let _storage = crate::storage::test_support::install_backend_for_test(
crate::storage::test_support::memory_backend(),
);
let _signed_out = crate::platform::llm_gate::SignedOutTestGuard::set(true);
let dir = tempfile::tempdir().unwrap();
let mut config = Config {
config_path: dir.path().join("config.toml"),
workspace_dir: dir.path().join("workspace"),
..Config::default()
};
let tool_calling = |config: &Config| {
let config = config.clone();
async move {
create_chat_model_from_string("chat", "local-openai:glm-5.3-flash", &config, 0.0)
.await
.expect("local-openai builds")
.profile()
.expect("local-openai exposes a profile")
.tool_calling
}
};
assert!(!tool_calling(&config).await, "off by default");
config.local_ai.native_tool_calling = true;
assert!(tool_calling(&config).await, "on when opted in");
}
4 changes: 4 additions & 0 deletions crates/openhuman-core/src/inference/schemas/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ pub fn schemas(function: &str) -> ControllerSchema {
"usage_embeddings",
"Whether embeddings workload may use the local provider.",
),
optional_bool(
"native_tool_calling",
"Whether the local provider gets native tools instead of the text tool protocol.",
),
],
outputs: vec![json_output("snapshot", "Updated config snapshot.")],
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub(super) struct InferenceUpdateLocalSettingsParams {
chat_model_id: Option<String>,
usage_embeddings: Option<bool>,
api_key: Option<String>,
native_tool_calling: Option<bool>,
}

#[derive(Debug, Deserialize)]
Expand Down Expand Up @@ -186,6 +187,7 @@ pub(super) fn handle_inference_update_local_settings(
chat_model_id: update.chat_model_id,
usage_embeddings: update.usage_embeddings,
api_key: update.api_key,
native_tool_calling: update.native_tool_calling,
};
to_json(crate::inference::ops::inference_update_local_settings(&ctx, patch).await?)
})
Expand Down
13 changes: 11 additions & 2 deletions crates/openhuman-core/src/inference/turn_models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ impl TurnModelSource {
.unwrap_or_else(|| {
crate::inference::provider::provider_for_role(&source.role, &source.config)
});
return !tinyinference_local::profile::is_local_provider_string(&provider_string);
return provider_string_native_tools(&provider_string, &source.config);
}
false
}
Expand Down Expand Up @@ -609,7 +609,7 @@ impl TurnModelSource {
};
(
provider_id,
!is_local,
provider_string_native_tools(&provider_string, &cn.config),
!is_local,
is_local,
self_hosted,
Expand Down Expand Up @@ -711,6 +711,15 @@ impl TurnModelSource {
}
}

/// Whether a provider string's model gets native tools: cloud providers do; a
/// local runtime does only when the operator opted in with
/// `local_ai.native_tool_calling`. The local-runtime model factory reads the
/// same flag, so session assembly, the harness dialect and the wire agree.
fn provider_string_native_tools(provider_string: &str, config: &crate::config::Config) -> bool {
!tinyinference_local::profile::is_local_provider_string(provider_string)
|| config.local_ai.native_tool_calling
}

#[cfg(test)]
#[path = "turn_models_tests.rs"]
mod tests;
36 changes: 36 additions & 0 deletions crates/openhuman-core/src/inference/turn_models_native_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,3 +366,39 @@ async fn injected_turn_without_profile_keeps_conservative_capabilities() {
assert_eq!(models.context_window(), None);
assert!(models.primary.profile().is_none());
}

/// A `local-openai` endpoint can front a hosted gateway whose model calls
/// tools natively (Teeny's builder: `glm-5.3-flash` on Surplus). Driven with
/// the JSON-in-tag text protocol there, the gateway read each `<tool_call>`
/// body as the tool's *name*, so every call after the first failed with
/// ``unknown tool `{"arguments": …, "name": "shell"}` ``. Session assembly
/// must report native tools for a local runtime once the operator opts in,
/// and only then.
#[test]
fn a_local_runtime_reports_native_tools_only_when_opted_in() {
let ctx = crate::core::runtime::DispatchCtx::unbound();
let mut config = crate::config::Config::default();
let source = |config: &crate::config::Config| {
TurnModelSource::new_crate_native_from_string(
&ctx,
"chat",
"local-openai:glm-5.3-flash",
Arc::new(config.clone()),
)
};
assert!(
!source(&config).supports_native_tool_calling(),
"local runtimes stay on the text protocol by default"
);
// A cloud provider calls tools natively whatever the local flag says.
let cloud = TurnModelSource::new_crate_native_from_string(
&ctx,
"chat",
"openai:gpt-4o",
Arc::new(config.clone()),
);
assert!(cloud.supports_native_tool_calling());

config.local_ai.native_tool_calling = true;
assert!(source(&config).supports_native_tool_calling());
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ Capability {
name: "Configure Local Provider",
domain: "local_ai",
category: CapabilityCategory::LocalAI,
description: "Point OpenHuman at a local runtime you run yourself: Ollama, LM Studio, MLX, oMLX, or a generic OpenAI-compatible server. OpenHuman does not install the runtime or download models; pull them in your runtime (e.g. `ollama pull <model>`). Local chat inference does not require an OpenHuman account.",
how_to: "Connections → API keys → LLM, or use provider strings: ollama:<model>, lmstudio:<model>, mlx:<model>, omlx:<model>, local-openai:<model>",
description: "Point OpenHuman at a local runtime you run yourself: Ollama, LM Studio, MLX, oMLX, or a generic OpenAI-compatible server. OpenHuman does not install the runtime or download models; pull them in your runtime (e.g. `ollama pull <model>`). Local chat inference does not require an OpenHuman account. Local runtimes get tools through a text protocol in the prompt by default; an endpoint whose model calls tools natively (often a hosted gateway behind local-openai) can opt into native tool calls instead.",
how_to: "Connections → API keys → LLM, or use provider strings: ollama:<model>, lmstudio:<model>, mlx:<model>, omlx:<model>, local-openai:<model>. Native tool calls are off by default; turn them on with `native_tool_calling: true` on the core `config_update_local_ai_settings` (or `inference_update_local_settings`) method, and off again before switching to a model that rejects native tools.",
status: CapabilityStatus::Beta,
privacy: None,
},
Expand Down
Loading