-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfaiss_test.py
36 lines (27 loc) · 1.05 KB
/
faiss_test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
from sentence_transformers import SentenceTransformer
import faiss
import numpy as np
import pickle
# Load the .pkl file
with open('documents/all_documents_text.pkl', 'rb') as f:
results = pickle.load(f)
# Load the SentenceTransformer model
model = SentenceTransformer('embedding_model')
# Load the FAISS index
index = faiss.read_index('faiss_index/bert_sentence_transformer.faiss')
# Sentence transformer inference
def search(query_text, top_k=5):
# Encode the query
query_vector = model.encode([query_text])
# Search the Faiss index
distances, indices = index.search(query_vector, top_k)
# Retrieve the original rows for the closest matches
closest_rows = [results[i] for i in indices[0]]
return closest_rows
# Querying the model
'''
Items 1-6 correspond to the following in order - url, entire_text, case_title, importance_number, judgment_date, facts, conclusion
'''
query = "An officer of the law physically assaulted me without any apparent or communicated reason?"
top_results = search(query, top_k=5)
print(type(top_results))