diff --git a/apps/frontend/src/app/office/[id]/team/[teamId]/docs/[docsId]/page.tsx b/apps/frontend/src/app/office/[id]/team/[teamId]/docs/[docsId]/page.tsx index 5f3e5e7..76ed0b5 100644 --- a/apps/frontend/src/app/office/[id]/team/[teamId]/docs/[docsId]/page.tsx +++ b/apps/frontend/src/app/office/[id]/team/[teamId]/docs/[docsId]/page.tsx @@ -77,6 +77,7 @@ export default function DocDetailsPage() { const [chatResponse, setChatResponse] = useState(""); const [chatLoading, setChatLoading] = useState(false); const [chatError, setChatError] = useState(null); + const [lastRagContextSignature, setLastRagContextSignature] = useState(""); // Prompt dialog states const [selectedText, setSelectedText] = useState(""); @@ -195,6 +196,24 @@ export default function DocDetailsPage() { } }; + const buildRagContext = () => { + const heading = title || doc?.title || ""; + const body = editor?.getText().trim() || doc?.content || ""; + return [heading, body].filter(Boolean).join("\n\n"); + }; + + const ensureRagContext = async (contextId: string) => { + const context = buildRagContext(); + const signature = `${contextId}:${context}`; + + if (!context.trim() || signature === lastRagContextSignature) { + return; + } + + await saveContextToFlask(contextId, context); + setLastRagContextSignature(signature); + }; + // Update the doc in the database and save context const handleUpdateDoc = async () => { try { @@ -208,7 +227,9 @@ export default function DocDetailsPage() { // Save the updated content to Flask backend using grandparentId if (grandparentId) { - await saveContextToFlask(grandparentId, updatedDoc.content); + const context = buildRagContext(); + await saveContextToFlask(grandparentId, context); + setLastRagContextSignature(`${grandparentId}:${context}`); } else { console.warn("Grandparent ID is not available."); } @@ -280,6 +301,8 @@ export default function DocDetailsPage() { setChatResponse(""); try { + await ensureRagContext(grandparentId); + const response = await axios.post( `${RAG_BASE_URL}/query/${grandparentId}`, { diff --git a/services/rag-service/app.py b/services/rag-service/app.py index 44e6a82..1266642 100644 --- a/services/rag-service/app.py +++ b/services/rag-service/app.py @@ -124,7 +124,7 @@ def generate_embeddings(chunks): embeddings.append(outputs.last_hidden_state.mean(dim=1).detach().numpy()) return embeddings -def save_to_faiss(context_id, embeddings, chunks): +def save_to_faiss(context_id, embeddings, chunks, replace=False): if not embeddings: return @@ -132,7 +132,7 @@ def save_to_faiss(context_id, embeddings, chunks): chunks_file = context_file(context_id, "chunks.json") # Load or create FAISS index - if os.path.exists(index_file): + if os.path.exists(index_file) and not replace: index = faiss.read_index(index_file) else: index = faiss.IndexFlatL2(embeddings[0].shape[1]) @@ -141,7 +141,7 @@ def save_to_faiss(context_id, embeddings, chunks): faiss.write_index(index, index_file) # Save chunks - if os.path.exists(chunks_file): + if os.path.exists(chunks_file) and not replace: with open(chunks_file, "r") as file: existing_chunks = json.load(file) else: @@ -233,7 +233,7 @@ def add_context(context_id): split_documents = split_document(request_data['context']) embeddings = generate_embeddings(split_documents) - save_to_faiss(context_id, embeddings, split_documents) + save_to_faiss(context_id, embeddings, split_documents, replace=True) return jsonify({'status': f'Context added successfully for context ID {context_id}'}), 200 @app.route('/query/', methods=['POST']) @@ -266,7 +266,7 @@ def save_context(context_type, context_id): split_documents = split_document(request_data['context']) embeddings = generate_embeddings(split_documents) - save_to_faiss(f"{context_type}_{context_id}", embeddings, split_documents) + save_to_faiss(f"{context_type}_{context_id}", embeddings, split_documents, replace=True) return jsonify({'status': f'Context added successfully for {context_type} ID {context_id}'}), 200 # Generic function to query context