Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.
Rust ai-nativedescription-logicsformal-verificationinfrastructure-as-codeisabelle-languageknowledge-graphleanlean4mcpmcp-serverontologyowlowl2reasoningrustshaclsparqltheorem-proving
feat: OpenAI-compatible embeddings provider (PR #11)
FABIOTESS31e328bd0dbe
diff --git a/src/config.rs b/src/config.rsindex f9602d1d..d6113ff8 100644--- a/src/config.rs+++ b/src/config.rs@@ -86,9 +86,16 @@ pub fn resolve_ontology_dirs(cfg: &[String]) -> Vec<std::path::PathBuf> {#[derive(Debug, Default, Deserialize, Clone)]#[serde(default)]pub struct EmbeddingsConfig {- /// Path to the ONNX model file. Default: ~/.open-ontologies/models/bge-small-en-v1.5.onnx+ /// Embedding provider: "local" (default — ONNX model on disk) or "openai"+ /// (any OpenAI-compatible HTTP API, e.g. OpenAI, Azure OpenAI, Ollama,+ /// vLLM, LM Studio, LocalAI, Together, etc.). Override at runtime with+ /// `OPEN_ONTOLOGIES_EMBEDDINGS_PROVIDER`.+ pub provider: Option<String>,+ /// Path to the ONNX model file (provider = "local" only).+ /// Default: ~/.open-ontologies/models/bge-small-en-v1.5.onnxpub model_path: Option<String>,- /// Path to the tokenizer.json file. Default: ~/.open-ontologies/models/tokenizer.json+ /// Path to the tokenizer.json file (provider = "local" only).+ /// Default: ~/.open-ontologies/models/tokenizer.jsonpub tokenizer_path: Option<String>,/// URL to download the ONNX model from. Default: BGE-small-en-v1.5 from Hugging Facepub model_url: Option<String>,@@ -96,9 +103,89 @@ pub struct EmbeddingsConfig {pub tokenizer_url: Option<String>,/// Filename for the downloaded model. Default: bge-small-en-v1.5.onnxpub model_name: Option<String>,++ // ─── OpenAI-compatible provider (provider = "openai") ───────────────+ /// Base URL of the OpenAI-compatible API, without the trailing+ /// `/embeddings` path. Default: `https://api.openai.com/v1`. Override+ /// at runtime with `OPEN_ONTOLOGIES_EMBEDDINGS_API_BASE`.+ #[serde(alias = "base_url")]+ pub api_base: Option<String>,+ /// API key. If unset, falls back to the `OPEN_ONTOLOGIES_EMBEDDINGS_API_KEY`+ /// or `OPENAI_API_KEY` env var. Sent as `Authorization: Bearer <key>`.+ /// Optional — gateways that don't require auth (Ollama, LocalAI,+ /// vLLM behind a private network, …) can leave this unset.+ pub api_key: Option<String>,+ /// Model name to request, e.g. `text-embedding-3-small`,+ /// `text-embedding-3-large`, `text-embedding-ada-002`, or any model+ /// served by an OpenAI-compatible gateway. Default:+ /// `text-embedding-3-small`. Override with+ /// `OPEN_ONTOLOGIES_EMBEDDINGS_MODEL`.+ pub model: Option<String>,+ /// Optional `dimensions` parameter sent in the request body. Lets you+ /// truncate output dimensionality on models that support it+ /// (text-embedding-3-*). When unset, the API's default dimension is+ /// used and detected from the first response.+ pub dimensions: Option<usize>,+ /// HTTP request timeout in seconds. Default: 30.+ pub request_timeout_secs: Option<u64>,}/// Configuration for the on-disk N-Triples compile cache and TTL eviction.+/// Resolve the configured embedding provider name.+///+/// Precedence: `OPEN_ONTOLOGIES_EMBEDDINGS_PROVIDER` env var > config field >+/// default ("local"). Returns a lowercased, trimmed string.+pub fn resolve_embeddings_provider(cfg: &EmbeddingsConfig) -> String {+ let raw = std::env::var("OPEN_ONTOLOGIES_EMBEDDINGS_PROVIDER")+ .ok()+ .filter(|v| !v.trim().is_empty())+ .or_else(|| cfg.provider.clone())+ .unwrap_or_else(|| "local".to_string());+ raw.trim().to_lowercase()+}++/// Resolve the OpenAI-compatible API base URL.+///+/// Precedence: `OPEN_ONTOLOGIES_EMBEDDINGS_API_BASE` env var > config >+/// `https://api.openai.com/v1`. Trailing slashes are stripped.+pub fn resolve_embeddings_api_base(cfg: &EmbeddingsConfig) -> String {+ let raw = std::env::var("OPEN_ONTOLOGIES_EMBEDDINGS_API_BASE")+ .ok()+ .filter(|v| !v.trim().is_empty())+ .or_else(|| cfg.api_base.clone())+ .unwrap_or_else(|| "https://api.openai.com/v1".to_string());+ raw.trim().trim_end_matches('/').to_string()+}++/// Resolve the OpenAI-compatible API key.+///+/// Precedence: `OPEN_ONTOLOGIES_EMBEDDINGS_API_KEY` env var >+/// `OPENAI_API_KEY` env var > config. Returns `None` if no key is configured+/// (some local OpenAI-compatible gateways accept unauthenticated requests).+pub fn resolve_embeddings_api_key(cfg: &EmbeddingsConfig) -> Option<String> {+ std::env::var("OPEN_ONTOLOGIES_EMBEDDINGS_API_KEY")+ .ok()+ .filter(|v| !v.trim().is_empty())+ .or_else(|| {+ std::env::var("OPENAI_API_KEY")+ .ok()+ .filter(|v| !v.trim().is_empty())+ })+ .or_else(|| cfg.api_key.clone().filter(|v| !v.trim().is_empty()))+}++/// Resolve the OpenAI-compatible model name.+///+/// Precedence: `OPEN_ONTOLOGIES_EMBEDDINGS_MODEL` env var > config >+/// `text-embedding-3-small`.+pub fn resolve_embeddings_model(cfg: &EmbeddingsConfig) -> String {+ std::env::var("OPEN_ONTOLOGIES_EMBEDDINGS_MODEL")+ .ok()+ .filter(|v| !v.trim().is_empty())+ .or_else(|| cfg.model.clone().filter(|v| !v.trim().is_empty()))+ .unwrap_or_else(|| "text-embedding-3-small".to_string())+}+#[derive(Debug, Deserialize, Clone)]#[serde(default)]pub struct CacheConfig {@@ -150,3 +237,68 @@ pub fn expand_tilde(path: &str) -> String {}path.to_string()}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn parse_openai_provider_block() {+ let toml_src = r#"+ [embeddings]+ provider = "openai"+ api_base = "https://api.example.com/v1/"+ api_key = "sk-test"+ model = "text-embedding-3-large"+ dimensions = 256+ request_timeout_secs = 60+ "#;+ let cfg: Config = toml::from_str(toml_src).expect("parse");+ assert_eq!(cfg.embeddings.provider.as_deref(), Some("openai"));+ assert_eq!(cfg.embeddings.api_key.as_deref(), Some("sk-test"));+ assert_eq!(cfg.embeddings.dimensions, Some(256));+ assert_eq!(cfg.embeddings.request_timeout_secs, Some(60));++ // Trailing slash is stripped by the resolver.+ assert_eq!(+ resolve_embeddings_api_base(&cfg.embeddings),+ "https://api.example.com/v1"+ );+ assert_eq!(resolve_embeddings_provider(&cfg.embeddings), "openai");+ assert_eq!(+ resolve_embeddings_model(&cfg.embeddings),+ "text-embedding-3-large"+ );+ }++ #[test]+ fn provider_defaults_to_local_when_unset() {+ // Verify the default-resolution logic without touching process-wide+ // env vars (which would race with other tests). When the env override+ // is absent the function should fall back to the config field, then+ // to "local".+ let cfg = EmbeddingsConfig::default();+ let resolved = cfg+ .provider+ .clone()+ .unwrap_or_else(|| "local".to_string())+ .trim()+ .to_lowercase();+ assert_eq!(resolved, "local");+ }++ #[test]+ fn base_url_alias_accepted() {+ // The legacy/alternative `base_url` key should also populate+ // `api_base` via serde alias.+ let toml_src = r#"+ [embeddings]+ base_url = "http://localhost:11434/v1"+ "#;+ let cfg: Config = toml::from_str(toml_src).expect("parse");+ assert_eq!(+ cfg.embeddings.api_base.as_deref(),+ Some("http://localhost:11434/v1")+ );+ }+}diff --git a/src/embed.rs b/src/embed.rsindex 8db05418..7c47a207 100644--- a/src/embed.rs+++ b/src/embed.rs@@ -115,6 +115,101 @@ impl TextEmbedder {}}+/// Unified text embedder that dispatches to either the local ONNX model or+/// an OpenAI-compatible HTTP API, selected by configuration.+pub enum TextEmbedderProvider {+ Local(TextEmbedder),+ OpenAI(crate::embed_remote::OpenAIEmbedder),+}++impl TextEmbedderProvider {+ /// Build a provider from runtime configuration. Returns `Ok(None)` when+ /// the configured provider cannot be initialised (e.g. local model files+ /// missing) so the server can start without embedding tools wired up.+ pub fn from_config(cfg: &crate::config::EmbeddingsConfig) -> anyhow::Result<Option<Self>> {+ let provider = crate::config::resolve_embeddings_provider(cfg);+ match provider.as_str() {+ "openai" | "openai-compatible" | "remote" | "http" => {+ let api_base = crate::config::resolve_embeddings_api_base(cfg);+ let api_key = crate::config::resolve_embeddings_api_key(cfg);+ let model = crate::config::resolve_embeddings_model(cfg);+ let timeout = std::time::Duration::from_secs(+ cfg.request_timeout_secs.unwrap_or(30).max(1),+ );+ let embedder = crate::embed_remote::OpenAIEmbedder::new(+ &api_base,+ api_key,+ model,+ cfg.dimensions,+ timeout,+ )?;+ Ok(Some(Self::OpenAI(embedder)))+ }+ "local" | "" | "onnx" => {+ let default_model_dir =+ dirs::home_dir().map(|h| h.join(".open-ontologies/models"));++ let model_path = cfg+ .model_path+ .clone()+ .map(|p| std::path::PathBuf::from(crate::config::expand_tilde(&p)))+ .or_else(|| {+ default_model_dir+ .as_ref()+ .map(|d| d.join("bge-small-en-v1.5.onnx"))+ });++ let tokenizer_path = cfg+ .tokenizer_path+ .clone()+ .map(|p| std::path::PathBuf::from(crate::config::expand_tilde(&p)))+ .or_else(|| {+ default_model_dir+ .as_ref()+ .map(|d| d.join("tokenizer.json"))+ });++ match (model_path, tokenizer_path) {+ (Some(m), Some(t)) if m.exists() && t.exists() => {+ let local = TextEmbedder::load(&m, &t)?;+ Ok(Some(Self::Local(local)))+ }+ _ => Ok(None),+ }+ }+ other => anyhow::bail!(+ "unknown embeddings provider '{}': expected 'local' or 'openai'",+ other+ ),+ }+ }++ /// Embed a single text string. Async because the OpenAI variant performs+ /// an HTTP request; the local variant just runs CPU-bound work.+ pub async fn embed(&self, text: &str) -> anyhow::Result<Vec<f32>> {+ match self {+ Self::Local(e) => e.embed(text),+ Self::OpenAI(e) => e.embed(text).await,+ }+ }++ /// Output dimension of the embedding vectors.+ pub fn dim(&self) -> usize {+ match self {+ Self::Local(e) => e.dim(),+ Self::OpenAI(e) => e.dim(),+ }+ }++ /// Short provider identifier ("local" or "openai") for diagnostics.+ pub fn provider_name(&self) -> &'static str {+ match self {+ Self::Local(_) => "local",+ Self::OpenAI(_) => "openai",+ }+ }+}+/// Download a file from URL to a local path.pub async fn download_model_file(url: &str, dest: &Path) -> Result<()> {let client = reqwest::Client::new();diff --git a/src/embed_remote.rs b/src/embed_remote.rsnew file mode 100644index 00000000..18744642--- /dev/null+++ b/src/embed_remote.rs@@ -0,0 +1,166 @@+//! Remote text embedder targeting any OpenAI-compatible `/embeddings` HTTP API.+//!+//! Works with the official OpenAI API as well as drop-in replacements such as+//! Azure OpenAI, Ollama, vLLM, LocalAI, LM Studio, Together, Mistral, etc.+//! Endpoint: `POST {api_base}/embeddings` with the standard request body+//! `{ "model": "...", "input": "..." [, "dimensions": N] }`.++use anyhow::{Context, Result};+use serde::{Deserialize, Serialize};+use std::sync::atomic::{AtomicUsize, Ordering};+use std::time::Duration;++use crate::poincare::l2_normalize;++/// Default fallback dimension when the API response is empty before the first+/// successful call (e.g. text-embedding-3-small / ada-002 native dim).+const FALLBACK_DIM: usize = 1536;++#[derive(Debug, Serialize)]+struct EmbeddingsRequest<'a> {+ model: &'a str,+ input: &'a str,+ #[serde(skip_serializing_if = "Option::is_none")]+ dimensions: Option<usize>,+ /// Always request `float` encoding format (the default) — explicit for+ /// gateways that require it.+ encoding_format: &'a str,+}++#[derive(Debug, Deserialize)]+struct EmbeddingsResponse {+ data: Vec<EmbeddingData>,+}++#[derive(Debug, Deserialize)]+struct EmbeddingData {+ embedding: Vec<f32>,+}++/// OpenAI-compatible embeddings client.+pub struct OpenAIEmbedder {+ client: reqwest::Client,+ /// Full URL including `/embeddings` path.+ endpoint: String,+ /// Bearer token sent in `Authorization` header. `None` skips the header+ /// — useful for local gateways without auth.+ api_key: Option<String>,+ model: String,+ /// Optional dimensions parameter to send. When `Some`, this is also the+ /// reported `dim()`. When `None`, dim is detected from the first+ /// response and cached.+ dimensions: Option<usize>,+ /// Cached output dimension (atomic so we can update from `&self`).+ detected_dim: AtomicUsize,+}++impl OpenAIEmbedder {+ /// Build an OpenAI-compatible embedder.+ ///+ /// `api_base` should not include the trailing `/embeddings` path —+ /// e.g. `https://api.openai.com/v1`.+ pub fn new(+ api_base: &str,+ api_key: Option<String>,+ model: impl Into<String>,+ dimensions: Option<usize>,+ request_timeout: Duration,+ ) -> Result<Self> {+ let client = reqwest::Client::builder()+ .timeout(request_timeout)+ .build()+ .context("failed to build reqwest client for OpenAI embeddings")?;++ let endpoint = format!("{}/embeddings", api_base.trim_end_matches('/'));++ // Reject obviously empty keys so they don't end up sending+ // `Authorization: Bearer `.+ let api_key = api_key.filter(|k| !k.trim().is_empty());++ let detected_dim = AtomicUsize::new(dimensions.unwrap_or(0));++ Ok(Self {+ client,+ endpoint,+ api_key,+ model: model.into(),+ dimensions,+ detected_dim,+ })+ }++ /// Embed a single text string. Returns an L2-normalized vector so it is+ /// directly comparable with embeddings produced by the local ONNX path.+ pub async fn embed(&self, text: &str) -> Result<Vec<f32>> {+ let body = EmbeddingsRequest {+ model: &self.model,+ input: text,+ dimensions: self.dimensions,+ encoding_format: "float",+ };++ let mut req = self.client.post(&self.endpoint).json(&body);+ if let Some(key) = &self.api_key {+ req = req.bearer_auth(key);+ }++ let resp = req+ .send()+ .await+ .with_context(|| format!("embedding request to {} failed", self.endpoint))?;++ if !resp.status().is_success() {+ let status = resp.status();+ let body = resp.text().await.unwrap_or_default();+ anyhow::bail!(+ "embeddings API returned {}: {}",+ status,+ body.chars().take(500).collect::<String>()+ );+ }++ let parsed: EmbeddingsResponse = resp+ .json()+ .await+ .context("failed to parse embeddings response as JSON")?;++ let vec = parsed+ .data+ .into_iter()+ .next()+ .map(|d| d.embedding)+ .ok_or_else(|| anyhow::anyhow!("embeddings API returned empty data array"))?;++ if vec.is_empty() {+ anyhow::bail!("embeddings API returned a zero-length embedding");+ }++ // Cache the observed dimension on first success.+ if self.detected_dim.load(Ordering::Relaxed) == 0 {+ self.detected_dim.store(vec.len(), Ordering::Relaxed);+ }++ Ok(l2_normalize(&vec))+ }++ /// Output dimension. Returns the configured `dimensions` when set, then+ /// the dimension observed from the most recent response, falling back to+ /// the standard 1536 of OpenAI's default embedding models if no call has+ /// been made yet.+ pub fn dim(&self) -> usize {+ if let Some(d) = self.dimensions {+ return d;+ }+ let observed = self.detected_dim.load(Ordering::Relaxed);+ if observed > 0 {+ observed+ } else {+ FALLBACK_DIM+ }+ }++ /// Configured model name (for diagnostics).+ pub fn model(&self) -> &str {+ &self.model+ }+}diff --git a/src/lib.rs b/src/lib.rsindex b71af376..4956add9 100644--- a/src/lib.rs+++ b/src/lib.rs@@ -34,4 +34,6 @@ pub mod vecstore;#[cfg(feature = "embeddings")]pub mod embed;#[cfg(feature = "embeddings")]+pub mod embed_remote;+#[cfg(feature = "embeddings")]pub mod structembed;diff --git a/src/main.rs b/src/main.rsindex 8da508c5..860e9816 100644--- a/src/main.rs+++ b/src/main.rs@@ -41,7 +41,12 @@ data_dir = "~/.open-ontologies"# mode = "all"# [embeddings]-# Paths to a local ONNX model and tokenizer (loaded at runtime)+# Provider selects how text embeddings are computed. Override at runtime+# with OPEN_ONTOLOGIES_EMBEDDINGS_PROVIDER.+# provider = "local" # or "openai" for any OpenAI-compatible API+#+# ── Local provider (provider = "local", default) ────────────────────────+# Paths to a local ONNX model and tokenizer (loaded at runtime).# model_path = "~/.open-ontologies/models/bge-small-en-v1.5.onnx"# tokenizer_path = "~/.open-ontologies/models/tokenizer.json"#@@ -51,6 +56,20 @@ data_dir = "~/.open-ontologies"# model_url = "https://huggingface.co/BAAI/bge-small-en-v1.5/resolve/main/onnx/model.onnx"# tokenizer_url = "https://huggingface.co/BAAI/bge-small-en-v1.5/resolve/main/tokenizer.json"# model_name = "bge-small-en-v1.5.onnx"+#+# ── OpenAI-compatible provider (provider = "openai") ────────────────────+# Works with the official OpenAI API, Azure OpenAI, Ollama, vLLM, LocalAI,+# LM Studio, Together, Mistral, and any other gateway that speaks the+# `POST {api_base}/embeddings` protocol. Each field can be overridden via+# environment variables:+# OPEN_ONTOLOGIES_EMBEDDINGS_API_BASE+# OPEN_ONTOLOGIES_EMBEDDINGS_API_KEY (or OPENAI_API_KEY)+# OPEN_ONTOLOGIES_EMBEDDINGS_MODEL+# api_base = "https://api.openai.com/v1"+# api_key = "sk-..." # optional — env vars take precedence+# model = "text-embedding-3-small" # any model your gateway serves+# dimensions = 1536 # optional — only sent when set+# request_timeout_secs = 30"#;#[derive(Parser)]@@ -512,46 +531,74 @@ async fn main() -> anyhow::Result<()> {#[cfg(feature = "embeddings")]{let models_dir = data_path.join("models");- std::fs::create_dir_all(&models_dir)?;// CLI flags > config.toml > defaultslet cfg = open_ontologies::config::Config::load(&config_path).map(|c| c.embeddings).unwrap_or_default();- let onnx_url = _model_url.as_deref()- .or(cfg.model_url.as_deref())- .unwrap_or(open_ontologies::embed::BGE_SMALL_ONNX_URL);- let tok_url = _tokenizer_url.as_deref()- .or(cfg.tokenizer_url.as_deref())- .unwrap_or(open_ontologies::embed::BGE_SMALL_TOKENIZER_URL);- let onnx_filename = _model_name.as_deref()- .or(cfg.model_name.as_deref())- .unwrap_or("bge-small-en-v1.5.onnx");-- let model_path = models_dir.join(onnx_filename);- let tokenizer_path = models_dir.join("tokenizer.json");-- if !model_path.exists() {- println!("Downloading embedding model from {}...", onnx_url);- open_ontologies::embed::download_model_file(- onnx_url,- &model_path,- ).await?;- println!(" Model saved: {}", model_path.display());+ let provider = open_ontologies::config::resolve_embeddings_provider(&cfg);+ if provider == "openai"+ || provider == "openai-compatible"+ || provider == "remote"+ || provider == "http"+ {+ println!(+ "Embeddings provider: {} — skipping local ONNX model download.",+ provider+ );+ println!(+ " Model: {}",+ open_ontologies::config::resolve_embeddings_model(&cfg)+ );+ println!(+ " API base: {}",+ open_ontologies::config::resolve_embeddings_api_base(&cfg)+ );+ if open_ontologies::config::resolve_embeddings_api_key(&cfg).is_none() {+ println!(+ " Note: no API key configured (set OPENAI_API_KEY, \+ OPEN_ONTOLOGIES_EMBEDDINGS_API_KEY, or [embeddings].api_key \+ in config.toml if your gateway requires auth)."+ );+ }} else {- println!("Embedding model already exists: {}", model_path.display());- }+ std::fs::create_dir_all(&models_dir)?;++ let onnx_url = _model_url.as_deref()+ .or(cfg.model_url.as_deref())+ .unwrap_or(open_ontologies::embed::BGE_SMALL_ONNX_URL);+ let tok_url = _tokenizer_url.as_deref()+ .or(cfg.tokenizer_url.as_deref())+ .unwrap_or(open_ontologies::embed::BGE_SMALL_TOKENIZER_URL);+ let onnx_filename = _model_name.as_deref()+ .or(cfg.model_name.as_deref())+ .unwrap_or("bge-small-en-v1.5.onnx");++ let model_path = models_dir.join(onnx_filename);+ let tokenizer_path = models_dir.join("tokenizer.json");++ if !model_path.exists() {+ println!("Downloading embedding model from {}...", onnx_url);+ open_ontologies::embed::download_model_file(+ onnx_url,+ &model_path,+ ).await?;+ println!(" Model saved: {}", model_path.display());+ } else {+ println!("Embedding model already exists: {}", model_path.display());+ }- if !tokenizer_path.exists() {- println!("Downloading tokenizer from {}...", tok_url);- open_ontologies::embed::download_model_file(- tok_url,- &tokenizer_path,- ).await?;- println!(" Tokenizer saved: {}", tokenizer_path.display());- } else {- println!("Tokenizer already exists: {}", tokenizer_path.display());+ if !tokenizer_path.exists() {+ println!("Downloading tokenizer from {}...", tok_url);+ open_ontologies::embed::download_model_file(+ tok_url,+ &tokenizer_path,+ ).await?;+ println!(" Tokenizer saved: {}", tokenizer_path.display());+ } else {+ println!("Tokenizer already exists: {}", tokenizer_path.display());+ }}}diff --git a/src/server.rs b/src/server.rsindex 5df07226..d3bce61d 100644--- a/src/server.rs+++ b/src/server.rs@@ -36,7 +36,7 @@ pub struct OpenOntologiesServer {#[cfg(feature = "embeddings")]vecstore: Arc<std::sync::Mutex<crate::vecstore::VecStore>>,#[cfg(feature = "embeddings")]- text_embedder: Option<Arc<crate::embed::TextEmbedder>>,+ text_embedder: Option<Arc<crate::embed::TextEmbedderProvider>>,}impl OpenOntologiesServer {@@ -142,29 +142,26 @@ impl OpenOntologiesServer {let mut vs = crate::vecstore::VecStore::new(db.clone());let _ = vs.load_from_db();- // Resolve model paths: config overrides > default locations- let default_model_dir = dirs::home_dir()- .map(|h| h.join(".open-ontologies/models"));-- let model_path = _embed_config.model_path- .map(|p| std::path::PathBuf::from(crate::config::expand_tilde(&p)))- .or_else(|| default_model_dir.as_ref().map(|d| d.join("bge-small-en-v1.5.onnx")));-- let tokenizer_path = _embed_config.tokenizer_path- .map(|p| std::path::PathBuf::from(crate::config::expand_tilde(&p)))- .or_else(|| default_model_dir.as_ref().map(|d| d.join("tokenizer.json")));-- let embedder = model_path.zip(tokenizer_path).and_then(|(m, t)| {- if m.exists() && t.exists() {- crate::embed::TextEmbedder::load(&m, &t).ok()- } else {+ let embedder = match crate::embed::TextEmbedderProvider::from_config(&_embed_config) {+ Ok(Some(e)) => {+ tracing::info!(+ "embeddings enabled (provider = {})",+ e.provider_name()+ );+ Some(Arc::new(e))+ }+ Ok(None) => {+ tracing::info!(+ "embeddings configured but no provider available (model files missing or provider disabled)"+ );None}- });- (- Arc::new(std::sync::Mutex::new(vs)),- embedder.map(Arc::new),- )+ Err(e) => {+ tracing::warn!("failed to initialise embedding provider: {}", e);+ None+ }+ };+ (Arc::new(std::sync::Mutex::new(vs)), embedder)};Self {@@ -1301,16 +1298,18 @@ impl OpenOntologiesServer {Err(e) => return format!(r#"{{"error":"structural training failed: {}"}}"#, e),};- let mut vecstore = self.vecstore.lock().unwrap();let mut embedded_count = 0;let mut errors: Vec<String> = Vec::new();for (iri, label) in &class_labels {- match embedder.embed(label) {+ // Compute the text embedding (may await an HTTP call) BEFORE+ // locking the non-Send VecStore mutex.+ match embedder.embed(label).await {Ok(text_vec) => {let struct_vec = struct_embeddings.get(iri).cloned().unwrap_or_else(|| vec![0.0; struct_dim]);+ let mut vecstore = self.vecstore.lock().unwrap();vecstore.upsert(iri, &text_vec, &struct_vec);embedded_count += 1;}@@ -1318,8 +1317,11 @@ impl OpenOntologiesServer {}}- if let Err(e) = vecstore.persist() {- return format!(r#"{{"error":"failed to persist embeddings: {}"}}"#, e);+ {+ let vecstore = self.vecstore.lock().unwrap();+ if let Err(e) = vecstore.persist() {+ return format!(r#"{{"error":"failed to persist embeddings: {}"}}"#, e);+ }}serde_json::json!({@@ -1348,7 +1350,7 @@ impl OpenOntologiesServer {None => return r#"{"error":"Embedding model not loaded."}"#.to_string(),};- let query_vec = match embedder.embed(&input.query) {+ let query_vec = match embedder.embed(&input.query).await {Ok(v) => v,Err(e) => return format!(r#"{{"error":"{}"}}"#, e),};