Skip to main content

GenerationEngine

Struct GenerationEngine 

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

Text generation engine using GGUF models.

Implementations§

Source§

impl GenerationEngine

Source

pub fn new(model_path: &Path) -> Result<Self>

Create a new generation engine with the specified model.

Source

pub fn load_default() -> Result<Self>

Load the default generation model.

Examples found in repository?
examples/query_expansion.rs (line 52)
13fn main() -> Result<()> {
14    let db_path = std::env::temp_dir().join("qmd_expansion.db");
15    let _ = std::fs::remove_file(&db_path);
16    let store = Store::open(&db_path)?;
17
18    let now = chrono::Utc::now().to_rfc3339();
19    for (name, content) in SAMPLE_DOCS {
20        let hash = Store::hash_content(content);
21        let title = Store::extract_title(content);
22        store.insert_content(&hash, content, &now)?;
23        store.insert_document("samples", name, &title, &hash, &now, &now)?;
24    }
25
26    let query = "rust error handling";
27    println!("Query: '{}'\n", query);
28
29    // Simple expansion
30    println!("Simple expansion:");
31    for q in expand_query_simple(query) {
32        let t = match q.query_type {
33            QueryType::Lex => "LEX",
34            QueryType::Vec => "VEC",
35            QueryType::Hyde => "HYD",
36        };
37        println!("  [{}] {}", t, q.text);
38    }
39
40    // Search with expanded queries
41    println!("\nSearch results:");
42    for q in expand_query_simple(query) {
43        if q.query_type == QueryType::Lex {
44            let n = store.search_fts(&q.text, 5, None)?.len();
45            println!("  '{}': {} results", q.text, n);
46        }
47    }
48
49    // LLM expansion
50    println!("\nLLM expansion:");
51    if GenerationEngine::is_available() {
52        let engine = GenerationEngine::load_default()?;
53        for q in engine.expand_query(query, true)? {
54            println!("  [{:?}] {}", q.query_type, q.text);
55        }
56    } else {
57        println!("  (not available)");
58    }
59
60    // Manual construction
61    println!("\nManual:");
62    for q in [
63        Queryable::lex("rust error"),
64        Queryable::vec("exception handling"),
65    ] {
66        println!("  [{:?}] {}", q.query_type, q.text);
67    }
68
69    let _ = std::fs::remove_file(&db_path);
70    Ok(())
71}
Source

pub fn is_available() -> bool

Check if generation model exists.

Examples found in repository?
examples/models.rs (line 33)
13fn main() -> Result<()> {
14    // Cache directory
15    println!("Cache: {}\n", get_model_cache_dir().display());
16
17    // Cached models
18    println!("Cached models:");
19    let models = list_cached_models();
20    if models.is_empty() {
21        println!("  (none - will download)");
22    } else {
23        for m in &models {
24            println!("  {}", m);
25        }
26    }
27
28    // Model availability check
29    println!("\nAvailability:");
30    let check = |name: &str, ok: bool| println!("  {}: {}", name, if ok { "yes" } else { "no" });
31    check("embed model", model_exists(DEFAULT_EMBED_MODEL));
32    check("EmbeddingEngine", EmbeddingEngine::load_default().is_ok());
33    check("GenerationEngine", GenerationEngine::is_available());
34    check("RerankEngine", RerankEngine::is_available());
35
36    // Download model (demo)
37    println!("\nDownloading embedding model...");
38    let result = pull_model(DEFAULT_EMBED_MODEL_URI, false)?;
39    println!("  Path: {}", result.path.display());
40    println!("  Size: {} bytes", result.size_bytes);
41    println!("  Refreshed: {}", result.refreshed);
42
43    // URI resolution
44    println!("\nURI resolution:");
45    let uri = "hf:ggml-org/embeddinggemma-300M-GGUF/embeddinggemma-300M-Q8_0.gguf";
46    if let Ok(path) = resolve_model(uri) {
47        println!("  {} ->\n  {}", uri, path.display());
48    }
49
50    // Load and test
51    let mut engine = EmbeddingEngine::new(&result.path)?;
52    let emb = engine.embed("test embedding")?;
53    println!("\nModel info:");
54    println!("  Name: {}", emb.model);
55    println!("  Dimensions: {}", emb.embedding.len());
56
57    Ok(())
58}
More examples
Hide additional examples
examples/query_expansion.rs (line 51)
13fn main() -> Result<()> {
14    let db_path = std::env::temp_dir().join("qmd_expansion.db");
15    let _ = std::fs::remove_file(&db_path);
16    let store = Store::open(&db_path)?;
17
18    let now = chrono::Utc::now().to_rfc3339();
19    for (name, content) in SAMPLE_DOCS {
20        let hash = Store::hash_content(content);
21        let title = Store::extract_title(content);
22        store.insert_content(&hash, content, &now)?;
23        store.insert_document("samples", name, &title, &hash, &now, &now)?;
24    }
25
26    let query = "rust error handling";
27    println!("Query: '{}'\n", query);
28
29    // Simple expansion
30    println!("Simple expansion:");
31    for q in expand_query_simple(query) {
32        let t = match q.query_type {
33            QueryType::Lex => "LEX",
34            QueryType::Vec => "VEC",
35            QueryType::Hyde => "HYD",
36        };
37        println!("  [{}] {}", t, q.text);
38    }
39
40    // Search with expanded queries
41    println!("\nSearch results:");
42    for q in expand_query_simple(query) {
43        if q.query_type == QueryType::Lex {
44            let n = store.search_fts(&q.text, 5, None)?.len();
45            println!("  '{}': {} results", q.text, n);
46        }
47    }
48
49    // LLM expansion
50    println!("\nLLM expansion:");
51    if GenerationEngine::is_available() {
52        let engine = GenerationEngine::load_default()?;
53        for q in engine.expand_query(query, true)? {
54            println!("  [{:?}] {}", q.query_type, q.text);
55        }
56    } else {
57        println!("  (not available)");
58    }
59
60    // Manual construction
61    println!("\nManual:");
62    for q in [
63        Queryable::lex("rust error"),
64        Queryable::vec("exception handling"),
65    ] {
66        println!("  [{:?}] {}", q.query_type, q.text);
67    }
68
69    let _ = std::fs::remove_file(&db_path);
70    Ok(())
71}
Source

pub fn generate( &self, prompt: &str, max_tokens: usize, ) -> Result<GenerationResult>

Generate text from a prompt using simple token-by-token generation.

Source

pub fn expand_query( &self, query: &str, include_lexical: bool, ) -> Result<Vec<Queryable>>

Expand a query into multiple search variations.

Examples found in repository?
examples/query_expansion.rs (line 53)
13fn main() -> Result<()> {
14    let db_path = std::env::temp_dir().join("qmd_expansion.db");
15    let _ = std::fs::remove_file(&db_path);
16    let store = Store::open(&db_path)?;
17
18    let now = chrono::Utc::now().to_rfc3339();
19    for (name, content) in SAMPLE_DOCS {
20        let hash = Store::hash_content(content);
21        let title = Store::extract_title(content);
22        store.insert_content(&hash, content, &now)?;
23        store.insert_document("samples", name, &title, &hash, &now, &now)?;
24    }
25
26    let query = "rust error handling";
27    println!("Query: '{}'\n", query);
28
29    // Simple expansion
30    println!("Simple expansion:");
31    for q in expand_query_simple(query) {
32        let t = match q.query_type {
33            QueryType::Lex => "LEX",
34            QueryType::Vec => "VEC",
35            QueryType::Hyde => "HYD",
36        };
37        println!("  [{}] {}", t, q.text);
38    }
39
40    // Search with expanded queries
41    println!("\nSearch results:");
42    for q in expand_query_simple(query) {
43        if q.query_type == QueryType::Lex {
44            let n = store.search_fts(&q.text, 5, None)?.len();
45            println!("  '{}': {} results", q.text, n);
46        }
47    }
48
49    // LLM expansion
50    println!("\nLLM expansion:");
51    if GenerationEngine::is_available() {
52        let engine = GenerationEngine::load_default()?;
53        for q in engine.expand_query(query, true)? {
54            println!("  [{:?}] {}", q.query_type, q.text);
55        }
56    } else {
57        println!("  (not available)");
58    }
59
60    // Manual construction
61    println!("\nManual:");
62    for q in [
63        Queryable::lex("rust error"),
64        Queryable::vec("exception handling"),
65    ] {
66        println!("  [{:?}] {}", q.query_type, q.text);
67    }
68
69    let _ = std::fs::remove_file(&db_path);
70    Ok(())
71}

Trait Implementations§

Source§

impl Debug for GenerationEngine

Source§

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

Formats the value using the given formatter. Read more

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> 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> PolicyExt for T
where T: ?Sized,

Source§

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

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