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
impl Inference
Sourcepub fn open(options: &InferenceOptions) -> Result<Inference>
pub fn open(options: &InferenceOptions) -> Result<Inference>
Opens an embedded inference runtime using explicit options.
Sourcepub fn open_default() -> Result<Inference>
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, ...).
Sourcepub fn close(&self) -> Result<()>
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.
Sourcepub fn generate(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>
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.
Sourcepub fn generate_stream(
&self,
request: impl AsRef<[u8]>,
on_chunk: &mut dyn FnMut(&[u8]) -> bool,
) -> InferenceResult<()>
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.
Sourcepub fn generate_batch(
&self,
request: impl AsRef<[u8]>,
) -> InferenceResult<Vec<u8>>
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.
Sourcepub fn decide(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>
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.
Sourcepub fn read(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>
pub fn read(&self, request: impl AsRef<[u8]>) -> InferenceResult<Vec<u8>>
POST /read for images, documents, or audio.
Sourcepub fn list_models(&self) -> InferenceResult<Vec<u8>>
pub fn list_models(&self) -> InferenceResult<Vec<u8>>
GET /models: the installed models.
Sourcepub fn pull(
&self,
request: impl AsRef<[u8]>,
progress: Option<&mut dyn FnMut(&PullProgress) -> bool>,
) -> InferenceResult<Vec<u8>>
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.