Wajahat698 commited on
Commit
719348d
·
verified ·
1 Parent(s): c0e6777

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -11
app.py CHANGED
@@ -1193,19 +1193,25 @@ update_message_counter()
1193
 
1194
 
1195
  # Define search functions
1196
- def search_knowledge_base(query):
1197
- """
1198
- Searches the FAISS index using the provided query.
1199
- Returns the most relevant documents based on the query.
1200
- """
1201
- if "faiss_db" not in st.session_state:
1202
- st.error("FAISS database is not initialized.")
1203
- return []
 
 
 
 
 
1204
 
1205
- # Retrieve the top 5 most relevant documents
1206
- retrieved_docs = st.session_state["faiss_db"].similarity_search(query, k=7)
1207
- return retrieved_docs
1208
 
 
 
1209
 
1210
  def google_search(query):
1211
  """
 
1193
 
1194
 
1195
  # Define search functions
1196
+ def search_knowledge_base(query, k=3):
1197
+ """Optimized FAISS search for main knowledge base and user-specific knowledge base."""
1198
+ results = []
1199
+
1200
+ # Search in the main FAISS index
1201
+ if "main_faiss_db" in st.session_state and st.session_state["main_faiss_db"] is not None:
1202
+ main_results = st.session_state["main_faiss_db"].similarity_search_with_score(query, k=5) # Fetch extra results for better ranking
1203
+ results.extend(main_results)
1204
+
1205
+ # Search in the selected document's FAISS index
1206
+ if "faiss_db" in st.session_state and st.session_state["faiss_db"] is not None:
1207
+ user_results = st.session_state["faiss_db"].similarity_search_with_score(query, k=5) # Fetch extra results for better ranking
1208
+ results.extend(user_results)
1209
 
1210
+ # Sort results by similarity score (higher score = more relevant)
1211
+ sorted_results = sorted(results, key=lambda x: x[1], reverse=True)
 
1212
 
1213
+ # Return only the top `k` most relevant results
1214
+ return [result[0] for result in sorted_results[:k]]
1215
 
1216
  def google_search(query):
1217
  """