pub struct GenerationEngine { /* private fields */ }Expand description
Text generation engine using GGUF models.
Implementations§
Source§impl GenerationEngine
impl GenerationEngine
Sourcepub fn new(model_path: &Path) -> Result<Self>
pub fn new(model_path: &Path) -> Result<Self>
Create a new generation engine with the specified model.
Sourcepub fn load_default() -> Result<Self>
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}Sourcepub fn is_available() -> bool
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
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}Sourcepub fn generate(
&self,
prompt: &str,
max_tokens: usize,
) -> Result<GenerationResult>
pub fn generate( &self, prompt: &str, max_tokens: usize, ) -> Result<GenerationResult>
Generate text from a prompt using simple token-by-token generation.
Sourcepub fn expand_query(
&self,
query: &str,
include_lexical: bool,
) -> Result<Vec<Queryable>>
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§
Auto Trait Implementations§
impl Freeze for GenerationEngine
impl RefUnwindSafe for GenerationEngine
impl Send for GenerationEngine
impl Sync for GenerationEngine
impl Unpin for GenerationEngine
impl UnwindSafe for GenerationEngine
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