Skip to main content

FimEngine

Struct FimEngine 

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

A loaded local FIM completion engine. Holds the model in memory; keep one alive and call FimEngine::complete repeatedly.

Implementations§

Source§

impl FimEngine

Source

pub fn load( cache_dir: &Path, choice: ModelChoice, progress: &(dyn Fn(DownloadProgress) + Sync), ) -> Result<Self, String>

Download (if needed) + load the choice model. cache_dir is where the GGUF weights + tokenizer are cached (see default_cache_dir). progress fires periodically while files download.

Blocking and slow on the first call (a ~1 GB download); fast afterwards (just the load). Run it on a worker thread.

Examples found in repository?
examples/smoke.rs (lines 21-27)
11fn main() {
12    let cache = fim_engine::default_cache_dir();
13    let choice = fim_engine::ModelChoice::Qwen1_5B;
14    eprintln!("cache dir: {}", cache.display());
15    eprintln!(
16        "model cached: {}",
17        fim_engine::is_model_cached(&cache, choice)
18    );
19
20    let t0 = Instant::now();
21    let mut engine = match fim_engine::FimEngine::load(&cache, choice, &|p| {
22        let pct = p
23            .total
24            .map(|t| format!("{}%", p.received * 100 / t.max(1)))
25            .unwrap_or_else(|| format!("{} bytes", p.received));
26        eprintln!("  download {} … {pct}", p.label);
27    }) {
28        Ok(e) => e,
29        Err(e) => {
30            eprintln!("LOAD FAILED: {e}");
31            std::process::exit(1);
32        }
33    };
34    eprintln!("loaded in {:.1}s", t0.elapsed().as_secs_f64());
35
36    // A classic FIM hole — the body of an `add` function.
37    let prefix = "fn add(a: i32, b: i32) -> i32 {\n    ";
38    let suffix = "\n}\n";
39    let t1 = Instant::now();
40    match engine.complete(prefix, suffix, 32) {
41        Ok(completion) => {
42            eprintln!("completion in {} ms", t1.elapsed().as_millis());
43            eprintln!("--- prefix ---\n{prefix}");
44            eprintln!("--- COMPLETION ---\n{completion}");
45            eprintln!("--- suffix ---\n{suffix}");
46        }
47        Err(e) => {
48            eprintln!("COMPLETE FAILED: {e}");
49            std::process::exit(1);
50        }
51    }
52}
Source

pub fn complete( &mut self, prefix: &str, suffix: &str, max_tokens: usize, ) -> Result<String, String>

Generate a completion for the cursor sitting between prefix (code before) and suffix (code after). Returns the text to insert — never includes the surrounding code. max_tokens bounds the length (≈ 64 is a good inline default).

Blocking + CPU-bound (~100–400 ms for the 1.5B model). Call on a worker thread; never on the UI thread.

Examples found in repository?
examples/smoke.rs (line 40)
11fn main() {
12    let cache = fim_engine::default_cache_dir();
13    let choice = fim_engine::ModelChoice::Qwen1_5B;
14    eprintln!("cache dir: {}", cache.display());
15    eprintln!(
16        "model cached: {}",
17        fim_engine::is_model_cached(&cache, choice)
18    );
19
20    let t0 = Instant::now();
21    let mut engine = match fim_engine::FimEngine::load(&cache, choice, &|p| {
22        let pct = p
23            .total
24            .map(|t| format!("{}%", p.received * 100 / t.max(1)))
25            .unwrap_or_else(|| format!("{} bytes", p.received));
26        eprintln!("  download {} … {pct}", p.label);
27    }) {
28        Ok(e) => e,
29        Err(e) => {
30            eprintln!("LOAD FAILED: {e}");
31            std::process::exit(1);
32        }
33    };
34    eprintln!("loaded in {:.1}s", t0.elapsed().as_secs_f64());
35
36    // A classic FIM hole — the body of an `add` function.
37    let prefix = "fn add(a: i32, b: i32) -> i32 {\n    ";
38    let suffix = "\n}\n";
39    let t1 = Instant::now();
40    match engine.complete(prefix, suffix, 32) {
41        Ok(completion) => {
42            eprintln!("completion in {} ms", t1.elapsed().as_millis());
43            eprintln!("--- prefix ---\n{prefix}");
44            eprintln!("--- COMPLETION ---\n{completion}");
45            eprintln!("--- suffix ---\n{suffix}");
46        }
47        Err(e) => {
48            eprintln!("COMPLETE FAILED: {e}");
49            std::process::exit(1);
50        }
51    }
52}

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

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

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

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

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

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

Initializes a with the given initializer. Read more
Source§

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

Dereferences the given pointer. Read more
Source§

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

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + 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
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + 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, 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.
Source§

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

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

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

fn with_current_subscriber(self) -> WithDispatch<Self>

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