Spaces:
Build error
Build error
Download src/config/mod.rs from LuckyMod/intentforge: direct link, hf CLI and curl.
- Browser
- Download file 24.7 kB
-
https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/config/mod.rs
- Command line
-
hf download hf://spaces/LuckyMod/intentforge/src/config/mod.rs
-
curl -L -o mod.rs https://huggingface.co/spaces/LuckyMod/intentforge/resolve/main/src/config/mod.rs
24.7 kB
| //! Configuration module for IntentForge v2 | |
| //! | |
| //! Handles loading and managing application configuration from YAML files | |
| //! and environment variables. | |
| use anyhow::{Context, Result}; | |
| use serde::{Deserialize, Serialize}; | |
| use std::path::Path; | |
| /// Main application configuration | |
| pub struct Config { | |
| pub meilisearch: MeilisearchConfig, | |
| pub qdrant: QdrantConfig, | |
| pub redis: RedisConfig, | |
| pub crawler: CrawlerConfig, | |
| pub indexer: IndexerConfig, | |
| pub trending: TrendingConfig, | |
| pub discovery: DiscoveryConfig, | |
| pub logging: LoggingConfig, | |
| pub extraction: ExtractionConfig, | |
| pub inference: InferenceConfig, | |
| pub anti_detection: AntiDetectionConfig, | |
| pub self_improvement: SelfImprovementConfig, | |
| pub whoogle_url: String, | |
| pub librex_url: String, | |
| pub searxng_url: String, | |
| pub query_layer_url: String, | |
| pub video_discovery: VideoDiscoveryConfig, | |
| pub image_search: ImageSearchConfig, | |
| pub api_key: Option<String>, | |
| } | |
| /// Meilisearch configuration | |
| pub struct MeilisearchConfig { | |
| pub enabled: bool, | |
| pub url: String, | |
| pub master_key: String, | |
| pub index_name: String, | |
| pub binary_quantization: bool, | |
| pub semantic_ratio: f32, | |
| pub task_timeout_secs: u64, | |
| pub redis_cache_enabled: bool, | |
| pub redis_cache_ttl_secs: u64, | |
| } | |
| /// Qdrant configuration | |
| pub struct QdrantConfig { | |
| pub url: String, | |
| pub api_key: Option<String>, | |
| pub collection_name: String, | |
| } | |
| fn default_qdrant_collection() -> String { | |
| "intentforge".to_string() | |
| } | |
| fn default_search_cache_ttl() -> u64 { | |
| 3600 // 1 hour | |
| } | |
| /// Redis configuration | |
| pub struct RedisConfig { | |
| pub url: String, | |
| pub bloom_fp_rate: f64, | |
| pub simhash_threshold: u8, | |
| } | |
| /// Crawler configuration | |
| pub struct CrawlerConfig { | |
| pub rate_limit: u32, | |
| pub max_concurrent: usize, | |
| pub timeout_secs: u64, | |
| pub user_agent: String, | |
| pub respect_robots: bool, | |
| pub static_first: bool, | |
| pub max_retries: u32, | |
| pub robots_cache_ttl_secs: u64, | |
| pub js_whitelist: Vec<String>, | |
| } | |
| /// Indexer configuration | |
| pub struct IndexerConfig { | |
| pub inference_batch_size: usize, | |
| pub min_content_length: usize, | |
| pub snippet_length: usize, | |
| } | |
| /// Trending monitor configuration | |
| pub struct TrendingConfig { | |
| pub refresh_interval: u64, | |
| pub top_n: usize, | |
| } | |
| /// Discovery service configuration | |
| pub struct DiscoveryConfig { | |
| pub interval_secs: u64, | |
| pub max_queue_size: usize, | |
| pub follow_links: bool, | |
| pub link_threshold: f32, | |
| pub common_crawl_delta: Option<CommonCrawlDeltaConfig>, | |
| } | |
| /// Common Crawl delta automation configuration | |
| pub struct CommonCrawlDeltaConfig { | |
| pub enabled: bool, | |
| pub interval_secs: u64, | |
| pub crawl_id: String, | |
| pub domains: Vec<String>, | |
| pub max_entries_per_domain: usize, | |
| pub data_dir: String, | |
| pub quality_threshold: f32, | |
| } | |
| /// Logging configuration | |
| pub struct LoggingConfig { | |
| pub level: String, | |
| pub format: String, | |
| } | |
| /// Trafilatura extraction configuration | |
| pub struct ExtractionConfig { | |
| pub trafilatura_url: String, | |
| pub batch_size: usize, | |
| pub timeout_secs: u64, | |
| } | |
| /// ONNX inference configuration | |
| pub struct InferenceConfig { | |
| pub model_path: String, | |
| pub inference_batch_size: usize, | |
| pub embedding_dim: usize, | |
| pub max_seq_length: usize, | |
| /// Threshold for the fast keyword-based intent scorer (0.0 - 1.0) | |
| pub fast_intent_threshold: f32, | |
| } | |
| /// Anti-detection configuration | |
| pub struct AntiDetectionConfig { | |
| pub enabled: bool, | |
| pub rotation_interval: u32, | |
| pub use_client_hints: bool, | |
| pub invidious_instances: Vec<String>, | |
| pub stackoverflow_api_key: Option<String>, | |
| pub pubmed_api_key: Option<String>, | |
| pub semantic_scholar_api_key: Option<String>, | |
| } | |
| /// Video discovery configuration | |
| pub struct VideoDiscoveryConfig { | |
| pub ytdlp_path: String, | |
| pub ytdlp_update_interval_hours: u64, | |
| pub piped_instances: Vec<String>, | |
| pub invidious_instances: Vec<String>, | |
| pub youtube_api_key: Option<String>, | |
| pub rapidapi_key: Option<String>, | |
| pub enabled_sources: Vec<String>, | |
| pub search_limit: usize, | |
| pub cache_ttl_secs: u64, | |
| } | |
| impl Default for VideoDiscoveryConfig { | |
| fn default() -> Self { | |
| Self { | |
| ytdlp_path: default_ytdlp_path(), | |
| ytdlp_update_interval_hours: default_ytdlp_update_interval(), | |
| piped_instances: vec![ | |
| "https://pipedapi.kavin.rocks".to_string(), | |
| "https://pipedapi.adminforge.de".to_string(), | |
| "https://api.piped.yt".to_string(), | |
| "https://pipedapi.leptonsh.top".to_string(), | |
| "https://piped.qiangdai.cn".to_string(), | |
| ], | |
| invidious_instances: vec![ | |
| "https://invidious.nerdvpn.de".to_string(), | |
| "https://inv.tux.pizza".to_string(), | |
| "https://yewtu.be".to_string(), | |
| ], | |
| youtube_api_key: std::env::var("YOUTUBE_API_KEY").ok(), | |
| rapidapi_key: std::env::var("RAPIDAPI_KEY").ok(), | |
| enabled_sources: default_video_sources(), | |
| search_limit: default_video_search_limit(), | |
| cache_ttl_secs: default_video_cache_ttl(), | |
| } | |
| } | |
| } | |
| /// External image search API configuration | |
| pub struct ImageSearchConfig { | |
| pub pixabay_api_key: Option<String>, | |
| pub pexels_api_key: Option<String>, | |
| pub search_limit: usize, | |
| pub cache_ttl_secs: u64, | |
| } | |
| impl Default for ImageSearchConfig { | |
| fn default() -> Self { | |
| Self { | |
| pixabay_api_key: std::env::var("PIXABAY_API_KEY").ok(), | |
| pexels_api_key: std::env::var("PEXELS_API_KEY").ok(), | |
| search_limit: default_image_search_limit(), | |
| cache_ttl_secs: default_image_cache_ttl(), | |
| } | |
| } | |
| } | |
| /// Self-improvement configuration | |
| pub struct SelfImprovementConfig { | |
| pub enabled: bool, | |
| pub min_gap_score: f32, | |
| pub max_results_per_query: usize, | |
| pub process_interval_secs: u64, | |
| /// Maximum number of gap queries to process concurrently | |
| pub max_parallel_gaps: usize, | |
| /// Minimum number of quality URLs to index before considering a gap filled | |
| pub min_urls_per_gap: usize, | |
| /// Minimum intent score (cosine similarity) for a URL to count as quality | |
| pub min_intent_score: f32, | |
| /// Minimum relevance score for a URL to count as quality | |
| pub min_relevance_score: f32, | |
| /// Maximum rounds of meta-search queries to try per gap | |
| pub max_search_rounds: usize, | |
| } | |
| // Default value functions | |
| fn default_binary_quantization() -> bool { | |
| true | |
| } | |
| fn default_semantic_ratio() -> f32 { | |
| 0.7 | |
| } | |
| fn default_task_timeout() -> u64 { | |
| 60 | |
| } | |
| fn default_bloom_fp_rate() -> f64 { | |
| 0.01 | |
| } | |
| fn default_simhash_threshold() -> u8 { | |
| 3 | |
| } | |
| fn default_rate_limit() -> u32 { | |
| 10 | |
| } | |
| fn default_max_concurrent() -> usize { | |
| 8 | |
| } | |
| fn default_timeout_secs() -> u64 { | |
| 30 | |
| } | |
| fn default_user_agent() -> String { | |
| "IntentForge/0.1 (Intent Discovery Engine)".to_string() | |
| } | |
| fn default_true() -> bool { | |
| true | |
| } | |
| fn default_batch_size() -> usize { | |
| 32 | |
| } | |
| fn default_min_content_length() -> usize { | |
| 100 | |
| } | |
| fn default_snippet_length() -> usize { | |
| 200 | |
| } | |
| fn default_refresh_interval() -> u64 { | |
| 300 | |
| } | |
| fn default_top_n() -> usize { | |
| 50 | |
| } | |
| fn default_discovery_interval() -> u64 { | |
| 3600 | |
| } | |
| fn default_max_queue_size() -> usize { | |
| 10000 | |
| } | |
| fn default_link_threshold() -> f32 { | |
| 0.8 | |
| } | |
| fn default_log_level() -> String { | |
| "info".to_string() | |
| } | |
| fn default_log_format() -> String { | |
| "json".to_string() | |
| } | |
| fn default_max_retries() -> u32 { | |
| 3 | |
| } | |
| fn default_robots_cache_ttl() -> u64 { | |
| 3600 | |
| } | |
| fn default_trafilatura_url() -> String { | |
| "http://localhost:8080".to_string() | |
| } | |
| fn default_extract_batch_size() -> usize { | |
| 16 | |
| } | |
| fn default_extract_timeout() -> u64 { | |
| 60 | |
| } | |
| fn default_model_path() -> String { | |
| "models/all-MiniLM-L6-v2.onnx".to_string() | |
| } | |
| fn default_inference_batch_size() -> usize { | |
| 32 | |
| } | |
| fn default_embedding_dim() -> usize { | |
| 384 | |
| } | |
| fn default_max_seq_length() -> usize { | |
| 128 | |
| } | |
| fn default_fast_intent_threshold() -> f32 { | |
| 0.4 | |
| } | |
| fn default_cc_delta_interval() -> u64 { | |
| 2592000 // 30 days | |
| } | |
| fn default_cc_crawl_id() -> String { | |
| "CC-MAIN-2025-08".to_string() // latest CC crawl as of 2026-03 | |
| } | |
| fn default_cc_max_entries() -> usize { | |
| 100 | |
| } | |
| fn default_cc_data_dir() -> String { | |
| "./cc_data".to_string() | |
| } | |
| fn default_cc_quality_threshold() -> f32 { | |
| 0.3 | |
| } | |
| fn default_rotation_interval() -> u32 { | |
| 10 | |
| } | |
| fn default_self_imp_enabled() -> bool { | |
| false | |
| } | |
| fn default_min_gap_score() -> f32 { | |
| 0.5 | |
| } | |
| fn default_max_results_per_query() -> usize { | |
| 10 | |
| } | |
| fn default_process_interval() -> u64 { | |
| 300 | |
| } | |
| fn default_max_parallel_gaps() -> usize { | |
| 3 | |
| } | |
| fn default_min_urls_per_gap() -> usize { | |
| 25 | |
| } | |
| fn default_min_intent_score() -> f32 { | |
| 0.3 | |
| } | |
| fn default_min_relevance_score() -> f32 { | |
| 0.3 | |
| } | |
| fn default_max_search_rounds() -> usize { | |
| 5 | |
| } | |
| fn default_whoogle_url() -> String { | |
| String::from("http://whoogle:5000") | |
| } | |
| fn default_librex_url() -> String { | |
| String::from("http://librex:80") | |
| } | |
| fn default_searxng_url() -> String { | |
| String::from("http://searxng:8080") | |
| } | |
| fn default_query_layer_url() -> String { | |
| String::from("http://localhost:8000") | |
| } | |
| fn default_ytdlp_path() -> String { | |
| String::from("yt-dlp") | |
| } | |
| fn default_ytdlp_update_interval() -> u64 { | |
| 12 | |
| } | |
| fn default_video_sources() -> Vec<String> { | |
| vec![ | |
| "youtube".to_string(), | |
| "rapid_youtube".to_string(), | |
| "piped".to_string(), | |
| "invidious".to_string(), | |
| "archive_org".to_string(), | |
| "dailymotion".to_string(), | |
| "vimeo".to_string(), | |
| "odysee".to_string(), | |
| "yt-dlp".to_string(), | |
| ] | |
| } | |
| fn default_video_search_limit() -> usize { | |
| 50 | |
| } | |
| fn default_video_cache_ttl() -> u64 { | |
| 1800 | |
| } | |
| fn default_image_search_limit() -> usize { | |
| 20 | |
| } | |
| fn default_image_cache_ttl() -> u64 { | |
| 86400 | |
| } | |
| impl Default for AntiDetectionConfig { | |
| fn default() -> Self { | |
| Self { | |
| enabled: true, | |
| rotation_interval: default_rotation_interval(), | |
| use_client_hints: true, | |
| invidious_instances: vec![ | |
| "https://invidious.nerdvpn.de".to_string(), | |
| "https://inv.tux.pizza".to_string(), | |
| "https://yewtu.be".to_string(), | |
| ], | |
| stackoverflow_api_key: None, | |
| pubmed_api_key: None, | |
| semantic_scholar_api_key: None, | |
| } | |
| } | |
| } | |
| impl Default for SelfImprovementConfig { | |
| fn default() -> Self { | |
| Self { | |
| enabled: default_self_imp_enabled(), | |
| min_gap_score: default_min_gap_score(), | |
| max_results_per_query: default_max_results_per_query(), | |
| process_interval_secs: default_process_interval(), | |
| max_parallel_gaps: default_max_parallel_gaps(), | |
| min_urls_per_gap: default_min_urls_per_gap(), | |
| min_intent_score: default_min_intent_score(), | |
| min_relevance_score: default_min_relevance_score(), | |
| max_search_rounds: default_max_search_rounds(), | |
| } | |
| } | |
| } | |
| impl Default for CommonCrawlDeltaConfig { | |
| fn default() -> Self { | |
| Self { | |
| enabled: true, | |
| interval_secs: default_cc_delta_interval(), | |
| crawl_id: default_cc_crawl_id(), | |
| domains: vec![ | |
| "github.com".to_string(), | |
| "arxiv.org".to_string(), | |
| "news.ycombinator.com".to_string(), | |
| "stackoverflow.com".to_string(), | |
| ], | |
| max_entries_per_domain: default_cc_max_entries(), | |
| data_dir: default_cc_data_dir(), | |
| quality_threshold: default_cc_quality_threshold(), | |
| } | |
| } | |
| } | |
| impl Config { | |
| /// Load configuration from a YAML file | |
| pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self> { | |
| let file = std::fs::File::open(path.as_ref()) | |
| .with_context(|| format!("Failed to open config file: {:?}", path.as_ref()))?; | |
| let content = | |
| std::io::read_to_string(file).with_context(|| "Failed to read config file")?; | |
| let config: Config = | |
| toml::from_str(&content).with_context(|| "Failed to parse config TOML")?; | |
| config.validate()?; | |
| Ok(config) | |
| } | |
| /// Load configuration from default locations | |
| pub fn load() -> Result<Self> { | |
| // Try loading from config.yaml in current directory | |
| let config_path = Path::new("config.yaml"); | |
| if config_path.exists() { | |
| let mut config = Self::from_file(config_path)?; | |
| config.apply_env_overrides(); | |
| return Ok(config); | |
| } | |
| // Try loading from ../config.yaml | |
| let config_path = Path::new("../config.yaml"); | |
| if config_path.exists() { | |
| let mut config = Self::from_file(config_path)?; | |
| config.apply_env_overrides(); | |
| return Ok(config); | |
| } | |
| // Return default configuration | |
| Self::default_config() | |
| } | |
| /// Apply environment variable overrides for Docker networking | |
| fn apply_env_overrides(&mut self) { | |
| if let Ok(url) = std::env::var("REDIS_URL") { | |
| self.redis.url = url; | |
| } | |
| if let Ok(url) = std::env::var("MEILI_URL") { | |
| self.meilisearch.url = url; | |
| } | |
| if let Ok(url) = std::env::var("MEILI_MASTER_KEY") { | |
| self.meilisearch.master_key = url; | |
| } | |
| if let Ok(url) = std::env::var("QDRANT_URL") { | |
| self.qdrant.url = url; | |
| } | |
| if let Ok(key) = std::env::var("QDRANT_API_KEY") { | |
| self.qdrant.api_key = Some(key); | |
| } | |
| if let Ok(url) = std::env::var("TRAFILATURA_URL") { | |
| self.extraction.trafilatura_url = url; | |
| } | |
| if let Ok(dir) = std::env::var("CC_DATA_DIR") { | |
| if let Some(ref mut cc) = self.discovery.common_crawl_delta { | |
| cc.data_dir = dir; | |
| } | |
| } | |
| if let Ok(url) = std::env::var("WHOOGLE_URL") { | |
| self.whoogle_url = url; | |
| } | |
| if let Ok(url) = std::env::var("LIBREX_URL") { | |
| self.librex_url = url; | |
| } | |
| if let Ok(url) = std::env::var("SEARXNG_URL") { | |
| self.searxng_url = url; | |
| } | |
| if let Ok(url) = std::env::var("QUERY_LAYER_URL") { | |
| self.query_layer_url = url; | |
| } | |
| if let Ok(key) = std::env::var("YOUTUBE_API_KEY") { | |
| self.video_discovery.youtube_api_key = Some(key); | |
| } | |
| if let Ok(key) = std::env::var("RAPIDAPI_KEY") { | |
| self.video_discovery.rapidapi_key = Some(key); | |
| } | |
| if let Ok(key) = std::env::var("PIXABAY_API_KEY") { | |
| self.image_search.pixabay_api_key = Some(key); | |
| } | |
| if let Ok(key) = std::env::var("PEXELS_API_KEY") { | |
| self.image_search.pexels_api_key = Some(key); | |
| } | |
| if let Ok(key) = std::env::var("INTENTFORGE_API_KEY") { | |
| self.api_key = Some(key); | |
| } | |
| } | |
| /// Create a default configuration | |
| pub fn default_config() -> Result<Self> { | |
| let config = Config { | |
| meilisearch: MeilisearchConfig { | |
| enabled: true, | |
| url: "http://localhost:7700".to_string(), | |
| master_key: "masterKey".to_string(), | |
| index_name: "intentforge".to_string(), | |
| binary_quantization: true, | |
| semantic_ratio: 0.7, | |
| task_timeout_secs: 60, | |
| redis_cache_enabled: true, | |
| redis_cache_ttl_secs: 3600, | |
| }, | |
| qdrant: QdrantConfig { | |
| url: "http://localhost:6334".to_string(), | |
| api_key: None, | |
| collection_name: "intentforge".to_string(), | |
| }, | |
| redis: RedisConfig { | |
| url: "redis://localhost:6379".to_string(), | |
| bloom_fp_rate: 0.01, | |
| simhash_threshold: 3, | |
| }, | |
| crawler: CrawlerConfig { | |
| rate_limit: 10, | |
| max_concurrent: 8, | |
| timeout_secs: 30, | |
| user_agent: "IntentForge/0.1 (Intent Discovery Engine)".to_string(), | |
| respect_robots: true, | |
| static_first: true, | |
| max_retries: 3, | |
| robots_cache_ttl_secs: 3600, | |
| js_whitelist: vec![ | |
| "producthunt.com".to_string(), | |
| "news.ycombinator.com".to_string(), | |
| ], | |
| }, | |
| indexer: IndexerConfig { | |
| inference_batch_size: 32, | |
| min_content_length: 100, | |
| snippet_length: 200, | |
| }, | |
| trending: TrendingConfig { | |
| refresh_interval: 300, | |
| top_n: 50, | |
| }, | |
| discovery: DiscoveryConfig { | |
| interval_secs: 3600, | |
| max_queue_size: 10000, | |
| follow_links: true, | |
| link_threshold: 0.8, | |
| common_crawl_delta: Some(CommonCrawlDeltaConfig::default()), | |
| }, | |
| logging: LoggingConfig { | |
| level: "info".to_string(), | |
| format: "json".to_string(), | |
| }, | |
| extraction: ExtractionConfig { | |
| trafilatura_url: "http://localhost:8080".to_string(), | |
| batch_size: 16, | |
| timeout_secs: 60, | |
| }, | |
| inference: InferenceConfig { | |
| model_path: "models/all-MiniLM-L6-v2.onnx".to_string(), | |
| inference_batch_size: 32, | |
| embedding_dim: 384, | |
| max_seq_length: 128, | |
| fast_intent_threshold: 0.4, | |
| }, | |
| anti_detection: AntiDetectionConfig::default(), | |
| self_improvement: SelfImprovementConfig::default(), | |
| whoogle_url: default_whoogle_url(), | |
| librex_url: default_librex_url(), | |
| searxng_url: default_searxng_url(), | |
| query_layer_url: default_query_layer_url(), | |
| video_discovery: VideoDiscoveryConfig::default(), | |
| image_search: ImageSearchConfig::default(), | |
| api_key: std::env::var("INTENTFORGE_API_KEY").ok(), | |
| }; | |
| config.validate()?; | |
| Ok(config) | |
| } | |
| /// Validate configuration | |
| pub fn validate(&self) -> Result<()> { | |
| if self.meilisearch.enabled { | |
| anyhow::ensure!( | |
| !self.meilisearch.url.is_empty(), | |
| "Meilisearch URL cannot be empty when enabled" | |
| ); | |
| anyhow::ensure!( | |
| !self.meilisearch.master_key.is_empty(), | |
| "Meilisearch master key cannot be empty when enabled" | |
| ); | |
| } | |
| anyhow::ensure!(!self.qdrant.url.is_empty(), "Qdrant URL cannot be empty"); | |
| anyhow::ensure!(!self.redis.url.is_empty(), "Redis URL cannot be empty"); | |
| anyhow::ensure!( | |
| self.crawler.rate_limit > 0, | |
| "Crawler rate limit must be positive" | |
| ); | |
| anyhow::ensure!( | |
| self.indexer.inference_batch_size > 0, | |
| "Inference batch size must be positive" | |
| ); | |
| anyhow::ensure!( | |
| self.inference.embedding_dim > 0, | |
| "Embedding dimension must be positive" | |
| ); | |
| Ok(()) | |
| } | |
| } | |
| mod tests { | |
| use super::*; | |
| fn test_default_config() { | |
| let config = Config::default_config().unwrap(); | |
| assert_eq!(config.meilisearch.index_name, "intentforge"); | |
| assert!(config.meilisearch.binary_quantization); | |
| assert_eq!(config.meilisearch.semantic_ratio, 0.7); | |
| } | |
| fn test_default_js_whitelist() { | |
| let config = Config::default_config().unwrap(); | |
| assert!(config | |
| .crawler | |
| .js_whitelist | |
| .contains(&"producthunt.com".to_string())); | |
| } | |
| fn test_extraction_defaults() { | |
| let config = Config::default_config().unwrap(); | |
| assert_eq!(config.extraction.trafilatura_url, "http://localhost:8080"); | |
| assert_eq!(config.extraction.batch_size, 16); | |
| } | |
| fn test_inference_defaults() { | |
| let config = Config::default_config().unwrap(); | |
| assert_eq!(config.inference.embedding_dim, 384); | |
| assert_eq!(config.inference.inference_batch_size, 32); | |
| assert_eq!(config.inference.max_seq_length, 128); | |
| } | |
| } | |