graphrag-core 0.2.0

Core portable library for GraphRAG - works on native and WASM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
// Allow dead code for configuration structures used for TOML parsing
#![allow(dead_code)]

use crate::config::Config;
use crate::core::{GraphRAGError, Result};
use std::fs;
use std::path::Path;

#[cfg(feature = "toml-support")]
use toml;

#[cfg(feature = "serde_json")]
use serde_json;

/// Configuration file format
#[derive(Debug, Clone)]
pub enum ConfigFormat {
    /// TOML configuration format
    Toml,
    /// JSON configuration format
    Json,
    /// YAML configuration format
    Yaml,
}

impl ConfigFormat {
    /// Determine configuration format from file extension
    pub fn from_extension(path: &str) -> Self {
        let path = Path::new(path);
        match path.extension().and_then(|s| s.to_str()) {
            Some("toml") => ConfigFormat::Toml,
            Some("json") => ConfigFormat::Json,
            Some("yaml" | "yml") => ConfigFormat::Yaml,
            _ => ConfigFormat::Toml, // Default
        }
    }
}

/// Load configuration from file
pub fn load_config(path: &str) -> Result<Config> {
    let format = ConfigFormat::from_extension(path);

    if !Path::new(path).exists() {
        return Err(GraphRAGError::Config {
            message: format!("Configuration file not found: {path}"),
        });
    }

    let content = fs::read_to_string(path)?;

    match format {
        ConfigFormat::Toml => load_toml_config(&content),
        ConfigFormat::Json => load_json_config(&content),
        ConfigFormat::Yaml => load_yaml_config(&content),
    }
}

#[cfg(feature = "toml-support")]
fn load_toml_config(content: &str) -> Result<Config> {
    let raw_config: RawConfig = toml::from_str(content).map_err(|e| GraphRAGError::Config {
        message: format!("Failed to parse TOML config: {e}"),
    })?;

    Ok(convert_raw_config(raw_config))
}

#[cfg(not(feature = "toml-support"))]
fn load_toml_config(_content: &str) -> Result<Config> {
    Err(GraphRAGError::Config {
        message: "TOML support not enabled. Enable 'toml-support' feature.".to_string(),
    })
}

#[cfg(feature = "serde_json")]
fn load_json_config(content: &str) -> Result<Config> {
    let raw_config: RawConfig =
        serde_json::from_str(content).map_err(|e| GraphRAGError::Config {
            message: format!("Failed to parse JSON config: {e}"),
        })?;

    Ok(convert_raw_config(raw_config))
}

#[cfg(not(feature = "serde_json"))]
fn load_json_config(_content: &str) -> Result<Config> {
    Err(GraphRAGError::Config {
        message: "JSON support not enabled. Enable 'serde_json' feature.".to_string(),
    })
}

#[cfg(feature = "yaml-support")]
fn load_yaml_config(content: &str) -> Result<Config> {
    let raw_config: RawConfig =
        serde_yaml::from_str(content).map_err(|e| GraphRAGError::Config {
            message: format!("Failed to parse YAML config: {e}"),
        })?;

    Ok(convert_raw_config(raw_config))
}

#[cfg(not(feature = "yaml-support"))]
fn load_yaml_config(_content: &str) -> Result<Config> {
    Err(GraphRAGError::Config {
        message: "YAML support not enabled. Enable 'yaml-support' feature.".to_string(),
    })
}

/// Raw configuration structure that matches the TOML file
#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct RawConfig {
    #[serde(default)]
    system: SystemConfig,
    #[serde(default)]
    features: FeaturesConfig,
    #[serde(default)]
    text_processing: RawTextProcessingConfig,
    #[serde(default)]
    entity_extraction: RawEntityExtractionConfig,
    #[serde(default)]
    graph_construction: RawGraphConstructionConfig,
    #[serde(default)]
    vector_processing: RawVectorProcessingConfig,
    #[serde(default)]
    query_processing: RawQueryProcessingConfig,
    #[serde(default)]
    adaptive_retrieval: RawAdaptiveRetrievalConfig,
    #[serde(default)]
    ranking_policies: RawRankingPoliciesConfig,
    #[serde(default)]
    reranking: RawRerankingConfig,
    #[serde(default)]
    generation: RawGenerationConfig,
    #[serde(default)]
    ollama: RawOllamaConfig,
    #[serde(default)]
    async_processing: RawAsyncProcessingConfig,
    #[serde(default)]
    function_calling: RawFunctionCallingConfig,
    #[serde(default)]
    monitoring: RawMonitoringConfig,
    #[serde(default)]
    storage: RawStorageConfig,
    #[serde(default)]
    parallel_processing: RawParallelProcessingConfig,
    #[serde(default)]
    logging: RawLoggingConfig,
    #[serde(default)]
    experimental: RawExperimentalConfig,
}

#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct SystemConfig {
    log_level: Option<String>,
    max_memory_mb: Option<u64>,
    temp_dir: Option<String>,
    output_dir: Option<String>,
}

#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct FeaturesConfig {
    text_processing: Option<bool>,
    entity_extraction: Option<bool>,
    graph_construction: Option<bool>,
    vector_processing: Option<bool>,
    async_processing: Option<bool>,
    function_calling: Option<bool>,
    monitoring: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct RawTextProcessingConfig {
    enabled: Option<bool>,
    chunk_size: Option<usize>,
    chunk_overlap: Option<usize>,
    min_chunk_size: Option<usize>,
    max_chunk_size: Option<usize>,
    normalize_whitespace: Option<bool>,
    remove_artifacts: Option<bool>,
    extract_keywords: Option<bool>,
    keyword_min_score: Option<f64>,
    #[serde(default)]
    enrichment: Option<RawEnrichmentConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct RawEnrichmentConfig {
    enabled: Option<bool>,
    auto_detect_format: Option<bool>,
    parser_type: Option<String>,
    extract_keywords: Option<bool>,
    max_keywords_per_chunk: Option<usize>,
    use_tfidf: Option<bool>,
    generate_summaries: Option<bool>,
    min_chunk_length_for_summary: Option<usize>,
    max_summary_length: Option<usize>,
    extract_chapter: Option<bool>,
    extract_section: Option<bool>,
    extract_position: Option<bool>,
    calculate_confidence: Option<bool>,
    detect_headings: Option<bool>,
    detect_numbering: Option<bool>,
    detect_underlines: Option<bool>,
    detect_all_caps: Option<bool>,
    detect_roman_numerals: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
#[allow(dead_code)]
struct RawEntityExtractionConfig {
    enabled: Option<bool>,
    min_confidence: Option<f32>,
    use_gleaning: Option<bool>,
    max_gleaning_rounds: Option<usize>,
    gleaning_improvement_threshold: Option<f64>,
    semantic_merging: Option<bool>,
    merge_similarity_threshold: Option<f64>,
    automatic_linking: Option<bool>,
    linking_confidence_threshold: Option<f64>,
    gleaning: Option<RawGleaningConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawGleaningConfig {
    focus_areas: Option<Vec<String>>,
    context_window: Option<usize>,
    llm_temperature: Option<f64>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawGraphConstructionConfig {
    enabled: Option<bool>,
    incremental_updates: Option<bool>,
    use_pagerank: Option<bool>,
    pagerank_damping: Option<f64>,
    pagerank_iterations: Option<usize>,
    pagerank_convergence: Option<f64>,
    extract_relationships: Option<bool>,
    relationship_confidence_threshold: Option<f64>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawVectorProcessingConfig {
    enabled: Option<bool>,
    embedding_model: Option<String>,
    embedding_dimensions: Option<usize>,
    use_hnsw_index: Option<bool>,
    hnsw_ef_construction: Option<usize>,
    hnsw_m: Option<usize>,
    similarity_threshold: Option<f64>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawQueryProcessingConfig {
    enabled: Option<bool>,
    use_advanced_pipeline: Option<bool>,
    use_intent_classification: Option<bool>,
    use_concept_extraction: Option<bool>,
    use_temporal_parsing: Option<bool>,
    confidence_threshold: Option<f64>,
    intent_classification: Option<RawIntentClassificationConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawIntentClassificationConfig {
    factual_patterns: Option<Vec<String>>,
    relational_patterns: Option<Vec<String>>,
    temporal_patterns: Option<Vec<String>>,
    causal_patterns: Option<Vec<String>>,
    comparative_patterns: Option<Vec<String>>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawAdaptiveRetrievalConfig {
    enabled: Option<bool>,
    default_strategies: Option<Vec<String>>,
    strategy_weights: Option<std::collections::HashMap<String, f64>>,
    dynamic_weighting: Option<bool>,
    diversity_factor: Option<f64>,
    max_results_per_strategy: Option<usize>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawRankingPoliciesConfig {
    enabled: Option<bool>,
    use_elbow_detection: Option<bool>,
    use_top_k_diversity: Option<bool>,
    use_threshold_filtering: Option<bool>,
    use_intent_aware_ranking: Option<bool>,
    use_confidence_filtering: Option<bool>,
    elbow_detection: Option<RawElbowDetectionConfig>,
    top_k: Option<RawTopKConfig>,
    threshold: Option<RawThresholdConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawElbowDetectionConfig {
    min_results: Option<usize>,
    max_results: Option<usize>,
    smoothing_factor: Option<f64>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawTopKConfig {
    k: Option<usize>,
    diversity_threshold: Option<f64>,
    entity_type_balance: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawThresholdConfig {
    min_score: Option<f64>,
    confidence_weight: Option<f64>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawRerankingConfig {
    enabled: Option<bool>,
    use_confidence_filtering: Option<bool>,
    use_cross_encoder: Option<bool>,
    use_diversity_selection: Option<bool>,
    final_result_limit: Option<usize>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawGenerationConfig {
    enabled: Option<bool>,
    use_context_assembly: Option<bool>,
    max_context_length: Option<usize>,
    use_prompt_templates: Option<bool>,
    include_citations: Option<bool>,
    include_confidence_scores: Option<bool>,
    templates: Option<RawTemplatesConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawTemplatesConfig {
    factual: Option<String>,
    relational: Option<String>,
    temporal: Option<String>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawOllamaConfig {
    enabled: Option<bool>,
    base_url: Option<String>,
    model_name: Option<String>,
    embedding_model: Option<String>,
    timeout_seconds: Option<u64>,
    max_retries: Option<u32>,
    generation: Option<RawOllamaGenerationConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawOllamaGenerationConfig {
    temperature: Option<f64>,
    top_p: Option<f64>,
    max_tokens: Option<u32>,
    stream: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawAsyncProcessingConfig {
    enabled: Option<bool>,
    max_concurrent_llm_calls: Option<usize>,
    max_concurrent_embeddings: Option<usize>,
    max_concurrent_documents: Option<usize>,
    llm_rate_limit_per_second: Option<f64>,
    embedding_rate_limit_per_second: Option<f64>,
    batching: Option<RawBatchingConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawBatchingConfig {
    batch_size: Option<usize>,
    batch_timeout_seconds: Option<u64>,
    max_batch_memory_mb: Option<usize>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawFunctionCallingConfig {
    enabled: Option<bool>,
    max_function_calls: Option<usize>,
    timeout_per_call_seconds: Option<u64>,
    allow_nested_calls: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawMonitoringConfig {
    enabled: Option<bool>,
    collect_performance_metrics: Option<bool>,
    collect_usage_statistics: Option<bool>,
    health_check_interval_seconds: Option<u64>,
    log_slow_operations: Option<bool>,
    slow_operation_threshold_ms: Option<u64>,
    benchmarking: Option<RawBenchmarkingConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawBenchmarkingConfig {
    enabled: Option<bool>,
    run_periodic_benchmarks: Option<bool>,
    benchmark_interval_hours: Option<u64>,
    auto_recommendations: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawStorageConfig {
    r#type: Option<String>,
    workspace_isolation: Option<bool>,
    max_workspaces: Option<usize>,
    backup_enabled: Option<bool>,
    backup_interval_hours: Option<u64>,
    persistent: Option<RawPersistentConfig>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawPersistentConfig {
    database_path: Option<String>,
    enable_wal: Option<bool>,
    cache_size_mb: Option<usize>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawParallelProcessingConfig {
    enabled: Option<bool>,
    max_threads: Option<usize>,
    thread_pool_size: Option<usize>,
    load_balancing: Option<bool>,
    work_stealing: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawLoggingConfig {
    level: Option<String>,
    format: Option<String>,
    include_timestamps: Option<bool>,
    include_module_path: Option<bool>,
    log_to_file: Option<bool>,
    log_file: Option<String>,
    max_log_file_mb: Option<usize>,
    rotate_logs: Option<bool>,
}

#[derive(Debug, serde::Deserialize, Default)]
struct RawExperimentalConfig {
    neural_reranking: Option<bool>,
    federated_learning: Option<bool>,
    real_time_updates: Option<bool>,
    distributed_processing: Option<bool>,
}

/// Convert raw configuration to the main Config struct
fn convert_raw_config(raw: RawConfig) -> Config {
    let mut config = Config::default();

    // Apply feature toggles
    if let Some(_text_enabled) = raw.features.text_processing {
        // Apply to appropriate config sections
    }

    // Apply text processing configuration
    if let Some(_chunk_size) = raw.text_processing.chunk_size {
        // config.text.chunk_size = chunk_size;
    }

    // Apply entity extraction configuration
    if let Some(min_confidence) = raw.entity_extraction.min_confidence {
        config.entities.min_confidence = min_confidence;
    }

    // Apply graph construction configuration
    if let Some(extract_rels) = raw.graph_construction.extract_relationships {
        config.graph.extract_relationships = extract_rels;
    }
    if let Some(threshold) = raw.graph_construction.relationship_confidence_threshold {
        config.graph.relationship_confidence_threshold = threshold as f32;
    }

    // Apply parallel processing configuration
    if let Some(enabled) = raw.parallel_processing.enabled {
        config.parallel.enabled = enabled;
    }
    if let Some(max_threads) = raw.parallel_processing.max_threads {
        config.parallel.num_threads = if max_threads == 0 {
            #[cfg(feature = "parallel-processing")]
            {
                num_cpus::get()
            }
            #[cfg(not(feature = "parallel-processing"))]
            {
                1
            }
        } else {
            max_threads
        };
    }

    config
}

/// Save configuration to file
pub fn save_config(config: &Config, path: &str) -> Result<()> {
    let format = ConfigFormat::from_extension(path);

    match format {
        ConfigFormat::Toml => save_toml_config(config, path),
        ConfigFormat::Json => save_json_config(config, path),
        ConfigFormat::Yaml => save_yaml_config(config, path),
    }
}

#[cfg(feature = "toml-support")]
fn save_toml_config(_config: &Config, path: &str) -> Result<()> {
    let content = r#"[text]
chunk_size = 1000
chunk_overlap = 200

[entities]
min_confidence = 0.7
entity_types = ["PERSON", "ORG", "LOCATION"]

[graph]
max_connections = 10
similarity_threshold = 0.8

[parallel]
enabled = true
num_threads = 0
"#;
    fs::write(path, content)?;
    Ok(())
}

#[cfg(not(feature = "toml-support"))]
fn save_toml_config(_config: &Config, _path: &str) -> Result<()> {
    Err(GraphRAGError::Config {
        message: "TOML support not enabled. Enable 'toml-support' feature.".to_string(),
    })
}

#[cfg(feature = "serde_json")]
fn save_json_config(_config: &Config, path: &str) -> Result<()> {
    let content = r#"{
  "text": {
    "chunk_size": 1000,
    "chunk_overlap": 200
  },
  "entities": {
    "min_confidence": 0.7,
    "entity_types": ["PERSON", "ORG", "LOCATION"]
  },
  "graph": {
    "max_connections": 10,
    "similarity_threshold": 0.8
  },
  "parallel": {
    "enabled": true,
    "num_threads": 0
  }
}"#;
    fs::write(path, content)?;
    Ok(())
}

#[cfg(not(feature = "serde_json"))]
fn save_json_config(_config: &Config, _path: &str) -> Result<()> {
    Err(GraphRAGError::Config {
        message: "JSON support not enabled.".to_string(),
    })
}

#[cfg(feature = "yaml-support")]
fn save_yaml_config(_config: &Config, path: &str) -> Result<()> {
    let content = r#"text:
  chunk_size: 1000
  chunk_overlap: 200

entities:
  min_confidence: 0.7
  entity_types: ["PERSON", "ORG", "LOCATION"]

graph:
  max_connections: 10
  similarity_threshold: 0.8

parallel:
  enabled: true
  num_threads: 0
"#;
    fs::write(path, content)?;
    Ok(())
}

#[cfg(not(feature = "yaml-support"))]
fn save_yaml_config(_config: &Config, _path: &str) -> Result<()> {
    Err(GraphRAGError::Config {
        message: "YAML support not enabled.".to_string(),
    })
}

// Removed convert_config_to_raw function as we now use static templates

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_config_format_detection() {
        assert!(matches!(
            ConfigFormat::from_extension("config.toml"),
            ConfigFormat::Toml
        ));
        assert!(matches!(
            ConfigFormat::from_extension("config.json"),
            ConfigFormat::Json
        ));
        assert!(matches!(
            ConfigFormat::from_extension("config.yaml"),
            ConfigFormat::Yaml
        ));
        assert!(matches!(
            ConfigFormat::from_extension("config.yml"),
            ConfigFormat::Yaml
        ));
        assert!(matches!(
            ConfigFormat::from_extension("config"),
            ConfigFormat::Toml
        ));
    }
}