Sanity check
Browse files
app.py
CHANGED
@@ -173,7 +173,33 @@ class EmbeddingRetriever:
|
|
173 |
retriever = EmbeddingRetriever(corpus_questions, corpus_answers, question_embeddings, embedding_model, cross_encoder)
|
174 |
|
175 |
###############################################################################
|
176 |
-
# 6)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
177 |
###############################################################################
|
178 |
class AnswerExpander:
|
179 |
def __init__(self, llm: GeminiLLM):
|
@@ -183,14 +209,15 @@ class AnswerExpander:
|
|
183 |
try:
|
184 |
reference_block = "\n".join(f"- {idx+1}) {ans}" for idx, ans in enumerate(retrieved_answers, start=1))
|
185 |
prompt = (
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
|
|
194 |
logger.debug(f"Generated prompt for answer expansion: {prompt}")
|
195 |
response = self.llm._call(prompt)
|
196 |
logger.debug(f"Expanded answer: {response}")
|
@@ -203,16 +230,31 @@ class AnswerExpander:
|
|
203 |
answer_expander = AnswerExpander(llm)
|
204 |
|
205 |
###############################################################################
|
206 |
-
#
|
207 |
###############################################################################
|
208 |
def handle_query(query: str) -> str:
|
209 |
if not query or not isinstance(query, str) or len(query.strip()) == 0:
|
210 |
return "Please provide a valid question."
|
211 |
|
212 |
try:
|
|
|
|
|
|
|
|
|
|
|
|
|
213 |
retrieved = retriever.retrieve(query)
|
214 |
if not retrieved:
|
215 |
return "I'm sorry, I couldn't find an answer to your question."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
216 |
responses = [ans[0] for ans in retrieved]
|
217 |
expanded_answer = answer_expander.expand(query, responses)
|
218 |
return expanded_answer
|
@@ -222,7 +264,7 @@ def handle_query(query: str) -> str:
|
|
222 |
return "An error occurred while processing your request."
|
223 |
|
224 |
###############################################################################
|
225 |
-
#
|
226 |
###############################################################################
|
227 |
def gradio_interface(query: str):
|
228 |
try:
|
@@ -260,7 +302,7 @@ interface = gr.Interface(
|
|
260 |
)
|
261 |
|
262 |
###############################################################################
|
263 |
-
#
|
264 |
###############################################################################
|
265 |
if __name__ == "__main__":
|
266 |
try:
|
|
|
173 |
retriever = EmbeddingRetriever(corpus_questions, corpus_answers, question_embeddings, embedding_model, cross_encoder)
|
174 |
|
175 |
###############################################################################
|
176 |
+
# 6) Sanity Check Tool
|
177 |
+
###############################################################################
|
178 |
+
class QuestionSanityChecker:
|
179 |
+
def __init__(self, llm: GeminiLLM):
|
180 |
+
self.llm = llm
|
181 |
+
|
182 |
+
def is_relevant(self, question: str) -> bool:
|
183 |
+
prompt = (
|
184 |
+
f"You are an assistant that determines whether a question is relevant to daily wellness.\n\n"
|
185 |
+
f"Question: {question}\n\n"
|
186 |
+
f"Is the above question relevant to daily wellness? Respond with 'Yes' or 'No' only."
|
187 |
+
)
|
188 |
+
try:
|
189 |
+
response = self.llm._call(prompt)
|
190 |
+
is_yes = 'yes' in response.lower()
|
191 |
+
logger.debug(f"Sanity check response: {response}, interpreted as {is_yes}")
|
192 |
+
return is_yes
|
193 |
+
except Exception as e:
|
194 |
+
logger.error(f"Error in sanity check: {e}")
|
195 |
+
logger.debug("Exception details:", exc_info=True)
|
196 |
+
return False
|
197 |
+
|
198 |
+
# Instantiate the sanity checker globally
|
199 |
+
sanity_checker = QuestionSanityChecker(llm)
|
200 |
+
|
201 |
+
###############################################################################
|
202 |
+
# 7) Answer Expansion
|
203 |
###############################################################################
|
204 |
class AnswerExpander:
|
205 |
def __init__(self, llm: GeminiLLM):
|
|
|
209 |
try:
|
210 |
reference_block = "\n".join(f"- {idx+1}) {ans}" for idx, ans in enumerate(retrieved_answers, start=1))
|
211 |
prompt = (
|
212 |
+
f"You are Daily Wellness AI, a friendly wellness expert. Below are multiple "
|
213 |
+
f"potential answers retrieved from a local knowledge base. You have a user question.\n\n"
|
214 |
+
f"Question: {query}\n\n"
|
215 |
+
f"Retrieved Answers:\n{reference_block}\n\n"
|
216 |
+
"Please synthesize these references into a single cohesive, creative, and brand-aligned response. "
|
217 |
+
"Add practical tips and positivity, and end with a short inspirational note. "
|
218 |
+
"Please provide a concise response in no more than 4 sentences.\n\n"
|
219 |
+
"Disclaimer: This is general wellness information, not a substitute for professional medical advice."
|
220 |
+
)
|
221 |
logger.debug(f"Generated prompt for answer expansion: {prompt}")
|
222 |
response = self.llm._call(prompt)
|
223 |
logger.debug(f"Expanded answer: {response}")
|
|
|
230 |
answer_expander = AnswerExpander(llm)
|
231 |
|
232 |
###############################################################################
|
233 |
+
# 8) Query Handling
|
234 |
###############################################################################
|
235 |
def handle_query(query: str) -> str:
|
236 |
if not query or not isinstance(query, str) or len(query.strip()) == 0:
|
237 |
return "Please provide a valid question."
|
238 |
|
239 |
try:
|
240 |
+
# Sanity Check: Determine if the question is relevant to daily wellness
|
241 |
+
is_relevant = sanity_checker.is_relevant(query)
|
242 |
+
if not is_relevant:
|
243 |
+
return "Your question seems out of context or not related to daily wellness. Please ask a wellness-related question."
|
244 |
+
|
245 |
+
# Proceed with retrieval
|
246 |
retrieved = retriever.retrieve(query)
|
247 |
if not retrieved:
|
248 |
return "I'm sorry, I couldn't find an answer to your question."
|
249 |
+
|
250 |
+
# Optional: Check similarity threshold (if still desired)
|
251 |
+
top_score = retrieved[0][1] # Assuming the list is sorted descending
|
252 |
+
similarity_threshold = 0.3 # Adjust this threshold based on empirical results
|
253 |
+
|
254 |
+
if top_score < similarity_threshold:
|
255 |
+
return "I'm sorry, I didn't understand your question. Could you please rephrase it?"
|
256 |
+
|
257 |
+
# Proceed with answer expansion
|
258 |
responses = [ans[0] for ans in retrieved]
|
259 |
expanded_answer = answer_expander.expand(query, responses)
|
260 |
return expanded_answer
|
|
|
264 |
return "An error occurred while processing your request."
|
265 |
|
266 |
###############################################################################
|
267 |
+
# 9) Gradio Interface
|
268 |
###############################################################################
|
269 |
def gradio_interface(query: str):
|
270 |
try:
|
|
|
302 |
)
|
303 |
|
304 |
###############################################################################
|
305 |
+
# 10) Launch Gradio
|
306 |
###############################################################################
|
307 |
if __name__ == "__main__":
|
308 |
try:
|