shodh_memory/query_parsing/
mod.rs1mod llm_parser;
23mod parser_trait;
24mod rule_based;
25
26pub use llm_parser::{ApiType, LlmParser};
27pub use parser_trait::*;
28pub use rule_based::RuleBasedParser;
29
30use std::sync::Arc;
31
32#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
34pub enum ParserType {
35 #[default]
37 RuleBased,
38 Llm,
40}
41
42#[derive(Debug, Clone)]
44pub struct ParserConfig {
45 pub parser_type: ParserType,
47 pub llm_model_path: Option<String>,
49 pub llm_threads: usize,
51 pub llm_context_size: usize,
53}
54
55impl Default for ParserConfig {
56 fn default() -> Self {
57 Self {
58 parser_type: ParserType::RuleBased,
59 llm_model_path: None,
60 llm_threads: 4,
61 llm_context_size: 2048,
62 }
63 }
64}
65
66impl ParserConfig {
67 pub fn rule_based() -> Self {
69 Self::default()
70 }
71
72 pub fn llm(model_path: impl Into<String>) -> Self {
74 Self {
75 parser_type: ParserType::Llm,
76 llm_model_path: Some(model_path.into()),
77 ..Default::default()
78 }
79 }
80}
81
82pub fn create_parser(config: ParserConfig) -> Arc<dyn QueryParser> {
84 match config.parser_type {
85 ParserType::RuleBased => Arc::new(RuleBasedParser::new()),
86 #[cfg(feature = "llm-parser")]
87 ParserType::Llm => {
88 let model_path = config
89 .llm_model_path
90 .expect("LLM model path required for LLM parser");
91 Arc::new(
92 LlmParser::new(&model_path, config.llm_threads, config.llm_context_size)
93 .expect("Failed to load LLM model"),
94 )
95 }
96 #[cfg(not(feature = "llm-parser"))]
97 ParserType::Llm => {
98 tracing::warn!("LLM parser requested but 'llm-parser' feature not enabled, falling back to rule-based");
99 Arc::new(RuleBasedParser::new())
100 }
101 }
102}