Expand description
Pluggable text-to-embedding hook.
Consumers that want to query the pipeline from natural-language text
(e.g. the GraphQL crate’s drillDown(queryText: ...), a REPL, a Python
shell helper) need a way to turn a &str into an Embedding without
this crate taking a dependency on any specific embedder. TextEmbedder
is that hook.
The trait is deliberately minimal: one fallible method, Send + Sync,
no lifetime parameters. Users implement it on their own newtype — a
thin wrapper around sentence-transformers, an OpenAI client, a local
ONNX model, or a deterministic hash — and hand an Arc<dyn TextEmbedder>
to whatever schema / pipeline / REPL consumes it.
Convenience types:
NoEmbedder— the default; returns an error onembed(). Wired into GraphQL schemas that haven’t configured a real embedder so text query resolvers fail with a clear message rather than panicking.FnEmbedder— a zero-cost wrapper that lifts a closure into the trait, for quick wiring in tests and examples.
§Example
ⓘ
use std::sync::Arc;
use sphereql_embed::text_embedder::{TextEmbedder, FnEmbedder, EmbedderError};
use sphereql_embed::types::Embedding;
let embedder: Arc<dyn TextEmbedder> = Arc::new(FnEmbedder::new(|text: &str| {
let len = text.len() as f64;
Ok(Embedding::new(vec![len, len.sqrt(), len.ln().max(0.0)]))
}));
let vec = embedder.embed("hello").unwrap();
assert_eq!(vec.dimension(), 3);Structs§
- FnEmbedder
- Zero-cost wrapper that lifts a closure into
TextEmbedder. - NoEmbedder
- Default embedder that always fails with a descriptive error.
Enums§
- Embedder
Error - Errors surfaced from a
TextEmbedderimplementation.
Traits§
- Text
Embedder - Turns free-form text into an
Embeddingsuitable for projection through the sphereQL pipeline.