Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export default function DocDetailsPage() {
const [chatResponse, setChatResponse] = useState("");
const [chatLoading, setChatLoading] = useState(false);
const [chatError, setChatError] = useState<string | null>(null);
const [lastRagContextSignature, setLastRagContextSignature] = useState("");

// Prompt dialog states
const [selectedText, setSelectedText] = useState<string>("");
Expand Down Expand Up @@ -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 {
Expand All @@ -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.");
}
Expand Down Expand Up @@ -280,6 +301,8 @@ export default function DocDetailsPage() {
setChatResponse("");

try {
await ensureRagContext(grandparentId);

const response = await axios.post(
`${RAG_BASE_URL}/query/${grandparentId}`,
{
Expand Down
10 changes: 5 additions & 5 deletions services/rag-service/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,15 @@ 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

index_file = context_file(context_id, "index.faiss")
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])
Expand All @@ -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:
Expand Down Expand Up @@ -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/<context_id>', methods=['POST'])
Expand Down Expand Up @@ -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
Expand Down
Loading