Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -23,7 +23,7 @@ JSON_SOURCE = "database.json"
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
-
app = FastAPI(title="Hebrew RAG Chatbot", version="6.
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -37,21 +37,19 @@ model = None
|
|
| 37 |
tokenizer = None
|
| 38 |
device = "cpu"
|
| 39 |
|
| 40 |
-
# --- מילון מונחים לחיפוש חכם ---
|
| 41 |
-
# המ
|
| 42 |
-
|
| 43 |
-
"בקר": ["ESP32", "Controller"
|
| 44 |
-
"מסך": ["Nextion", "HMI", "
|
| 45 |
-
"
|
| 46 |
-
"
|
| 47 |
-
"חיבור": ["Connect", "
|
| 48 |
-
"
|
| 49 |
-
"
|
| 50 |
-
"
|
| 51 |
-
"
|
| 52 |
-
"
|
| 53 |
-
"לד": ["LED", "Light"],
|
| 54 |
-
"ממסר": ["Relay", "Switch"]
|
| 55 |
}
|
| 56 |
|
| 57 |
def init_db():
|
|
@@ -78,31 +76,20 @@ def init_db():
|
|
| 78 |
conn.close()
|
| 79 |
|
| 80 |
def expand_query(query: str) -> List[str]:
|
| 81 |
-
"""מפרק את השאילתה
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
words = clean_q.split()
|
| 85 |
-
search_terms = set()
|
| 86 |
|
| 87 |
for w in words:
|
| 88 |
-
#
|
| 89 |
-
if len(w) > 3 and w.startswith('ה')
|
| 90 |
-
|
| 91 |
-
else:
|
| 92 |
-
w_clean = w
|
| 93 |
-
|
| 94 |
-
# סינון מילים קצרות מדי אלא אם הן מכילות מספר (כמו D5)
|
| 95 |
-
if len(w_clean) < 2 and not any(char.isdigit() for char in w_clean):
|
| 96 |
-
continue
|
| 97 |
-
|
| 98 |
-
search_terms.add(w_clean)
|
| 99 |
|
| 100 |
-
# הוספת מי
|
| 101 |
-
for
|
| 102 |
-
if
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
return list(search_terms)
|
| 107 |
|
| 108 |
def query_knowledge_base(query: str) -> str:
|
|
@@ -110,24 +97,22 @@ def query_knowledge_base(query: str) -> str:
|
|
| 110 |
cursor = conn.cursor()
|
| 111 |
|
| 112 |
search_terms = expand_query(query)
|
| 113 |
-
logger.info(f"🔍 Searching for: {search_terms}")
|
| 114 |
|
| 115 |
results = []
|
| 116 |
seen_ids = set()
|
| 117 |
|
| 118 |
for term in search_terms:
|
| 119 |
-
#
|
| 120 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 121 |
rows = cursor.fetchall()
|
| 122 |
-
for row_id, content,
|
| 123 |
if row_id not in seen_ids:
|
| 124 |
score = 1
|
| 125 |
-
# בו
|
| 126 |
-
if term.lower() in
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
if any(char.isdigit() for char in term) and term in content:
|
| 130 |
-
score += 10
|
| 131 |
|
| 132 |
results.append((score, content))
|
| 133 |
seen_ids.add(row_id)
|
|
@@ -136,7 +121,8 @@ def query_knowledge_base(query: str) -> str:
|
|
| 136 |
|
| 137 |
# מיון לפי רלוונטיות
|
| 138 |
results.sort(key=lambda x: x[0], reverse=True)
|
| 139 |
-
|
|
|
|
| 140 |
|
| 141 |
return "\n---\n".join(final_content)
|
| 142 |
|
|
@@ -200,18 +186,17 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 200 |
|
| 201 |
if context:
|
| 202 |
sys_msg = (
|
| 203 |
-
"System: You are a
|
| 204 |
-
"
|
| 205 |
"1. Answer ONLY in Hebrew.\n"
|
| 206 |
-
"2. Use the Context below
|
| 207 |
-
"3.
|
| 208 |
-
"4. If asked about
|
| 209 |
f"\nContext:\n{context}"
|
| 210 |
)
|
| 211 |
else:
|
| 212 |
sys_msg = "System: You are a helpful assistant. Answer strictly in Hebrew. If you don't know, say so politely."
|
| 213 |
|
| 214 |
-
# Force system message update
|
| 215 |
if request.messages[0].role == "system":
|
| 216 |
request.messages[0].content = sys_msg
|
| 217 |
else:
|
|
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
+
app = FastAPI(title="Hebrew RAG Chatbot", version="6.1.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 37 |
tokenizer = None
|
| 38 |
device = "cpu"
|
| 39 |
|
| 40 |
+
# --- מילון מונחים לחיפוש חכם (מורחב) ---
|
| 41 |
+
# המפתחות הם מילים שהמשתמש עשוי לכתוב, הערכים הם מילים שיש לחפש בדאטה-בייס
|
| 42 |
+
SEARCH_MAP = {
|
| 43 |
+
"בקר": ["ESP32", "Controller"],
|
| 44 |
+
"מסך": ["Nextion", "HMI", "LCD"],
|
| 45 |
+
"שרטוט": ["Schematic", "Connections", "Signal", "Net"],
|
| 46 |
+
"סכמה": ["Schematic", "Connections"],
|
| 47 |
+
"חיבור": ["Connect", "Pin", "Net"],
|
| 48 |
+
"פין": ["Pin", "D", "GPIO"],
|
| 49 |
+
"זרם": ["Current", "ACS712"],
|
| 50 |
+
"לד": ["LED"],
|
| 51 |
+
"ממסר": ["Relay"],
|
| 52 |
+
"ספק": ["Power", "5V", "3.3V"]
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
def init_db():
|
|
|
|
| 76 |
conn.close()
|
| 77 |
|
| 78 |
def expand_query(query: str) -> List[str]:
|
| 79 |
+
"""מפרק את השאילתה ומוסיף מונחים באנגלית רלוונטיים"""
|
| 80 |
+
words = re.sub(r'[^\w\s]', '', query).split()
|
| 81 |
+
search_terms = set(words) # מתחילים עם המילים המקוריות
|
|
|
|
|
|
|
| 82 |
|
| 83 |
for w in words:
|
| 84 |
+
# טיפול בה' הידיעה
|
| 85 |
+
root_word = w[1:] if len(w) > 3 and w.startswith('ה') else w
|
| 86 |
+
search_terms.add(root_word)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
# הוספת תרגומים מהמילון
|
| 89 |
+
for key, values in SEARCH_MAP.items():
|
| 90 |
+
if key in root_word: # אם המילה "שרטוט" נמצאת בשאילתה
|
| 91 |
+
search_terms.update(values) # הוסף "Schematic", "Connections" וכו'
|
| 92 |
+
|
|
|
|
| 93 |
return list(search_terms)
|
| 94 |
|
| 95 |
def query_knowledge_base(query: str) -> str:
|
|
|
|
| 97 |
cursor = conn.cursor()
|
| 98 |
|
| 99 |
search_terms = expand_query(query)
|
| 100 |
+
logger.info(f"🔍 Searching for terms: {search_terms}")
|
| 101 |
|
| 102 |
results = []
|
| 103 |
seen_ids = set()
|
| 104 |
|
| 105 |
for term in search_terms:
|
| 106 |
+
# מחפש גם בתוכן וגם בכותרת
|
| 107 |
cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
|
| 108 |
rows = cursor.fetchall()
|
| 109 |
+
for row_id, content, topic in rows:
|
| 110 |
if row_id not in seen_ids:
|
| 111 |
score = 1
|
| 112 |
+
# ציון גבוה יותר אם המונח נמצא בכותרת
|
| 113 |
+
if term.lower() in topic.lower(): score += 5
|
| 114 |
+
# ציון גבוה מאוד אם המונח הוא מספר (למשל "18") ונמצא בתוכן
|
| 115 |
+
if term.isdigit() and term in content: score += 10
|
|
|
|
|
|
|
| 116 |
|
| 117 |
results.append((score, content))
|
| 118 |
seen_ids.add(row_id)
|
|
|
|
| 121 |
|
| 122 |
# מיון לפי רלוונטיות
|
| 123 |
results.sort(key=lambda x: x[0], reverse=True)
|
| 124 |
+
# מחזיר את 2 התוצאות הכי טובות
|
| 125 |
+
final_content = [r[1] for r in results[:2]]
|
| 126 |
|
| 127 |
return "\n---\n".join(final_content)
|
| 128 |
|
|
|
|
| 186 |
|
| 187 |
if context:
|
| 188 |
sys_msg = (
|
| 189 |
+
"System: You are a technical assistant for the 'Telem Smart Home' project.\n"
|
| 190 |
+
"INSTRUCTIONS:\n"
|
| 191 |
"1. Answer ONLY in Hebrew.\n"
|
| 192 |
+
"2. Use the Context below to answer. It contains schematic data in English (e.g., 'U2 Pin 5 connects to...').\n"
|
| 193 |
+
"3. TRANSLATE the technical details from English to Hebrew for the user.\n"
|
| 194 |
+
"4. If asked about 'Schematic' (שרטוט), summarize the connections found in the context.\n"
|
| 195 |
f"\nContext:\n{context}"
|
| 196 |
)
|
| 197 |
else:
|
| 198 |
sys_msg = "System: You are a helpful assistant. Answer strictly in Hebrew. If you don't know, say so politely."
|
| 199 |
|
|
|
|
| 200 |
if request.messages[0].role == "system":
|
| 201 |
request.messages[0].content = sys_msg
|
| 202 |
else:
|