Spaces:
Running
Running
Commit
·
0d1bcfb
1
Parent(s):
e229be8
Updated code with greeting support
Browse files- src/__pycache__/main.cpython-312.pyc +0 -0
- src/agents/__pycache__/system_instructions_rag.cpython-312.pyc +0 -0
- src/agents/system_instructions_rag.py +15 -0
- src/db/__pycache__/mongodb_store.cpython-312.pyc +0 -0
- src/models/__pycache__/rag.cpython-312.pyc +0 -0
- src/utils/__pycache__/document_processor.cpython-312.pyc +0 -0
- src/utils/greeting_handler.py +145 -0
src/__pycache__/main.cpython-312.pyc
CHANGED
Binary files a/src/__pycache__/main.cpython-312.pyc and b/src/__pycache__/main.cpython-312.pyc differ
|
|
src/agents/__pycache__/system_instructions_rag.cpython-312.pyc
CHANGED
Binary files a/src/agents/__pycache__/system_instructions_rag.cpython-312.pyc and b/src/agents/__pycache__/system_instructions_rag.cpython-312.pyc differ
|
|
src/agents/system_instructions_rag.py
CHANGED
@@ -3,6 +3,7 @@ from typing import List, Dict, Optional
|
|
3 |
from src.agents.rag_agent import RAGResponse
|
4 |
from src.utils.logger import logger
|
5 |
from src.agents.rag_agent import RAGAgent
|
|
|
6 |
|
7 |
|
8 |
class SystemInstructionsRAGAgent(RAGAgent):
|
@@ -35,6 +36,20 @@ class SystemInstructionsRAGAgent(RAGAgent):
|
|
35 |
scores=None
|
36 |
)
|
37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
38 |
# Get conversation history if conversation_id exists
|
39 |
conversation_history = []
|
40 |
if conversation_id:
|
|
|
3 |
from src.agents.rag_agent import RAGResponse
|
4 |
from src.utils.logger import logger
|
5 |
from src.agents.rag_agent import RAGAgent
|
6 |
+
from src.utils.greeting_handler import GreetingHandler
|
7 |
|
8 |
|
9 |
class SystemInstructionsRAGAgent(RAGAgent):
|
|
|
36 |
scores=None
|
37 |
)
|
38 |
|
39 |
+
# Check if this is a greeting (using the singleton GreetingHandler)
|
40 |
+
greeting_handler = GreetingHandler()
|
41 |
+
is_greeting, greeting_response = greeting_handler.is_greeting(
|
42 |
+
query)
|
43 |
+
|
44 |
+
if is_greeting:
|
45 |
+
logger.info(f"Detected greeting in query: {query}")
|
46 |
+
return RAGResponse(
|
47 |
+
response=greeting_response,
|
48 |
+
context_docs=[],
|
49 |
+
sources=[],
|
50 |
+
scores=None
|
51 |
+
)
|
52 |
+
|
53 |
# Get conversation history if conversation_id exists
|
54 |
conversation_history = []
|
55 |
if conversation_id:
|
src/db/__pycache__/mongodb_store.cpython-312.pyc
CHANGED
Binary files a/src/db/__pycache__/mongodb_store.cpython-312.pyc and b/src/db/__pycache__/mongodb_store.cpython-312.pyc differ
|
|
src/models/__pycache__/rag.cpython-312.pyc
CHANGED
Binary files a/src/models/__pycache__/rag.cpython-312.pyc and b/src/models/__pycache__/rag.cpython-312.pyc differ
|
|
src/utils/__pycache__/document_processor.cpython-312.pyc
CHANGED
Binary files a/src/utils/__pycache__/document_processor.cpython-312.pyc and b/src/utils/__pycache__/document_processor.cpython-312.pyc differ
|
|
src/utils/greeting_handler.py
ADDED
@@ -0,0 +1,145 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# src/utils/greeting_handler.py
|
2 |
+
import re
|
3 |
+
import random
|
4 |
+
from typing import Tuple, Optional
|
5 |
+
|
6 |
+
|
7 |
+
class GreetingHandler:
|
8 |
+
"""Handler for detecting and responding to greetings"""
|
9 |
+
|
10 |
+
# Singleton instance
|
11 |
+
_instance = None
|
12 |
+
|
13 |
+
def __new__(cls):
|
14 |
+
if cls._instance is None:
|
15 |
+
cls._instance = super(GreetingHandler, cls).__new__(cls)
|
16 |
+
cls._instance._initialize()
|
17 |
+
return cls._instance
|
18 |
+
|
19 |
+
def _initialize(self):
|
20 |
+
"""Initialize patterns and responses"""
|
21 |
+
# Common greeting patterns with word boundaries to match whole words
|
22 |
+
self.greeting_patterns = [
|
23 |
+
# English Greetings
|
24 |
+
r'\b(hi|hello|hey|howdy|greetings|good morning|good afternoon|good evening|good day)\b',
|
25 |
+
r'\bho+la\b', # Spanish greeting with possible repeated 'o'
|
26 |
+
r'\bhiy+a\b', # "Hiya" with possible repeated 'y'
|
27 |
+
r'\bhel+o+\b', # "Hello" with possible repeated letters
|
28 |
+
r'\bhey+\b', # "Hey" with possible repeated 'y'
|
29 |
+
r'\bhi+\b', # "Hi" with possible repeated 'i'
|
30 |
+
r'\byo+\b', # "Yo" with possible repeated 'o'
|
31 |
+
r'how are you(\s|\?|$)',
|
32 |
+
r'what\'?s up',
|
33 |
+
r'nice to (meet|see) you',
|
34 |
+
r'\bhiya\b',
|
35 |
+
r'\bsup\b',
|
36 |
+
r'\bwhat up\b',
|
37 |
+
r'\bhow\'?s it going\b',
|
38 |
+
r'\bhow\'?s everything\b',
|
39 |
+
r'\bhow\'?s life\b',
|
40 |
+
r'\bhow\'?s your day\b',
|
41 |
+
r'\bhow\'?ve you been\b',
|
42 |
+
r'\bwhat\'?s new\b',
|
43 |
+
r'\bany news\b',
|
44 |
+
r'\bhowdy partner\b',
|
45 |
+
r'\bheya\b',
|
46 |
+
r'\bhallo\b',
|
47 |
+
r'\bhey there\b',
|
48 |
+
r'\btop of the morning\b',
|
49 |
+
|
50 |
+
# International Greetings
|
51 |
+
r'\bbonjour\b', # French greeting
|
52 |
+
r'\bciao\b', # Italian greeting
|
53 |
+
r'\bnamaste\b', # Hindi greeting
|
54 |
+
r'\bsalam\b', # Arabic greeting
|
55 |
+
r'\bassalamu alaikum\b', # Islamic greeting
|
56 |
+
r'\bshalom\b', # Hebrew greeting
|
57 |
+
|
58 |
+
# Russian Greetings (Transliterated)
|
59 |
+
r'\bprivet\b', # "Privet" - Informal Hello
|
60 |
+
r'\bzdravstvuyte\b', # "Zdravstvuyte" - Formal Hello
|
61 |
+
r'\bdobroe utro\b', # "Dobroe utro" - Good morning
|
62 |
+
r'\bdobryy den\b', # "Dobryy den" - Good afternoon
|
63 |
+
r'\bdobryy vecher\b', # "Dobryy vecher" - Good evening
|
64 |
+
r'\bkak dela\b', # "Kak dela" - How are you?
|
65 |
+
r'\bkak pozhivayesh\b', # "Kak pozhivayesh" - How have you been?
|
66 |
+
|
67 |
+
# Arabic Greetings (Transliterated)
|
68 |
+
r'\bmarhaban\b', # "Marhaban" - Hello
|
69 |
+
r'\bahlan\b', # "Ahlan" - Informal Hello
|
70 |
+
r'\bassalamu alaykum\b', # "As-salamu alaykum" - Peace be upon you
|
71 |
+
r'\bsabah al-khair\b', # "Sabah al-khair" - Good morning
|
72 |
+
r'\bmasa al-khair\b', # "Masa al-khair" - Good evening
|
73 |
+
r'\bkayfa halak\b', # "Kayfa halak" (m) - How are you?
|
74 |
+
r'\bkayfa halik\b', # "Kayfa halik" (f) - How are you?
|
75 |
+
r'\bkayfa al-umur\b', # "Kayfa al-umur" - How’s everything?
|
76 |
+
r'\bakhbarak\b', # "Akhbarak" (m) - Any news?
|
77 |
+
r'\bakhbarik\b', # "Akhbarik" (f) - Any news?
|
78 |
+
]
|
79 |
+
self.compiled_patterns = [re.compile(
|
80 |
+
pattern, re.IGNORECASE) for pattern in self.greeting_patterns]
|
81 |
+
|
82 |
+
# List of greeting responses to choose from for variety
|
83 |
+
self.greeting_responses = [
|
84 |
+
"Hello! How can I help you today?",
|
85 |
+
"Hi there! What can I assist you with?",
|
86 |
+
"Hey! How can I be of service?",
|
87 |
+
"Greetings! How may I assist you?",
|
88 |
+
"Hello! I'm here to help. What would you like to know?",
|
89 |
+
"Hi! I'm your assistant. What can I help you with?",
|
90 |
+
"Hello there! I'm ready to assist you. What's on your mind?",
|
91 |
+
"Hey! I'm here to provide information. What are you looking for?",
|
92 |
+
"Hi! How can I assist you today?"
|
93 |
+
]
|
94 |
+
|
95 |
+
# Special responses for "how are you" type greetings
|
96 |
+
self.how_are_you_responses = [
|
97 |
+
"I'm doing great, thanks for asking! How can I help you today?",
|
98 |
+
"I'm well, thank you! How may I assist you?",
|
99 |
+
"I'm fine, thanks! What can I help you with?",
|
100 |
+
"Doing well! I'm ready to help with whatever you need."
|
101 |
+
]
|
102 |
+
|
103 |
+
def is_greeting(self, query: str) -> Tuple[bool, Optional[str]]:
|
104 |
+
"""
|
105 |
+
Check if the query is a greeting
|
106 |
+
|
107 |
+
Args:
|
108 |
+
query (str): The user query
|
109 |
+
|
110 |
+
Returns:
|
111 |
+
Tuple[bool, Optional[str]]: (is_greeting, appropriate_response)
|
112 |
+
"""
|
113 |
+
# Convert to lowercase for case-insensitive matching
|
114 |
+
query_lower = query.lower().strip()
|
115 |
+
|
116 |
+
# Skip detection for longer queries (likely not just greetings)
|
117 |
+
word_count = len(query_lower.split())
|
118 |
+
if word_count > 10:
|
119 |
+
return False, None
|
120 |
+
|
121 |
+
# Check if query matches any greeting pattern
|
122 |
+
is_how_are_you = False
|
123 |
+
|
124 |
+
for pattern in self.compiled_patterns:
|
125 |
+
if re.search(pattern, query_lower):
|
126 |
+
# Special case for "how are you" type queries
|
127 |
+
if "how are you" in query_lower:
|
128 |
+
is_how_are_you = True
|
129 |
+
return True, self._get_response(is_how_are_you)
|
130 |
+
|
131 |
+
return False, None
|
132 |
+
|
133 |
+
def _get_response(self, is_how_are_you: bool = False) -> str:
|
134 |
+
"""
|
135 |
+
Get an appropriate greeting response
|
136 |
+
|
137 |
+
Args:
|
138 |
+
is_how_are_you (bool): Whether this is a "how are you" type greeting
|
139 |
+
|
140 |
+
Returns:
|
141 |
+
str: A greeting response
|
142 |
+
"""
|
143 |
+
if is_how_are_you:
|
144 |
+
return random.choice(self.how_are_you_responses)
|
145 |
+
return random.choice(self.greeting_responses)
|