Ralqasimi commited on
Commit
aa7ef94
·
verified ·
1 Parent(s): a99e8b1

Update knowledge_base.py

Browse files
Files changed (1) hide show
  1. knowledge_base.py +4 -8
knowledge_base.py CHANGED
@@ -32,20 +32,16 @@ def create_faiss_index(texts):
32
  return index, texts
33
 
34
  # Search the FAISS index
35
- def search_faiss(faiss_index, stored_texts, query, top_k=3):
36
  """
37
- Search the FAISS index for the most relevant texts based on the query.
38
  """
39
- # Load the same model used for indexing
40
- model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
41
 
42
- # Encode the query into an embedding
43
  query_embedding = model.encode([query])
44
 
45
- # Search the FAISS index
46
  distances, indices = faiss_index.search(query_embedding, top_k)
47
-
48
- # Retrieve the corresponding texts
49
  results = [stored_texts[i] for i in indices[0] if i < len(stored_texts)]
50
 
51
  return results
 
32
  return index, texts
33
 
34
  # Search the FAISS index
35
+ def search_faiss(faiss_index, stored_texts, query, top_k=3):
36
  """
37
+ Search FAISS for the most relevant texts.
38
  """
39
+ from sentence_transformers import SentenceTransformer
 
40
 
41
+ model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
42
  query_embedding = model.encode([query])
43
 
 
44
  distances, indices = faiss_index.search(query_embedding, top_k)
 
 
45
  results = [stored_texts[i] for i in indices[0] if i < len(stored_texts)]
46
 
47
  return results