Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ import json
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
import uuid
|
| 6 |
from typing import Dict
|
| 7 |
-
|
| 8 |
from config import OPENAI_API_KEY, DB_PATH, EMBED_MODEL
|
| 9 |
from utils import get_embedding, cosine_similarity, find_top_k_matches
|
| 10 |
from scraper import scrape_workshops_from_squarespace
|
|
@@ -24,7 +24,7 @@ from database import (
|
|
| 24 |
if not OPENAI_API_KEY:
|
| 25 |
raise ValueError("OPENAI_API_KEY not found in .env file")
|
| 26 |
|
| 27 |
-
openai.api_key =
|
| 28 |
|
| 29 |
|
| 30 |
# Store session ID for the conversation
|
|
@@ -530,8 +530,8 @@ def process_question(question: str, current_session_id: str):
|
|
| 530 |
top_faqs.append((score, entry_id, question_text, answer_text))
|
| 531 |
top_faqs.sort(reverse=True)
|
| 532 |
|
| 533 |
-
faq_threshold = 0.85
|
| 534 |
-
ambiguous_threshold = 0.70
|
| 535 |
|
| 536 |
# If high-confidence FAQ match found
|
| 537 |
if top_faqs and top_faqs[0][0] >= faq_threshold:
|
|
@@ -569,17 +569,26 @@ def process_question(question: str, current_session_id: str):
|
|
| 569 |
elif top_faqs and top_faqs[0][0] >= ambiguous_threshold:
|
| 570 |
# AMBIGUOUS ZONE
|
| 571 |
needs_clarification = False
|
|
|
|
| 572 |
|
| 573 |
-
if
|
| 574 |
-
|
| 575 |
-
|
| 576 |
-
is_generic_query = any(w in question.lower() for w in ['price', 'cost', 'how much', 'schedule', 'when'])
|
| 577 |
-
if is_generic_query and not current_topic:
|
| 578 |
-
needs_clarification = True
|
| 579 |
-
|
| 580 |
-
clarification_count = session_state.get('clarification_count', 0)
|
| 581 |
-
if clarification_count > 0:
|
| 582 |
needs_clarification = False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 583 |
|
| 584 |
if needs_clarification:
|
| 585 |
update_session_state(current_session_id, increment_clarification=True, increment_count=False)
|
|
|
|
| 4 |
from datetime import datetime, timedelta
|
| 5 |
import uuid
|
| 6 |
from typing import Dict
|
| 7 |
+
|
| 8 |
from config import OPENAI_API_KEY, DB_PATH, EMBED_MODEL
|
| 9 |
from utils import get_embedding, cosine_similarity, find_top_k_matches
|
| 10 |
from scraper import scrape_workshops_from_squarespace
|
|
|
|
| 24 |
if not OPENAI_API_KEY:
|
| 25 |
raise ValueError("OPENAI_API_KEY not found in .env file")
|
| 26 |
|
| 27 |
+
openai.api_key = OPENAI_API_KEY
|
| 28 |
|
| 29 |
|
| 30 |
# Store session ID for the conversation
|
|
|
|
| 530 |
top_faqs.append((score, entry_id, question_text, answer_text))
|
| 531 |
top_faqs.sort(reverse=True)
|
| 532 |
|
| 533 |
+
faq_threshold = 0.72 # Lowered from 0.85 to capture direct matches better
|
| 534 |
+
ambiguous_threshold = 0.60 # Lowered from 0.70
|
| 535 |
|
| 536 |
# If high-confidence FAQ match found
|
| 537 |
if top_faqs and top_faqs[0][0] >= faq_threshold:
|
|
|
|
| 569 |
elif top_faqs and top_faqs[0][0] >= ambiguous_threshold:
|
| 570 |
# AMBIGUOUS ZONE
|
| 571 |
needs_clarification = False
|
| 572 |
+
best_match_q = top_faqs[0][2]
|
| 573 |
|
| 574 |
+
# 1. Never clarify if the best match question is identical to the user question
|
| 575 |
+
if question.lower().strip('?') == best_match_q.lower().strip('?'):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 576 |
needs_clarification = False
|
| 577 |
+
else:
|
| 578 |
+
# 2. Check Format logic (only if locational)
|
| 579 |
+
is_locational = any(w in question.lower() for w in ['online', 'studio', 'person', 'atlanta', 'location', 'where'])
|
| 580 |
+
if is_locational and not user_preference:
|
| 581 |
+
needs_clarification = True
|
| 582 |
+
|
| 583 |
+
# 3. Check Topic logic (only if generic)
|
| 584 |
+
is_generic_query = any(w in question.lower() for w in ['price', 'cost', 'how much', 'schedule', 'when'])
|
| 585 |
+
if is_generic_query and not current_topic:
|
| 586 |
+
needs_clarification = True
|
| 587 |
+
|
| 588 |
+
# 4. Force resolve if already asked once
|
| 589 |
+
clarification_count = session_state.get('clarification_count', 0)
|
| 590 |
+
if clarification_count > 0:
|
| 591 |
+
needs_clarification = False
|
| 592 |
|
| 593 |
if needs_clarification:
|
| 594 |
update_session_state(current_session_id, increment_clarification=True, increment_count=False)
|