Update knowledge_base.py
Browse files- knowledge_base.py +8 -2
knowledge_base.py
CHANGED
@@ -1,19 +1,25 @@
|
|
1 |
# Create FAISS index
|
2 |
def create_faiss_index(texts):
|
3 |
-
|
|
|
|
|
4 |
import faiss
|
5 |
from sentence_transformers import SentenceTransformer
|
6 |
|
|
|
7 |
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
8 |
embeddings = model.encode(texts)
|
9 |
|
|
|
10 |
dimension = embeddings.shape[1]
|
11 |
index = faiss.IndexFlatL2(dimension)
|
12 |
index.add(embeddings)
|
13 |
|
14 |
return index, texts
|
15 |
|
16 |
-
|
|
|
|
|
17 |
"""
|
18 |
Search the FAISS index for the most relevant texts based on the query.
|
19 |
"""
|
|
|
1 |
# Create FAISS index
|
2 |
def create_faiss_index(texts):
|
3 |
+
"""
|
4 |
+
Create a FAISS index from the provided list of texts.
|
5 |
+
"""
|
6 |
import faiss
|
7 |
from sentence_transformers import SentenceTransformer
|
8 |
|
9 |
+
# Load pre-trained SentenceTransformer model
|
10 |
model = SentenceTransformer("sentence-transformers/all-MiniLM-L6-v2")
|
11 |
embeddings = model.encode(texts)
|
12 |
|
13 |
+
# Create the FAISS index
|
14 |
dimension = embeddings.shape[1]
|
15 |
index = faiss.IndexFlatL2(dimension)
|
16 |
index.add(embeddings)
|
17 |
|
18 |
return index, texts
|
19 |
|
20 |
+
|
21 |
+
# Search the FAISS index
|
22 |
+
def search_faiss(faiss_index, stored_texts, query, top_k=3):
|
23 |
"""
|
24 |
Search the FAISS index for the most relevant texts based on the query.
|
25 |
"""
|