Spaces:
Sleeping
Sleeping
update relevance check logic
Browse files- rag_app/rag_2.py +3 -3
rag_app/rag_2.py
CHANGED
@@ -81,13 +81,13 @@ def is_harmful(query):
|
|
81 |
return any(keyword in query.lower() for keyword in harmful_keywords)
|
82 |
|
83 |
|
84 |
-
def
|
85 |
retriever = index.as_retriever(similarity_top_k=1)
|
86 |
nodes = retriever.retrieve(query)
|
87 |
if not nodes:
|
88 |
return False
|
89 |
similarity = nodes[0].score
|
90 |
-
return
|
91 |
|
92 |
|
93 |
def get_sequence_probability(llm, input_sequence):
|
@@ -113,7 +113,7 @@ def answer_question(query):
|
|
113 |
storage_context = StorageContext.from_defaults(persist_dir="models/precomputed_index")
|
114 |
index = load_index_from_storage(storage_context)
|
115 |
|
116 |
-
if
|
117 |
return "This query doesn't appear relevant to finance."
|
118 |
|
119 |
retriever = QueryFusionRetriever(
|
|
|
81 |
return any(keyword in query.lower() for keyword in harmful_keywords)
|
82 |
|
83 |
|
84 |
+
def is_not_relevant(query, index, threshold=0.7):
|
85 |
retriever = index.as_retriever(similarity_top_k=1)
|
86 |
nodes = retriever.retrieve(query)
|
87 |
if not nodes:
|
88 |
return False
|
89 |
similarity = nodes[0].score
|
90 |
+
return similarity <= threshold
|
91 |
|
92 |
|
93 |
def get_sequence_probability(llm, input_sequence):
|
|
|
113 |
storage_context = StorageContext.from_defaults(persist_dir="models/precomputed_index")
|
114 |
index = load_index_from_storage(storage_context)
|
115 |
|
116 |
+
if is_not_relevant(query, index, 0.2):
|
117 |
return "This query doesn't appear relevant to finance."
|
118 |
|
119 |
retriever = QueryFusionRetriever(
|