1- import glob
2- from pathlib import Path
1+ import re
32import sqlite3
3+ from pathlib import Path
44from typing import Optional
55
6+ from sqlite_rag .logger import Logger
7+
68from .chunker import Chunker
79from .database import Database
810from .engine import Engine
@@ -17,10 +19,12 @@ def __init__(self, settings: Optional[Settings] = None):
1719 if settings is None :
1820 # TODO: load defaults or from the database
1921 settings = Settings (
20- model_path_or_name = "./all-MiniLM-L6-v2.e4ce9877.q8_0.gguf" , db_path = "sqliterag.db"
22+ model_path_or_name = "./all-MiniLM-L6-v2.e4ce9877.q8_0.gguf" ,
23+ db_path = "sqliterag.db" ,
2124 )
2225
2326 self .settings = settings
27+ self ._logger = Logger ()
2428
2529 self ._conn = sqlite3 .connect (settings .db_path )
2630
@@ -37,54 +41,39 @@ def _ensure_initialized(self):
3741
3842 self .ready = True
3943
40- def add (self , path : str , recursively : bool = False ):
44+ def add (self , path : str , recursive : bool = False ) -> int :
4145 """Add the file content into the database"""
4246 self ._ensure_initialized ()
4347
4448 if not Path (path ).exists ():
4549 raise FileNotFoundError (f"{ path } does not exist." )
4650
47- files_to_process : list [Path ] = []
48- path_obj = Path (path )
49-
50- if path_obj .is_file ():
51- files_to_process .append (path_obj )
52- elif path_obj .is_dir ():
53- if recursively :
54- files_to_process = list (path_obj .rglob ("*" ))
55- else :
56- files_to_process = list (path_obj .glob ("*" ))
57-
58- files_to_process = [
59- f
60- for f in files_to_process
61- if f .is_file () and FileReader .is_supported (f )
62- ]
51+ files_to_process = FileReader .collect_files (Path (path ), recursive = recursive )
6352
53+ self ._logger .info (f"Processing { len (files_to_process )} files..." )
6454 for file_path in files_to_process :
65- # TODO: check the file extension
66- content = FileReader .parse_file (file_path )
6755 # TODO: include metadata extraction and mdx options (see our docsearch)
68- document = Document ( content = content , uri = str ( file_path ) )
69- chunks = self ._chunker . chunk ( document . content )
70- chunks = self . _engine . generate_embedding ( chunks )
71- document . chunks = chunks
56+ content = FileReader . parse_file ( file_path )
57+ document = self ._engine . process (
58+ Document ( content = content , uri = str ( file_path ) )
59+ )
7260
7361 self ._repository .add_document (document )
62+ self ._logger .info (str (file_path ))
7463
7564 if self .settings .quantize_scan :
7665 self ._engine .quantize ()
7766
67+ return len (files_to_process )
68+
7869 def add_text (
7970 self , text : str , uri : Optional [str ] = None , metadata : dict = {}
8071 ) -> None :
8172 """Add a text content into the database"""
8273 self ._ensure_initialized ()
8374
8475 document = Document (content = text , uri = uri , metadata = metadata )
85- chunks = self ._chunker .chunk (document .content )
86- chunks = self ._engine .generate_embedding (chunks )
87- document .chunks = chunks
76+ document = self ._engine .process (document )
8877
8978 self ._repository .add_document (document )
9079
@@ -105,16 +94,16 @@ def find_document(self, identifier: str) -> Document | None:
10594 def remove_document (self , identifier : str ) -> bool :
10695 """Remove document by ID or URI"""
10796 self ._ensure_initialized ()
108-
97+
10998 # First find the document to get its ID
11099 document = self ._repository .find_document_by_id_or_uri (identifier )
111100 if not document :
112101 return False
113-
102+
114103 return self ._repository .remove_document (document .id or "" )
115104
116105 # def search(
117106 # self, query: str, top_k: int = 10
118107 # ) -> list[Document]:
119108 # """Search for documents matching the query"""
120- # self._ensure_initialized()
109+ # self._ensure_initialized()
0 commit comments