Skip to content

Commit fc108c3

Browse files
author
Daniele Briggi
committed
feat(hash): better file hashing
1 parent feb738b commit fc108c3

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/sqlite_rag/engine.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,12 @@ def generate_embedding(self, chunks: list[Chunk]) -> list[Chunk]:
4949
cursor = self._conn.cursor()
5050

5151
for chunk in chunks:
52-
cursor.execute("SELECT llm_embed_generate(?) AS embedding", (chunk.content,))
52+
try:
53+
cursor.execute("SELECT llm_embed_generate(?) AS embedding", (chunk.content,))
54+
except sqlite3.Error as e:
55+
print(f"Error generating embedding for chunk\n: ```{chunk.content}```")
56+
raise e
57+
5358
result = cursor.fetchone()
5459

5560
if result is None:

src/sqlite_rag/models/document.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
import hashlib
23

34
from attr import dataclass
45

@@ -24,4 +25,4 @@ class Document:
2425

2526
def hash(self) -> str:
2627
"""Generate a hash for the document content"""
27-
return str(hash(self.content))
28+
return hashlib.blake2b(self.content.encode()).hexdigest()

src/sqlite_rag/sqliterag.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,13 @@ def add(self, path: str, recursive: bool = False) -> int:
6464

6565
exists = self._repository.document_exists_by_hash(document.hash())
6666
if exists:
67-
self._logger.info(f"Unchanged document: {file_path}")
67+
self._logger.info(f"Unchanged: {file_path}")
6868
continue
6969

70+
self._logger.info(f"Processing: {file_path}")
7071
document = self._engine.process(document)
7172

7273
self._repository.add_document(document)
73-
self._logger.info(str(file_path))
7474

7575
if self.settings.quantize_scan:
7676
self._engine.quantize()
@@ -108,10 +108,10 @@ def remove_document(self, identifier: str) -> bool:
108108

109109
# First find the document to get its ID
110110
document = self._repository.find_document_by_id_or_uri(identifier)
111-
if not document:
111+
if not document or not document.id:
112112
return False
113113

114-
return self._repository.remove_document(document.id or "")
114+
return self._repository.remove_document(document.id)
115115

116116
def rebuild(self, remove_missing: bool = False) -> dict:
117117
"""Rebuild embeddings and full-text index for all documents"""
@@ -124,13 +124,15 @@ def rebuild(self, remove_missing: bool = False) -> dict:
124124
removed = 0
125125

126126
for doc in documents:
127+
doc_id = doc.id or ""
128+
127129
if doc.uri and Path(doc.uri).exists():
128130
# File still exists, recreate embeddings
129131
try:
130132
content = FileReader.parse_file(Path(doc.uri))
131133
doc.content = content
132134

133-
self._repository.remove_document(doc.id or "")
135+
self._repository.remove_document(doc_id)
134136
processed_doc = self._engine.process(doc)
135137
self._repository.add_document(processed_doc)
136138

@@ -151,7 +153,7 @@ def rebuild(self, remove_missing: bool = False) -> dict:
151153
else:
152154
# Document without URI (text content)
153155
try:
154-
self._repository.remove_document(doc.id or "")
156+
self._repository.remove_document(doc_id)
155157
processed_doc = self._engine.process(doc)
156158
self._repository.add_document(processed_doc)
157159

0 commit comments

Comments
 (0)