daniel.diaz commited on
Commit
c8801fd
·
1 Parent(s): 6342765

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -11
app.py CHANGED
@@ -23,20 +23,17 @@ def embed_query(text):
23
  # Semantic search using FAISS (for older FAISS versions)
24
 
25
  # Semantic search with fallback handling
 
 
26
  def search(query, k=3):
27
  query_vec = embed_query(query).astype(np.float32)
28
 
29
- try:
30
- # Try modern FAISS API (common in recent versions)
31
- distances, labels = index.search(query_vec, k)
32
- except TypeError:
33
- try:
34
- # Try older FAISS API where distances and labels must be preallocated
35
- distances = np.empty((1, k), dtype=np.float32)
36
- labels = np.empty((1, k), dtype=np.int64)
37
- index.search(query_vec, k, distances, labels)
38
- except Exception as e:
39
- raise RuntimeError(f"FAISS search failed: {e}")
40
 
41
  return [chunks[i] for i in labels[0]]
42
 
 
23
  # Semantic search using FAISS (for older FAISS versions)
24
 
25
  # Semantic search with fallback handling
26
+
27
+ # Semantic search using FAISS - strictly for older API with preallocated arrays
28
  def search(query, k=3):
29
  query_vec = embed_query(query).astype(np.float32)
30
 
31
+ # Preallocate arrays (required for FAISS IndexFlatL2 in older versions)
32
+ distances = np.empty((1, k), dtype=np.float32)
33
+ labels = np.empty((1, k), dtype=np.int64)
34
+
35
+ # Call FAISS with all required arguments
36
+ index.search(query_vec, k, distances, labels)
 
 
 
 
 
37
 
38
  return [chunks[i] for i in labels[0]]
39