Skip to content

~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.

trying to fix RAG

LearningCircuitcbbf631eb75b
diff --git a/src/local_deep_research/research_library/routes/rag_routes.py b/src/local_deep_research/research_library/routes/rag_routes.py
index 24bbbe0bab..8d7300c19f 100644
--- a/src/local_deep_research/research_library/routes/rag_routes.py
+++ b/src/local_deep_research/research_library/routes/rag_routes.py
@@ -128,7 +128,9 @@ def view_document_chunks(document_id):
"start_char": chunk.start_char,
"end_char": chunk.end_char,
"embedding_model": chunk.embedding_model,
- "embedding_model_type": chunk.embedding_model_type,
+ "embedding_model_type": chunk.embedding_model_type.value
+ if chunk.embedding_model_type
+ else None,
"embedding_dimension": chunk.embedding_dimension,
"created_at": chunk.created_at,
}
@@ -288,15 +290,30 @@ def get_available_models():
@login_required
def get_index_info():
"""Get information about the current RAG index."""
+ from ...database.library_init import get_default_library_id
+
try:
+ # Get collection_id from request or use default Library collection
+ collection_id = request.args.get("collection_id")
+ if not collection_id:
+ collection_id = get_default_library_id(session["username"])
+
+ logger.info(
+ f"Getting RAG index info for collection_id: {collection_id}"
+ )
+
rag_service = get_rag_service()
- info = rag_service.get_current_index_info()
+ info = rag_service.get_current_index_info(collection_id)
if info is None:
+ logger.info(
+ f"No RAG index found for collection_id: {collection_id}"
+ )
return jsonify(
{"success": True, "info": None, "message": "No index found"}
)
+ logger.info(f"Found RAG index for collection_id: {collection_id}")
return jsonify({"success": True, "info": info})
except Exception as e:
logger.exception("Error getting index info")
@@ -420,6 +437,10 @@ def index_all():
if not collection_id:
collection_id = get_default_library_id(username)
+ logger.info(
+ f"Starting index-all for collection_id: {collection_id}, force_reindex: {force_reindex}"
+ )
+
# Create RAG service in request context before generator runs
rag_service = get_rag_service()
@@ -659,6 +680,18 @@ def get_documents():
# Debug logging to help diagnose indexing status issues
indexed_count = sum(1 for d in documents if d["rag_indexed"])
+
+ # Additional debug: check all DocumentCollection entries for this collection
+ all_doc_collections = (
+ db_session.query(DocumentCollection)
+ .filter_by(collection_id=collection_id)
+ .all()
+ )
+ indexed_in_db = sum(1 for dc in all_doc_collections if dc.indexed)
+ logger.info(
+ f"DocumentCollection table shows: {indexed_in_db}/{len(all_doc_collections)} indexed for collection {collection_id}"
+ )
+
logger.info(
f"Returning {len(documents)} documents on page {page}: "
f"{indexed_count} indexed, {len(documents) - indexed_count} not indexed"
@@ -836,7 +869,9 @@ def get_collections():
if coll.embedding_model:
collection_data["embedding"] = {
"model": coll.embedding_model,
- "provider": coll.embedding_model_type,
+ "provider": coll.embedding_model_type.value
+ if coll.embedding_model_type
+ else None,
"dimension": coll.embedding_dimension,
"chunk_size": coll.chunk_size,
"chunk_overlap": coll.chunk_overlap,
diff --git a/src/local_deep_research/research_library/services/library_rag_service.py b/src/local_deep_research/research_library/services/library_rag_service.py
index 0be6a76d00..9c0d5bf7e8 100644
--- a/src/local_deep_research/research_library/services/library_rag_service.py
+++ b/src/local_deep_research/research_library/services/library_rag_service.py
@@ -123,11 +123,13 @@ class LibraryRAGService:
cache_dir.mkdir(parents=True, exist_ok=True)
return cache_dir / f"{index_hash}.faiss"
- def _get_or_create_rag_index(self) -> RAGIndex:
+ def _get_or_create_rag_index(self, collection_id: str) -> RAGIndex:
"""Get or create RAGIndex record for the current configuration."""
with get_user_db_session(self.username) as session:
+ # Use collection_<uuid> format
+ collection_name = f"collection_{collection_id}"
index_hash = self._get_index_hash(
- "library", self.embedding_model, self.embedding_provider
+ collection_name, self.embedding_model, self.embedding_provider
)
# Try to get existing index
@@ -146,7 +148,7 @@ class LibraryRAGService:
embedding_dim = len(test_embedding)
rag_index = RAGIndex(
- collection_name="library",
+ collection_name=collection_name,
embedding_model=self.embedding_model,
embedding_model_type=EmbeddingProvider(
self.embedding_provider
@@ -168,14 +170,17 @@ class LibraryRAGService:
return rag_index
- def load_or_create_faiss_index(self) -> FAISS:
+ def load_or_create_faiss_index(self, collection_id: str) -> FAISS:
"""
Load existing FAISS index or create new one.
+ Args:
+ collection_id: UUID of the collection
+
Returns:
FAISS vector store instance
"""
- rag_index = self._get_or_create_rag_index()
+ rag_index = self._get_or_create_rag_index(collection_id)
self.rag_index_record = rag_index
index_path = Path(rag_index.index_path)
@@ -237,20 +242,22 @@ class LibraryRAGService:
collection_id: UUID of collection (defaults to Library if None)
"""
with get_user_db_session(self.username) as session:
- # Get collection name
+ # Get collection name in the format stored in RAGIndex (collection_<uuid>)
if collection_id:
collection = (
session.query(Collection)
.filter_by(id=collection_id)
.first()
)
- collection_name = collection.name if collection else "unknown"
+ collection_name = (
+ f"collection_{collection_id}" if collection else "unknown"
+ )
else:
# Default to Library collection
from ...database.library_init import get_default_library_id
collection_id = get_default_library_id(self.username)
- collection_name = "Library"
+ collection_name = f"collection_{collection_id}"
rag_index = (
session.query(RAGIndex)
@@ -259,6 +266,11 @@ class LibraryRAGService:
)
if not rag_index:
+ # Debug: check all RAG indices for this collection
+ all_indices = session.query(RAGIndex).all()
+ logger.info(
+ f"No RAG index found for collection_name='{collection_name}'. All indices: {[(idx.collection_name, idx.is_current) for idx in all_indices]}"
+ )
return None
# Calculate actual counts from DocumentCollection (not from chunks)
@@ -277,7 +289,9 @@ class LibraryRAGService:
return {
"embedding_model": rag_index.embedding_model,
- "embedding_model_type": rag_index.embedding_model_type,
+ "embedding_model_type": rag_index.embedding_model_type.value
+ if rag_index.embedding_model_type
+ else None,
"embedding_dimension": rag_index.embedding_dimension,
"chunk_size": rag_index.chunk_size,
"chunk_overlap": rag_index.chunk_overlap,
@@ -309,10 +323,18 @@ class LibraryRAGService:
return {"status": "error", "error": "Document not found"}
# Get or create DocumentCollection entry
- doc_collection = (
+ all_doc_collections = (
session.query(DocumentCollection)
.filter_by(document_id=document_id, collection_id=collection_id)
- .first()
+ .all()
+ )
+
+ logger.info(
+ f"Found {len(all_doc_collections)} DocumentCollection entries for doc={document_id}, coll={collection_id}"
+ )
+
+ doc_collection = (
+ all_doc_collections[0] if all_doc_collections else None
)
if not doc_collection:
@@ -325,6 +347,9 @@ class LibraryRAGService:
)
session.add(doc_collection)
session.flush()
+ logger.info(
+ f"Created new DocumentCollection entry for doc={document_id}, coll={collection_id}"
+ )
# Check if already indexed for this collection
if doc_collection.indexed and not force_reindex:
@@ -377,7 +402,10 @@ class LibraryRAGService:
.filter_by(id=collection_id)
.first()
)
- collection_name = collection.name if collection else "unknown"
+ # Use collection_<uuid> format for internal storage
+ collection_name = (
+ f"collection_{collection_id}" if collection else "unknown"
+ )
# Store chunks in database using embedding manager
embedding_ids = self.embedding_manager._store_chunks_to_db(
@@ -389,7 +417,9 @@ class LibraryRAGService:
# Load or create FAISS index
if self.faiss_index is None:
- self.faiss_index = self.load_or_create_faiss_index()
+ self.faiss_index = self.load_or_create_faiss_index(
+ collection_id
+ )
# If force_reindex, remove old chunks from FAISS before adding new ones
if force_reindex:
@@ -469,6 +499,15 @@ class LibraryRAGService:
doc_collection.chunk_count = len(chunks)
doc_collection.last_indexed_at = datetime.now(UTC)
+ # Force immediate flush to execute the UPDATE statement
+ # This is necessary because isolation_level=None (autocommit mode)
+ # doesn't work well with ORM delayed updates
+ session.flush()
+
+ logger.info(
+ f"Marked DocumentCollection as indexed: doc_id={document_id}, coll_id={collection_id}, chunks={len(chunks)}"
+ )
+
# Update RAGIndex statistics (only if not already indexed)
rag_index_obj = (
session.query(RAGIndex)
@@ -486,6 +525,20 @@ class LibraryRAGService:
# Commit and checkpoint WAL to ensure persistence
session.commit()
session.execute(text("PRAGMA wal_checkpoint(FULL)"))
+
+ # Verify the update was persisted
+ session.expire_all()
+ verification = (
+ session.query(DocumentCollection)
+ .filter_by(
+ document_id=document_id, collection_id=collection_id
+ )
+ .first()
+ )
+ logger.info(
+ f"Post-commit verification: doc={document_id}, indexed={verification.indexed if verification else 'NOT_FOUND'}, chunks={verification.chunk_count if verification else 0}"
+ )
+
logger.info(
f"Indexed document {document_id} with {len(chunks)} chunks"
)
@@ -619,13 +672,16 @@ class LibraryRAGService:
}
try:
- # Get collection name
+ # Get collection name in the format collection_<uuid>
collection = (
session.query(Collection)
.filter_by(id=collection_id)
.first()
)
- collection_name = collection.name if collection else "unknown"
+ # Use collection_<uuid> format for internal storage
+ collection_name = (
+ f"collection_{collection_id}" if collection else "unknown"
+ )
# Delete chunks from database
deleted_count = self.embedding_manager._delete_chunks_from_db(
@@ -788,11 +844,13 @@ class LibraryRAGService:
or 0
)
- # Get collection name
+ # Get collection name in the format stored in DocumentChunk (collection_<uuid>)
collection = (
session.query(Collection).filter_by(id=collection_id).first()
)
- collection_name = collection.name if collection else "library"
+ collection_name = (
+ f"collection_{collection_id}" if collection else "library"
+ )
# Get embedding model info from chunks
chunk_sample = (
@@ -805,7 +863,9 @@ class LibraryRAGService:
if chunk_sample:
embedding_info = {
"model": chunk_sample.embedding_model,
- "model_type": chunk_sample.embedding_model_type,
+ "model_type": chunk_sample.embedding_model_type.value
+ if chunk_sample.embedding_model_type
+ else None,
"dimension": chunk_sample.embedding_dimension,
}
diff --git a/src/local_deep_research/web/static/js/rag_manager.js b/src/local_deep_research/web/static/js/rag_manager.js
index b6e54fd760..b2fef86268 100644
--- a/src/local_deep_research/web/static/js/rag_manager.js
+++ b/src/local_deep_research/web/static/js/rag_manager.js
@@ -250,8 +250,10 @@ function updateModelDescription() {
async function loadCurrentIndexInfo() {
const infoContainer = document.getElementById('current-index-info');
+ if (!currentCollectionId) return;
+
try {
- const response = await safeFetch('/library/api/rag/info');
+ const response = await safeFetch(`/library/api/rag/info?collection_id=${currentCollectionId}`);
const data = await response.json();
if (data.success) {
@@ -426,7 +428,7 @@ function renderStats(stats) {
statsContainer.innerHTML = `
<div class="ldr-stat-card">
- <span class="ldr-stat-value">${stats.total_text_documents}</span>
+ <span class="ldr-stat-value">${stats.total_documents}</span>
<span class="ldr-stat-label">Total Text Documents</span>
</div>
<div class="ldr-stat-card">