From d7aeecbae0738e5e3de45b1845a69a2690261bea Mon Sep 17 00:00:00 2001 From: Ranga Dananjaya Date: Mon, 28 Jul 2025 10:41:48 +0530 Subject: [PATCH 1/2] Check for duplicate conversations before insertion into the database. --- main.py | 24 ++++++++++++++++++++---- 1 file changed, 20 insertions(+), 4 deletions(-) diff --git a/main.py b/main.py index d0ac1bd..7a62e01 100644 --- a/main.py +++ b/main.py @@ -83,13 +83,29 @@ def ask_ai(data: InputData): capture_output=True, text=True ) - if result.stdout.strip(): - users_collection.insert_one({ + + ai_response = result.stdout.strip() + + # Check if this exact conversation already exists before inserting + if ai_response: + existing_doc = users_collection.find_one({ "user_id": data.user_ID, "user_promt": data.query, - "AI": result.stdout.strip() + "AI": ai_response }) - return {"response": result.stdout.strip(), "error": result.stderr.strip()} + + # Only insert if this exact conversation doesn't exist + if not existing_doc: + users_collection.insert_one({ + "user_id": data.user_ID, + "user_promt": data.query, + "AI": ai_response + }) + print(f"New conversation stored for user {data.user_ID}") + else: + print(f"Duplicate conversation found for user {data.user_ID}, skipping insert") + + return {"response": ai_response, "error": result.stderr.strip()} except Exception as e: return {"error": str(e)} From cd08e96007ef39bc22de6282164aa2485eee1ba8 Mon Sep 17 00:00:00 2001 From: Ranga Dananjaya Date: Mon, 28 Jul 2025 12:41:02 +0530 Subject: [PATCH 2/2] Enhance conversation handling by deleting previous duplicates before inserting new entries into the database. --- main.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main.py b/main.py index 7a62e01..5e4eb62 100644 --- a/main.py +++ b/main.py @@ -86,24 +86,28 @@ def ask_ai(data: InputData): ai_response = result.stdout.strip() - # Check if this exact conversation already exists before inserting + # Check if this conversation already exists and update with latest data if ai_response: existing_doc = users_collection.find_one({ "user_id": data.user_ID, - "user_promt": data.query, - "AI": ai_response + "user_promt": data.query }) - # Only insert if this exact conversation doesn't exist - if not existing_doc: - users_collection.insert_one({ + if existing_doc: + # Delete ALL previous documents with the same query for this user + delete_result = users_collection.delete_many({ "user_id": data.user_ID, - "user_promt": data.query, - "AI": ai_response + "user_promt": data.query }) - print(f"New conversation stored for user {data.user_ID}") - else: - print(f"Duplicate conversation found for user {data.user_ID}, skipping insert") + print(f"Deleted {delete_result.deleted_count} previous conversations for user {data.user_ID}") + + # Insert the new conversation (whether it was a duplicate or not) + users_collection.insert_one({ + "user_id": data.user_ID, + "user_promt": data.query, + "AI": ai_response + }) + print(f"Conversation stored for user {data.user_ID}") return {"response": ai_response, "error": result.stderr.strip()} except Exception as e: