Skip to main content

graphrag_core/graphrag/
documents.rs

1#![allow(unused_imports)]
2
3use crate::config::Config;
4use crate::core::{
5    ChunkId, Document, DocumentId, Entity, EntityId, GraphRAGError, KnowledgeGraph, Relationship,
6    Result, TextChunk,
7};
8use crate::{critic, ollama, persistence, query, retrieval};
9
10#[cfg(feature = "parallel-processing")]
11#[allow(unused_imports)]
12use crate::parallel;
13
14use super::GraphRAG;
15
16impl GraphRAG {
17    /// Add a document from text content
18    pub fn add_document_from_text(&mut self, text: &str) -> Result<()> {
19        use crate::text::TextProcessor;
20        use indexmap::IndexMap;
21
22        // Use UUID for doc ID (works in both native and WASM)
23        let doc_id = DocumentId::new(format!("doc_{}", uuid::Uuid::new_v4().simple()));
24
25        let document = Document {
26            id: doc_id,
27            title: "Document".to_string(),
28            content: text.to_string(),
29            metadata: IndexMap::new(),
30            chunks: Vec::new(),
31        };
32
33        let text_processor =
34            TextProcessor::new(self.config.text.chunk_size, self.config.text.chunk_overlap)?;
35        let chunks = text_processor.chunk_text(&document)?;
36
37        let document_with_chunks = Document { chunks, ..document };
38
39        self.add_document(document_with_chunks)
40    }
41
42    /// Add a document to the system
43    pub fn add_document(&mut self, document: Document) -> Result<()> {
44        let graph = self
45            .knowledge_graph
46            .as_mut()
47            .ok_or_else(|| GraphRAGError::Config {
48                message: "Knowledge graph not initialized".to_string(),
49            })?;
50
51        graph.add_document(document)
52    }
53}