Skip to content

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

feat(embeddings): validate custom endpoint before constructing provider

Steven Enamakel1dfe24e52299
diff --git a/src/openhuman/inference/embeddings/factory.rs b/src/openhuman/inference/embeddings/factory.rs
index 6ed86ba4cc..ba3fac9c03 100644
--- a/src/openhuman/inference/embeddings/factory.rs
+++ b/src/openhuman/inference/embeddings/factory.rs
@@ -33,14 +33,17 @@ fn custom_openai_provider(
api_key: &str,
model: &str,
dims: usize,
-) -> Box<dyn EmbeddingProvider> {
+) -> anyhow::Result<Box<dyn EmbeddingProvider>> {
+ let base_url = validate_custom_endpoint(base_url, !api_key.is_empty())?;
if dims == 0 {
- Box::new(DimensionAgnosticOpenAiProbe::new(
- openai_model(base_url, api_key, model, 0, false),
+ Ok(Box::new(DimensionAgnosticOpenAiProbe::new(
+ openai_model(&base_url, api_key, model, 0, false),
api_key,
- ))
+ )))
} else {
- TinyAgentsEmbeddingProvider::boxed(openai_model(base_url, api_key, model, dims, false))
+ Ok(TinyAgentsEmbeddingProvider::boxed(openai_model(
+ &base_url, api_key, model, dims, false,
+ )))
}
}
@@ -274,7 +277,7 @@ pub fn create_embedding_provider(
)),
name if name.starts_with("custom:") => {
let base_url = name.strip_prefix("custom:").unwrap_or("");
- Ok(custom_openai_provider(base_url, "", model, dims))
+ custom_openai_provider(base_url, "", model, dims)
}
"none" => Ok(TinyAgentsEmbeddingProvider::boxed(NoopEmbeddingModel)),
unknown => Err(anyhow::anyhow!(
@@ -328,11 +331,11 @@ pub fn create_embedding_provider_with_credentials(
)),
"custom" => {
let url = custom_endpoint.unwrap_or("");
- Ok(custom_openai_provider(url, api_key, model, dims))
+ custom_openai_provider(url, api_key, model, dims)
}
name if name.starts_with("custom:") => {
let url = custom_endpoint.unwrap_or_else(|| name.strip_prefix("custom:").unwrap_or(""));
- Ok(custom_openai_provider(url, api_key, model, dims))
+ custom_openai_provider(url, api_key, model, dims)
}
"none" => Ok(TinyAgentsEmbeddingProvider::boxed(NoopEmbeddingModel)),
unknown => Err(anyhow::anyhow!(
diff --git a/src/openhuman/inference/embeddings/mod_tests.rs b/src/openhuman/inference/embeddings/mod_tests.rs
index ce8a0d5f7d..2a34b2697b 100644
--- a/src/openhuman/inference/embeddings/mod_tests.rs
+++ b/src/openhuman/inference/embeddings/mod_tests.rs
@@ -65,8 +65,9 @@ fn factory_custom_url() {
#[test]
fn factory_custom_empty_url() {
- let p = create_embedding_provider("custom:", "model", 768).unwrap();
- assert_eq!(p.name(), "openai");
+ let error = create_embedding_provider("custom:", "model", 768)
+ .expect_err("an empty custom endpoint must be rejected before construction");
+ assert!(error.to_string().contains("must not be empty"));
}
#[test]