Skip to main content

Inference

Struct Inference 

Source
pub struct Inference { /* private fields */ }
Expand description

An embedded inference runtime handle, with no database. Models load on first use and stay cached until the handle closes.

Inference is Send + Sync and safe for concurrent use from any thread, exactly like crate::Database: libantfly’s inference handles have the same “close waits for in-flight calls, a closed or foreign handle is rejected” contract. See zig/CAPI.md’s “Inference In Process” section for what running on a GPU/driver backend means for call_timeout/close/process-fault behavior: once a call reaches the device or driver it runs to completion, and a driver fault terminates the process.

Implementations§

Source§

impl Inference

Source

pub fn open(options: &InferenceOptions) -> Result<Inference>

Opens an embedded inference runtime using explicit options.

Source

pub fn open_default() -> Result<Inference>

Opens an embedded inference runtime with every default: models directory from $ANTFLY_INFERENCE_MODELS_DIR/ ~/.antfly/inference/models, automatic resource budgets, no per-call timeout. Equivalent to antfly_inference_open(NULL, ...).

Source

pub fn close(&self) -> Result<()>

Releases the embedded inference handle, waiting for in-flight calls on other threads to finish first – including any in-progress Inference::pull or Inference::generate_stream, neither of which close itself cancels (their own progress/chunk callbacks can, by returning false). Idempotent and safe to call concurrently with itself or from several threads at once; calls made after (or racing) close fail. Takes &self, not self, for the same reason as crate::Database::close. Drop also closes, for callers who never need to close early.

Source

pub fn embed(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /embed.

Source

pub fn rerank(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /rerank.

Source

pub fn chunk(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /chunk.

Source

pub fn generate(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /generate. A request with "stream": true fails with Error::InvalidArgument: responses from this method are always complete. Use Inference::generate_stream to stream.

Source

pub fn generate_stream( &self, request: impl AsRef<[u8]>, on_chunk: &mut dyn FnMut(&[u8]) -> bool, ) -> InferenceResult<()>

Streams a generate request (the same body as Inference::generate; "stream" is set for the caller). on_chunk is called on the calling thread for each chunk – the JSON of a chat.completion.chunk, valid only for the duration of the call – as the model produces tokens; generation waits for each call to return before producing the next chunk.

Returning false from on_chunk stops generation and this method returns Error::Cancelled (wrapped in InferenceError); cancellation is always honored at the chunk where on_chunk returns false. A request rejected before generation starts (for example a missing model) fails like Inference::generate, with the JSON error body attached; a failure mid-stream fails with Error::Internal and a JSON body naming "STREAM_FAILED".

A panic inside on_chunk is caught and does not unwind across the C ABI boundary (unwinding through extern "C" is undefined behavior): it cancels the stream (as if on_chunk had returned false) and is resumed once the underlying call returns.

Source

pub fn generate_batch( &self, request: impl AsRef<[u8]>, ) -> InferenceResult<Vec<u8>>

POST /generate/batch: up to 128 non-streaming generate requests in one call; per-item failures are reported in the response body, not as an Err.

Source

pub fn rewrite(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /rewrite.

Source

pub fn decide(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

Answers named choice, ordinal score, and Boolean questions. POST /decisions; accepts DecideRequest and returns DecideResponse JSON from specs/openapi/inference/api.yaml. Models must support typed decisions.

Source

pub fn extract(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /extract.

Source

pub fn read(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>

POST /read for images, documents, or audio.

Source

pub fn list_models(&self) -> InferenceResult<Vec<u8>>

GET /models: the installed models.

Source

pub fn pull( &self, request: impl AsRef<[u8]>, progress: Option<&mut dyn FnMut(&PullProgress) -> bool>, ) -> InferenceResult<Vec<u8>>

Downloads a model from the Hugging Face Hub into the handle’s models directory, like antfly inference pull. request is documented on antfly_inference_pull_json in zig/pkg/antfly-embedded/include/antfly.h ({"model": "owner/name[:variant]", ...}).

progress, if given, is called synchronously on the calling thread as each file starts, every 16 MiB, and as it completes; the download waits for each call to return before continuing. Returning false cancels the pull: this method then fails with Error::Cancelled (wrapped in InferenceError) and the model is not installed. Cancellation takes effect at the report where progress returns false – it is always honored, even at the very last report – not before. Completed files stay staged, so pulling the same model again resumes rather than restarts. A panic inside progress is caught and does not unwind across the C ABI boundary (unwinding through extern "C" is undefined behavior): it cancels the pull (as if progress had returned false) and is resumed once the underlying antfly_inference_pull_json call returns.

Inference::close waits for a pull in progress. On success the response is {"models": [...], "models_dir": "..."}; a model missing from the hub fails with Error::NotFound, a bad request or a model over the configured size limits with Error::InvalidArgument, and a network or hub failure with Error::Busy.

Trait Implementations§

Source§

impl Debug for Inference

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Drop for Inference

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Inference

Source§

impl Sync for Inference

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

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.