Skip to content

Switchyard lets LLM applications route traffic across models and providers while preserving native OpenAI and Anthropic API compatibility - enabling flexible model selection, benchmarking, and cost/performance optimization.

Rust

chore: Move libsy-examples ensemble to libsy/examples (#102)

Graham King2fa6b87c870b
diff --git a/Cargo.lock b/Cargo.lock
index 9662c3551..0370a7e5e 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -906,18 +906,6 @@ version = "0.2.186"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "68ab91017fe16c622486840e4c83c9a37afeff978bd239b5293d61ece587de66"
-[[package]]
-name = "libsy-examples"
-version = "0.1.0"
-dependencies = [
- "async-trait",
- "futures",
- "switchyard-libsy",
- "switchyard-protocol",
- "tokio",
- "tokio-stream",
-]
-
[[package]]
name = "libyaml-rs"
version = "0.3.0"
@@ -1712,6 +1700,7 @@ dependencies = [
"futures",
"rand 0.8.7",
"serde_json",
+ "switchyard-llm-client",
"switchyard-protocol",
"tokio",
"tokio-stream",
diff --git a/Cargo.toml b/Cargo.toml
index 551d52cfa..3e78719b5 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,7 +5,6 @@
resolver = "2"
members = [
"crates/libsy",
- "crates/libsy-examples",
"crates/libsy-llm-client",
"crates/switchyard-components",
"crates/switchyard-components-v2",
diff --git a/crates/libsy-examples/Cargo.toml b/crates/libsy-examples/Cargo.toml
deleted file mode 100644
index 2f9ccfe55..000000000
--- a/crates/libsy-examples/Cargo.toml
+++ /dev/null
@@ -1,21 +0,0 @@
-# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
-# SPDX-License-Identifier: Apache-2.0
-
-[package]
-name = "libsy-examples"
-version = "0.1.0"
-description = "Example algorithms built on libsy"
-publish = false
-authors.workspace = true
-edition.workspace = true
-license.workspace = true
-repository.workspace = true
-rust-version.workspace = true
-
-[dependencies]
-switchyard-libsy = { path = "../libsy" }
-switchyard-protocol = { path = "../protocol" }
-async-trait = "0.1"
-futures = "0.3"
-tokio = { version = "1", features = ["full"] }
-tokio-stream = "0.1"
diff --git a/crates/libsy-examples/src/lib.rs b/crates/libsy-examples/src/lib.rs
deleted file mode 100644
index 505dcc9f8..000000000
--- a/crates/libsy-examples/src/lib.rs
+++ /dev/null
@@ -1,13 +0,0 @@
-// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
-// SPDX-License-Identifier: Apache-2.0
-
-//! Reference algorithms for [`libsy`], kept out of the core crate but compiled and
-//! tested here so they stay current. Each is a worked example of the
-//! [`Algorithm`](switchyard_libsy::Algorithm) trait; the `examples/` directory has runnable agents
-//! that drive them. The core crate provides [`switchyard_libsy::algorithms::Random`] for uniform random
-//! routing.
-//!
-//! - [`ensemble::EnsembleOrchAlgo`] — fan out to several models, judge, and commit
-//! (stateful).
-
-pub mod ensemble;
diff --git a/crates/libsy/Cargo.toml b/crates/libsy/Cargo.toml
index 911169e52..fca1dcf80 100644
--- a/crates/libsy/Cargo.toml
+++ b/crates/libsy/Cargo.toml
@@ -21,4 +21,5 @@ tokio = { version = "1", features = ["full"] }
tokio-stream = "0.1"
[dev-dependencies]
+switchyard-llm-client = { path = "../libsy-llm-client" }
tokio = { version = "1", features = ["macros", "rt"] }
diff --git a/crates/libsy/README.md b/crates/libsy/README.md
index 40a736f86..c687b3af7 100644
--- a/crates/libsy/README.md
+++ b/crates/libsy/README.md
@@ -260,11 +260,12 @@ agents live in [`examples`](examples/) folder.
(one call).
- [`LlmClassifier`](src/algorithms/llm_class.rs) — classify, then route
strong/weak; fail open to strong.
-- [`EnsembleOrchAlgo`](../libsy-examples/src/ensemble.rs) — stateful: fan out to
+- [`EnsembleOrchAlgo`](examples/ensemble.rs) — stateful: fan out to
candidates, judge the best, commit to the winner after N exploration turns.
-**Runnable agents** (`cargo run -p libsy --example <name>`):
+**Runnable agents** (`cargo run -p switchyard-libsy --example <name>`):
+- [`ensemble`](examples/ensemble.rs) — query three NVIDIA-hosted candidates, then judge and commit.
- [`research_agent`](examples/research_agent.rs) — client-backed
targets, `run` (libsy makes the calls).
- [`research_agent_core`](examples/research_agent_core.rs) — client-less
diff --git a/crates/libsy-examples/src/ensemble.rs b/crates/libsy/examples/ensemble.rs
similarity index 93%
rename from crates/libsy-examples/src/ensemble.rs
rename to crates/libsy/examples/ensemble.rs
index c151ee48e..3c9853769 100644
--- a/crates/libsy-examples/src/ensemble.rs
+++ b/crates/libsy/examples/ensemble.rs
@@ -9,6 +9,8 @@
//! requests and, after `exploration_turns` ensemble turns, commits to the
//! winningest model — every subsequent request routes straight to that one model
//! with no fan-out and no judge call.
+//! Run with:
+//! NVIDIA_API_KEY=... cargo run -p switchyard-libsy --example ensemble
//!
//! Unlike the reference routers, this algorithm is **stateful**: the win tally,
//! turn counter, and committed choice live behind a [`std::sync::Mutex`] so one
@@ -22,9 +24,20 @@ use std::sync::{Arc, Mutex};
use async_trait::async_trait;
-use switchyard_libsy::{Algorithm, Context, Decision, Driver, LlmTargetSet, Request, Response};
+use switchyard_libsy::{
+ Algorithm, Context, Decision, Driver, LlmTarget, LlmTargetSet, Request, Response,
+ RoutedLlmClient,
+};
+use switchyard_llm_client::{Backend, HttpBackendConfig, ModelConfig, TranslatingLlmClient};
use switchyard_protocol::{completion_text, prompt_text, text_request};
+const CANDIDATE_MODELS: [&str; 3] = [
+ "nvidia/qwen/qwen3.6-27b",
+ "nvidia/nvidia/nemotron-3-super-v3",
+ "nvidia/openai/gpt-oss-20b",
+];
+const JUDGE_MODEL: &str = "nvidia/deepseek-ai/deepseek-v4-flash";
+
/// Which step of the ensemble flow produced a decision.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum EnsemblePhase {
@@ -392,6 +405,55 @@ impl Algorithm for EnsembleOrchAlgo {
}
}
+#[tokio::main(flavor = "current_thread")]
+async fn main() -> Result<(), Box<dyn Error + Send + Sync>> {
+ let backend = HttpBackendConfig {
+ base_url: std::env::var("NVIDIA_BASE_URL")
+ .unwrap_or_else(|_| "https://inference-api.nvidia.com/v1".to_string()),
+ api_key: Some(std::env::var("NVIDIA_API_KEY")?),
+ extra_headers: BTreeMap::new(),
+ };
+ let models: Vec<_> = CANDIDATE_MODELS
+ .iter()
+ .copied()
+ .chain(std::iter::once(JUDGE_MODEL))
+ .map(|model| ModelConfig::new(model, Backend::OpenAiChat(backend.clone()), None))
+ .collect();
+ let client = Arc::new(TranslatingLlmClient::new(&models)?) as Arc<dyn RoutedLlmClient>;
+ let targets = LlmTargetSet::new(
+ CANDIDATE_MODELS
+ .iter()
+ .copied()
+ .chain(std::iter::once(JUDGE_MODEL))
+ .map(|model| LlmTarget {
+ semantic_name: model.to_string(),
+ llm_client: Some(client.clone()),
+ })
+ .collect(),
+ );
+ let algorithm: Arc<dyn Algorithm> = Arc::new(EnsembleOrchAlgo::new(
+ CANDIDATE_MODELS.map(str::to_string).to_vec(),
+ JUDGE_MODEL,
+ 1,
+ targets,
+ ));
+ let request = Request {
+ llm_request: text_request(
+ Some("ensemble".to_string()),
+ "Explain how LLM routing improves reliability.",
+ ),
+ raw_request: None,
+ metadata: None,
+ };
+
+ let (_, response) = algorithm.run(Context::default(), request).await?;
+ println!(
+ "{}",
+ completion_text(&response.llm_response.into_agg().await?)
+ );
+ Ok(())
+}
+
#[cfg(test)]
mod tests {
use super::*;