Skip to main content

ModelRuntime

Struct ModelRuntime 

Source
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

Source

pub fn builder() -> ModelRuntimeBuilder

Create a new ModelRuntimeBuilder for configuring and constructing a runtime.

Source

pub async fn register(&self, spec: ModelAliasSpec) -> Result<()>

Register a new model alias at runtime.

Source

pub async fn contains_alias(&self, alias: &str) -> bool

Check if an alias exists in the catalog.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> AsAny for T
where T: Any,

§

fn as_any(&self) -> &(dyn Any + 'static)

§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

§

fn type_name(&self) -> &'static str

Gets the type name of self
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: AsAny + ?Sized,

§

fn is<T>(&self) -> bool
where T: AsAny,

Returns true if the boxed type is the same as T. Read more
§

fn downcast_ref<T>(&self) -> Option<&T>
where T: AsAny,

Forward to the method defined on the type Any.
§

fn downcast_mut<T>(&mut self) -> Option<&mut T>
where T: AsAny,

Forward to the method defined on the type Any.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 F
where T: FromSample<F>,

§

fn into_sample(self) -> T

§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,