Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(tinyagents): add embedding-based ranker for agent discovery

Steven Enamakel52626a020470
diff --git a/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker.rs b/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker.rs
index 6a51f20773..a6cbb2203d 100644
--- a/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker.rs
@@ -1,13 +1,13 @@
//! Host wiring for the harness `EmbeddingToolRanker`: adapt the process's
//! `EmbeddingProvider` onto the vendor `EmbeddingModel` and decide whether the
//! provider can embed at all. The ranker itself (cosine ranking, the in-memory
-//! and on-disk catalogue cache) lives in `tinyagents-harness`
-//! (`tool::discover::EmbeddingToolRanker`).
+//! and durable catalogue cache) lives in `tinyagents-harness`
+//! (`tool::discover::EmbeddingToolRanker`, `DriverEmbeddingCache`).
use std::sync::Arc;
use async_trait::async_trait;
-use tinyagents_harness::tool::discover::EmbeddingToolRanker;
+use tinyagents_harness::tool::discover::{DriverEmbeddingCache, EmbeddingToolRanker};
use tinyinference_embeddings::{EmbeddingModel, Error as EmbedError, Result as EmbedResult};
use crate::inference::embedding_host::EmbeddingProvider;
@@ -43,13 +43,34 @@ impl EmbeddingModel for RankerEmbeddingModel {
}
/// A ranker over `provider` with an in-memory cache; chain
-/// `.with_disk_cache(path)` to persist it. The cache is keyed by the provider's
-/// embedding-space signature, so a model change invalidates it.
+/// `.with_cache(cache)` to persist it (see [`embedding_tool_ranker_in`]). The
+/// cache is keyed by the provider's embedding-space signature, so a model
+/// change invalidates it.
pub fn embedding_tool_ranker(provider: Arc<dyn EmbeddingProvider>) -> EmbeddingToolRanker {
let signature = provider.signature();
EmbeddingToolRanker::new(Arc::new(RankerEmbeddingModel(provider)), signature)
}
+/// A ranker over `provider` whose catalogue embeddings are cached in the
+/// storage backend, in the calling scope (`DriverEmbeddingCache`). When the
+/// backend cannot be had the ranker still works, with its in-memory cache
+/// only.
+pub fn embedding_tool_ranker_in(
+ provider: Arc<dyn EmbeddingProvider>,
+ workspace_dir: &std::path::Path,
+) -> EmbeddingToolRanker {
+ let ranker = embedding_tool_ranker(provider);
+ match crate::storage::local::open("tool_search", workspace_dir).and_then(|o| o.scoped()) {
+ Ok(scoped) => ranker.with_cache(Arc::new(DriverEmbeddingCache::new(Arc::clone(
+ scoped.documents(),
+ )))),
+ Err(error) => {
+ log::warn!("[tool-search] embedding cache unavailable; in-memory only: {error:#}");
+ ranker
+ }
+ }
+}
+
/// Whether `provider` can embed at all. The `none` provider embeds nothing and
/// would rank everything at zero.
pub fn embedding_provider_is_usable(provider: &dyn EmbeddingProvider) -> bool {
diff --git a/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker_tests.rs b/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker_tests.rs
index 0af9bb17a8..1a7a2f41f5 100644
--- a/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker_tests.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/discovery/embedding_ranker_tests.rs
@@ -50,23 +50,35 @@ fn candidates() -> Vec<RankCandidate> {
}
/// The provider adapter carries the embedding through to the vendor ranker, and
-/// the provider's signature keys the disk cache it writes.
+/// the provider's signature keys the cache it writes into the storage backend.
#[tokio::test]
async fn the_ranker_ranks_through_the_provider_and_keys_its_cache_by_signature() {
let tmp = tempfile::tempdir().unwrap();
- let path = tmp.path().join("cache.json");
let embedder = Arc::new(BagEmbedder {
calls: AtomicUsize::new(0),
});
- let ranker = embedding_tool_ranker(embedder.clone()).with_disk_cache(path.clone());
+ let ranker = embedding_tool_ranker_in(embedder.clone(), tmp.path());
let hits = ranker
.rank("ping alex", &RankContext::empty(), &candidates(), 1)
.await
.unwrap();
assert_eq!(hits[0].key, "SLACK_SEND_MESSAGE");
- let on_disk: serde_json::Value =
- serde_json::from_slice(&std::fs::read(&path).unwrap()).unwrap();
- assert_eq!(on_disk["signature"], "provider=bag;model=bag-v1;dims=3");
+ let after_first = embedder.calls.load(std::sync::atomic::Ordering::SeqCst);
+
+ // A second ranker over the same workspace reads the stored catalogue
+ // embeddings instead of embedding the catalogue again.
+ let again = embedding_tool_ranker_in(embedder.clone(), tmp.path());
+ let hits = again
+ .rank("ping alex", &RankContext::empty(), &candidates(), 1)
+ .await
+ .unwrap();
+ assert_eq!(hits[0].key, "SLACK_SEND_MESSAGE");
+ let after_second = embedder.calls.load(std::sync::atomic::Ordering::SeqCst);
+ assert_eq!(
+ after_second - after_first,
+ 1,
+ "only the query was embedded the second time, not the catalogue"
+ );
}
#[test]
diff --git a/crates/openhuman-core/src/agent/tinyagents/discovery/mod.rs b/crates/openhuman-core/src/agent/tinyagents/discovery/mod.rs
index 808964dee8..9b1a63347a 100644
--- a/crates/openhuman-core/src/agent/tinyagents/discovery/mod.rs
+++ b/crates/openhuman-core/src/agent/tinyagents/discovery/mod.rs
@@ -218,7 +218,9 @@ impl ToolRanker for OverlapRanker {
mod embedding_ranker;
-pub use embedding_ranker::{embedding_provider_is_usable, embedding_tool_ranker};
+pub use embedding_ranker::{
+ embedding_provider_is_usable, embedding_tool_ranker, embedding_tool_ranker_in,
+};
#[cfg(test)]
#[path = "discovery_tests.rs"]
diff --git a/crates/openhuman-tinyhumans/src/jev/ranker.rs b/crates/openhuman-tinyhumans/src/jev/ranker.rs
index 339e906bb8..794e62ef7d 100644
--- a/crates/openhuman-tinyhumans/src/jev/ranker.rs
+++ b/crates/openhuman-tinyhumans/src/jev/ranker.rs
@@ -10,7 +10,7 @@ use std::{
use std::{future::Future, pin::Pin, sync::Arc, time::Duration};
use openhuman_embed::__host::agent::tinyagents::discovery::{
- embedding_provider_is_usable, embedding_tool_ranker,
+ embedding_provider_is_usable, embedding_tool_ranker_in,
};
use openhuman_embed::__host::config::Config;
use tinyjevclient::Client;
@@ -205,12 +205,7 @@ fn retriever_for(config: &Config) -> Result<Arc<dyn ToolRanker>, RankError> {
provider.model_id()
);
Ok(Arc::new(
- embedding_tool_ranker(provider).with_disk_cache(
- config
- .workspace_dir
- .join("cache")
- .join("tool_search_embeddings.json"),
- ),
+ embedding_tool_ranker_in(provider, &config.workspace_dir),
))
}