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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -4
app.py CHANGED
@@ -21,16 +21,25 @@ def embed_query(text):
21
  return np.array(response.data[0].embedding, dtype=np.float32).reshape(1, -1)
22
 
23
  # Semantic search using FAISS (for older FAISS versions)
 
 
24
  def search(query, k=3):
25
  query_vec = embed_query(query).astype(np.float32)
26
 
27
- distances = np.empty((1, k), dtype=np.float32)
28
- labels = np.empty((1, k), dtype=np.int64)
 
 
 
 
 
 
 
 
 
29
 
30
- index.search(query_vec, k, distances, labels)
31
  return [chunks[i] for i in labels[0]]
32
 
33
- # Chat modes
34
  def chat_no_rag(question):
35
  response = client.chat.completions.create(
36
  model="gpt-3.5-turbo",
 
21
  return np.array(response.data[0].embedding, dtype=np.float32).reshape(1, -1)
22
 
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
 
 
43
  def chat_no_rag(question):
44
  response = client.chat.completions.create(
45
  model="gpt-3.5-turbo",