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
impl FimEngine
Sourcepub fn load(
cache_dir: &Path,
choice: ModelChoice,
progress: &(dyn Fn(DownloadProgress) + Sync),
) -> Result<Self, String>
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}Sourcepub fn complete(
&mut self,
prefix: &str,
suffix: &str,
max_tokens: usize,
) -> Result<String, String>
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§
impl !Freeze for FimEngine
impl !RefUnwindSafe for FimEngine
impl !UnwindSafe for FimEngine
impl Send for FimEngine
impl Sync for FimEngine
impl Unpin for FimEngine
impl UnsafeUnpin for FimEngine
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
Mutably borrows from an owned value. Read more
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§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> ⓘ
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 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> ⓘ
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