pub struct ModelRuntime { /* private fields */ }Expand description
The central runtime that owns registered providers and a catalog of model aliases.
Obtain an instance via ModelRuntime::builder() and the
ModelRuntimeBuilder. Once built, use embedding,
reranker, or generator to obtain
typed, instrumented model handles.
Models are loaded lazily on first access (unless configured for eager or background warmup) and cached in an internal registry so that subsequent requests for the same model are served instantly.
Implementations§
Source§impl ModelRuntime
impl ModelRuntime
Sourcepub fn builder() -> ModelRuntimeBuilder
pub fn builder() -> ModelRuntimeBuilder
Create a new ModelRuntimeBuilder for configuring and constructing a
runtime.
Sourcepub async fn register(&self, spec: ModelAliasSpec) -> Result<()>
pub async fn register(&self, spec: ModelAliasSpec) -> Result<()>
Register a new model alias at runtime.
Sourcepub async fn contains_alias(&self, alias: &str) -> bool
pub async fn contains_alias(&self, alias: &str) -> bool
Check if an alias exists in the catalog.
Sourcepub async fn prefetch_all(&self) -> Result<()>
pub async fn prefetch_all(&self) -> Result<()>
Pre-load and cache every model in the catalog.
Models already loaded are skipped. Fails fast on the first error. Call this during application startup to avoid cold-start latency on first inference.
Sourcepub async fn prefetch(&self, aliases: &[&str]) -> Result<()>
pub async fn prefetch(&self, aliases: &[&str]) -> Result<()>
Pre-load and cache specific aliases.
Returns an error immediately if an alias is not found in the catalog or if any model fails to load. Models already loaded are skipped.
Sourcepub async fn embedding(&self, alias: &str) -> Result<Arc<dyn EmbeddingModel>>
pub async fn embedding(&self, alias: &str) -> Result<Arc<dyn EmbeddingModel>>
Resolve, load (if necessary), and return an instrumented EmbeddingModel
handle for the given alias.
The returned handle is cached per alias so that repeated calls skip spec lookup, key hashing, and wrapper allocation.
Sourcepub async fn embedder(&self, alias: &str) -> Result<Arc<dyn EmbeddingModel>>
pub async fn embedder(&self, alias: &str) -> Result<Arc<dyn EmbeddingModel>>
Resolve a dense text EmbeddingModel by alias.
Agent-noun alias for embedding, matching the
image_embedder / sparse_embedder / multi_vector_embedder naming.
Sourcepub async fn reranker(&self, alias: &str) -> Result<Arc<dyn RerankerModel>>
pub async fn reranker(&self, alias: &str) -> Result<Arc<dyn RerankerModel>>
Resolve, load (if necessary), and return an instrumented RerankerModel
handle for the given alias.
The returned handle is cached per alias so that repeated calls skip spec lookup, key hashing, and wrapper allocation.
Sourcepub async fn generator(&self, alias: &str) -> Result<Arc<dyn GeneratorModel>>
pub async fn generator(&self, alias: &str) -> Result<Arc<dyn GeneratorModel>>
Resolve, load (if necessary), and return an instrumented GeneratorModel
handle for the given alias.
The returned handle is cached per alias so that repeated calls skip spec lookup, key hashing, and wrapper allocation.
Sourcepub async fn raw_tensor_model(
&self,
alias: &str,
) -> Result<Arc<dyn RawTensorModel>>
pub async fn raw_tensor_model( &self, alias: &str, ) -> Result<Arc<dyn RawTensorModel>>
Resolve, load (if necessary), and return an instrumented RawTensorModel
handle for the given alias.
The returned handle is cached per alias so that repeated calls skip spec lookup, key hashing, and wrapper allocation.
Sourcepub async fn image_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn ImageEmbeddingModel>>
pub async fn image_embedder( &self, alias: &str, ) -> Result<Arc<dyn ImageEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
ImageEmbeddingModel handle for the given alias.
§Examples
let embedder = runtime.image_embedder("embed/siglip").await?;
let image = ImageInput::Bytes {
data: std::fs::read("photo.png").unwrap(),
media_type: "image/png".to_string(),
};
let result = embedder.embed(vec![image]).await?;
println!("dimension: {}", result.vectors[0].len());§Errors
Returns an error if the alias is unknown, the model fails to load, or the provider does not implement image embedding.
Sourcepub async fn audio_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn AudioEmbeddingModel>>
pub async fn audio_embedder( &self, alias: &str, ) -> Result<Arc<dyn AudioEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
AudioEmbeddingModel handle for the given alias.
Sourcepub async fn multimodal_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn MultimodalEmbeddingModel>>
pub async fn multimodal_embedder( &self, alias: &str, ) -> Result<Arc<dyn MultimodalEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
MultimodalEmbeddingModel handle for the given alias.
Sourcepub async fn sparse_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn SparseEmbeddingModel>>
pub async fn sparse_embedder( &self, alias: &str, ) -> Result<Arc<dyn SparseEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
SparseEmbeddingModel handle for the given alias.
§Errors
Returns an error if the alias is unknown, the model fails to load, or the provider does not implement sparse embedding.
Sourcepub async fn multi_vector_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn MultiVectorEmbeddingModel>>
pub async fn multi_vector_embedder( &self, alias: &str, ) -> Result<Arc<dyn MultiVectorEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
MultiVectorEmbeddingModel handle for the given alias.
§Errors
Returns an error if the alias is unknown, the model fails to load, or the provider does not implement multi-vector embedding.
Sourcepub async fn hybrid_embedder(
&self,
alias: &str,
) -> Result<Arc<dyn HybridEmbeddingModel>>
pub async fn hybrid_embedder( &self, alias: &str, ) -> Result<Arc<dyn HybridEmbeddingModel>>
Resolve, load (if necessary), and return an instrumented
HybridEmbeddingModel handle for the given alias.
The handle serves dense, sparse, and multi-vector heads from a single
forward pass; select which to materialize with a
HeadSet. Only multi-output graphs with a
hybrid preset (e.g. BGEM3Hybrid) resolve here — single-head models use
the per-task resolvers.
§Errors
Returns an error if the alias is unknown, the model has no hybrid preset, the model fails to load, or the loaded handle lacks the hybrid capability.
Sourcepub async fn nlp_model(&self, alias: &str) -> Result<Arc<dyn NlpModel>>
pub async fn nlp_model(&self, alias: &str) -> Result<Arc<dyn NlpModel>>
Resolve, load (if necessary), and return an instrumented NlpModel
handle for the given alias.
Sourcepub async fn document_extractor(
&self,
alias: &str,
) -> Result<Arc<dyn DocumentExtractionModel>>
pub async fn document_extractor( &self, alias: &str, ) -> Result<Arc<dyn DocumentExtractionModel>>
Resolve, load (if necessary), and return an instrumented
DocumentExtractionModel handle for the given alias.
§Examples
let extractor = runtime.document_extractor("docext/olmocr").await?;
let page = ImageInput::Bytes {
data: std::fs::read("page.png").unwrap(),
media_type: "image/png".to_string(),
};
let options = DocExtractOptions {
output: DocOutputFormat::Markdown,
include_tables: true,
include_formulas: true,
include_bboxes: false,
};
let pages = extractor.extract(vec![page], options).await?;
println!("{}", pages[0].plain_markdown);§Errors
Returns an error if the alias is unknown, the model fails to load, or the provider does not implement document extraction.
Sourcepub async fn transcriber(
&self,
alias: &str,
) -> Result<Arc<dyn TranscriptionModel>>
pub async fn transcriber( &self, alias: &str, ) -> Result<Arc<dyn TranscriptionModel>>
Resolve, load (if necessary), and return an instrumented
TranscriptionModel handle for the given alias.
Sourcepub async fn ocr_model(&self, alias: &str) -> Result<Arc<dyn OcrModel>>
pub async fn ocr_model(&self, alias: &str) -> Result<Arc<dyn OcrModel>>
Resolve, load (if necessary), and return an instrumented OcrModel
handle for the given alias.
§Examples
let ocr = runtime.ocr_model("ocr/ppocr-en").await?;
let image = ImageInput::Bytes {
data: std::fs::read("scan.png").unwrap(),
media_type: "image/png".to_string(),
};
let results = ocr.recognize(vec![image]).await?;
println!("{}", results[0].plain_text);§Errors
Returns an error if the alias is unknown, the model fails to load, or the provider does not implement OCR.
Auto Trait Implementations§
impl !Freeze for ModelRuntime
impl !RefUnwindSafe for ModelRuntime
impl Send for ModelRuntime
impl Sync for ModelRuntime
impl Unpin for ModelRuntime
impl UnsafeUnpin for ModelRuntime
impl !UnwindSafe for ModelRuntime
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Downcast for Twhere
T: AsAny + ?Sized,
impl<T> Downcast for Twhere
T: AsAny + ?Sized,
§fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
fn downcast_ref<T>(&self) -> Option<&T>where
T: AsAny,
Any.§fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
fn downcast_mut<T>(&mut self) -> Option<&mut T>where
T: AsAny,
Any.§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
impl<F, T> IntoSample<T> for Fwhere
T: FromSample<F>,
fn into_sample(self) -> T
§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.