-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimilarity.py
More file actions
22 lines (20 loc) · 813 Bytes
/
similarity.py
File metadata and controls
22 lines (20 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import torch
from sentence_transformers.util import cos_sim
from embeddings import faq_embeddings, data, model
from preprocess import clean_text
import logging
logging.basicConfig(filename="low_score_questions.log", level=logging.INFO, encoding="utf-8")
def get_best_answer(user_question):
user_question = clean_text(user_question)
user_embedding = model.encode(
[user_question],
convert_to_tensor=True
)
similarities = cos_sim(user_embedding, faq_embeddings)
best_index = torch.argmax(similarities).item()
best_score = similarities[0][best_index].item()
adaptive_threshold = 0.55 if len(data) < 500 else 0.5
if best_score < adaptive_threshold :
logging.info(f"Unknown question: {user_question}")
return None
return data["answer"][best_index]