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="
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
@@ -40,13 +40,18 @@ device = "cpu"
|
|
| 40 |
# --- מילון מונחים לחיפוש חכם ---
|
| 41 |
# המודל יחפש גם את המילה וגם את התרגום הטכני שלה
|
| 42 |
SEARCH_SYNONYMS = {
|
| 43 |
-
"בקר": ["ESP32", "Controller", "MCU"],
|
| 44 |
-
"מסך": ["Nextion", "HMI", "Screen", "Display"],
|
| 45 |
-
"זרם": ["Current", "ACS712", "Amps"],
|
| 46 |
"פין": ["Pin", "GPIO", "D"],
|
| 47 |
-
"חיבור": ["Connect", "Wire", "Signal"],
|
| 48 |
-
"ספק": ["Power", "Supply", "5V", "3.3V"],
|
| 49 |
-
"תקשורת": ["UART", "RX", "TX", "Baud"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 50 |
}
|
| 51 |
|
| 52 |
def init_db():
|
|
@@ -74,19 +79,27 @@ def init_db():
|
|
| 74 |
|
| 75 |
def expand_query(query: str) -> List[str]:
|
| 76 |
"""מפרק את השאילתה למילות מפתח ומוסיף מילים נרדפות טכניות"""
|
| 77 |
-
|
|
|
|
|
|
|
| 78 |
search_terms = set()
|
| 79 |
|
| 80 |
for w in words:
|
| 81 |
-
# ת
|
| 82 |
-
if len(w)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
continue
|
| 84 |
|
| 85 |
-
search_terms.add(
|
| 86 |
|
| 87 |
# הוספת מילים נרדפות
|
| 88 |
for hebrew_key, english_vals in SEARCH_SYNONYMS.items():
|
| 89 |
-
if hebrew_key in
|
| 90 |
for val in english_vals:
|
| 91 |
search_terms.add(val)
|
| 92 |
|
|
@@ -103,22 +116,25 @@ def query_knowledge_base(query: str) -> str:
|
|
| 103 |
seen_ids = set()
|
| 104 |
|
| 105 |
for term in search_terms:
|
| 106 |
-
# ח
|
| 107 |
-
cursor.execute("SELECT id, content FROM knowledge WHERE content LIKE ?", (f'%{term}%',))
|
| 108 |
rows = cursor.fetchall()
|
| 109 |
-
for row_id, content in rows:
|
| 110 |
if row_id not in seen_ids:
|
| 111 |
-
# בונוס לתוצאות שמכילות מספרים אם השאילתה הכילה מספר
|
| 112 |
score = 1
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
if any(char.isdigit() for char in term) and term in content:
|
| 114 |
-
score = 10
|
| 115 |
|
| 116 |
results.append((score, content))
|
| 117 |
seen_ids.add(row_id)
|
| 118 |
|
| 119 |
conn.close()
|
| 120 |
|
| 121 |
-
# מיון לפי רלוונטיות
|
| 122 |
results.sort(key=lambda x: x[0], reverse=True)
|
| 123 |
final_content = [r[1] for r in results[:3]] # לוקח עד 3 תוצאות
|
| 124 |
|
|
@@ -187,8 +203,9 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 187 |
"System: You are a specific assistant for the 'Telem Smart Home' project.\n"
|
| 188 |
"Instructions:\n"
|
| 189 |
"1. Answer ONLY in Hebrew.\n"
|
| 190 |
-
"2. Use the Context below accurately. It contains schematic connections.\n"
|
| 191 |
-
"3.
|
|
|
|
| 192 |
f"\nContext:\n{context}"
|
| 193 |
)
|
| 194 |
else:
|
|
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
+
app = FastAPI(title="Hebrew RAG Chatbot", version="6.0.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 40 |
# --- מילון מונחים לחיפוש חכם ---
|
| 41 |
# המודל יחפש גם את המילה וגם את התרגום הטכני שלה
|
| 42 |
SEARCH_SYNONYMS = {
|
| 43 |
+
"בקר": ["ESP32", "Controller", "MCU", "DevKit"],
|
| 44 |
+
"מסך": ["Nextion", "HMI", "Screen", "Display", "LCD"],
|
| 45 |
+
"זרם": ["Current", "ACS712", "Amps", "Sensor"],
|
| 46 |
"פין": ["Pin", "GPIO", "D"],
|
| 47 |
+
"חיבור": ["Connect", "Wire", "Signal", "Net"],
|
| 48 |
+
"ספק": ["Power", "Supply", "5V", "3.3V", "VIN"],
|
| 49 |
+
"תקשורת": ["UART", "RX", "TX", "Baud"],
|
| 50 |
+
"שרטוט": ["Schematic", "Diagram", "Circuit", "Connections", "Logic"], # הוספתי את זה
|
| 51 |
+
"תרשים": ["Schematic", "Diagram"],
|
| 52 |
+
"רכיב": ["Component", "Part", "U"],
|
| 53 |
+
"לד": ["LED", "Light"],
|
| 54 |
+
"ממסר": ["Relay", "Switch"]
|
| 55 |
}
|
| 56 |
|
| 57 |
def init_db():
|
|
|
|
| 79 |
|
| 80 |
def expand_query(query: str) -> List[str]:
|
| 81 |
"""מפרק את השאילתה למילות מפתח ומוסיף מילים נרדפות טכניות"""
|
| 82 |
+
# ניקוי סימני פיסוק
|
| 83 |
+
clean_q = re.sub(r'[^\w\s]', '', query)
|
| 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 |
+
w_clean = w[1:]
|
| 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 hebrew_key, english_vals in SEARCH_SYNONYMS.items():
|
| 102 |
+
if hebrew_key in w_clean: # בודק הכלה (למשל "בשרטוט" מכיל "שרטוט")
|
| 103 |
for val in english_vals:
|
| 104 |
search_terms.add(val)
|
| 105 |
|
|
|
|
| 116 |
seen_ids = set()
|
| 117 |
|
| 118 |
for term in search_terms:
|
| 119 |
+
# FIX: מחפש עכשיו גם בכותרת (Topic) וגם בתוכן (Content)
|
| 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, topic_name in rows:
|
| 123 |
if row_id not in seen_ids:
|
|
|
|
| 124 |
score = 1
|
| 125 |
+
# בונוס לתוצאות בכותרת
|
| 126 |
+
if term.lower() in topic_name.lower():
|
| 127 |
+
score += 5
|
| 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)
|
| 134 |
|
| 135 |
conn.close()
|
| 136 |
|
| 137 |
+
# מיון לפי רלוונטיות
|
| 138 |
results.sort(key=lambda x: x[0], reverse=True)
|
| 139 |
final_content = [r[1] for r in results[:3]] # לוקח עד 3 תוצאות
|
| 140 |
|
|
|
|
| 203 |
"System: You are a specific assistant for the 'Telem Smart Home' project.\n"
|
| 204 |
"Instructions:\n"
|
| 205 |
"1. Answer ONLY in Hebrew.\n"
|
| 206 |
+
"2. Use the Context below accurately. It contains schematic connections from EasyEDA.\n"
|
| 207 |
+
"3. 'U2' is usually the ESP32. 'U3' is the main board. 'U4' is a Logic Converter.\n"
|
| 208 |
+
"4. If asked about a Pin, state exactly what component and pin it connects to.\n"
|
| 209 |
f"\nContext:\n{context}"
|
| 210 |
)
|
| 211 |
else:
|