Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -84,41 +84,39 @@ def expand_query(query: str) -> List[str]:
|
|
| 84 |
|
| 85 |
def simplify_schematic_text(text: str) -> str:
|
| 86 |
"""
|
| 87 |
-
מנקה את הטקסט
|
|
|
|
| 88 |
"""
|
| 89 |
-
|
|
|
|
| 90 |
text = re.sub(r'\([A-Za-z0-9_\-]+\)', '', text)
|
| 91 |
-
|
| 92 |
-
# 2. פישוט שמות פינים
|
| 93 |
-
text = text.replace("D16_or_RX2", "
|
| 94 |
-
text = text.replace("D17_or_TX2", "
|
| 95 |
-
text = text.replace("
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
# 3. החלפות שמות לרכיבים בעברית
|
| 99 |
replacements = {
|
| 100 |
"ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO": "בקר ESP32",
|
| 101 |
-
"TLM-SH_board": "לוח ראשי",
|
| 102 |
"LevelConvertor": "ממיר רמות",
|
| 103 |
"NEXTION": "מסך",
|
| 104 |
-
"LCD1": "מסך",
|
| 105 |
"HMI": "מסך",
|
|
|
|
| 106 |
"StudentBoard": "לוח תלמיד",
|
| 107 |
-
"connects to": "מחובר אל",
|
| 108 |
"Pin": "פין"
|
| 109 |
}
|
| 110 |
|
| 111 |
for eng, heb in replacements.items():
|
| 112 |
text = text.replace(eng, heb)
|
| 113 |
|
| 114 |
-
# 4. מחיקת
|
| 115 |
text = re.sub(r'\b[UJ]\d+\b', '', text)
|
| 116 |
|
| 117 |
-
# 5. ניקוי כפילויות
|
| 118 |
text = text.replace("Pin Pin", "פין")
|
| 119 |
text = text.replace("פין פין", "פין")
|
| 120 |
-
|
| 121 |
-
# 6. ניקוי רווחים
|
| 122 |
text = re.sub(r'\s+', ' ', text).strip()
|
| 123 |
|
| 124 |
return text
|
|
@@ -139,7 +137,8 @@ def query_knowledge_base(query: str) -> str:
|
|
| 139 |
for row_id, content, topic in rows:
|
| 140 |
if row_id not in seen_ids:
|
| 141 |
score = 1
|
| 142 |
-
|
|
|
|
| 143 |
score += 100
|
| 144 |
elif term.lower() in topic.lower():
|
| 145 |
score += 10
|
|
@@ -156,18 +155,25 @@ def query_knowledge_base(query: str) -> str:
|
|
| 156 |
if results:
|
| 157 |
raw_content = results[0][1]
|
| 158 |
|
|
|
|
| 159 |
if "Schematic" in raw_content:
|
| 160 |
lines = raw_content.split('\n')
|
| 161 |
relevant_lines = []
|
|
|
|
|
|
|
| 162 |
target_terms = [t.lower() for t in search_terms if len(t) > 1]
|
|
|
|
|
|
|
|
|
|
| 163 |
for line in lines:
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
clean_text = simplify_schematic_text("\n".join(relevant_lines))
|
| 170 |
-
return f"להלן רשימת
|
| 171 |
|
| 172 |
return raw_content
|
| 173 |
return ""
|
|
@@ -188,7 +194,7 @@ class Message(BaseModel):
|
|
| 188 |
class ChatCompletionRequest(BaseModel):
|
| 189 |
messages: List[Message]
|
| 190 |
max_tokens: Optional[int] = 1024
|
| 191 |
-
temperature: Optional[float] = 0.1
|
| 192 |
stream: Optional[bool] = False
|
| 193 |
|
| 194 |
@app.on_event("startup")
|
|
@@ -205,20 +211,21 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 205 |
save_message("user", last_user_msg.content)
|
| 206 |
context = query_knowledge_base(last_user_msg.content)
|
| 207 |
|
| 208 |
-
#
|
| 209 |
sys_msg = (
|
| 210 |
"תפקידך: טכנאי מומחה לפרויקט 'בית חכם תלם'.\n"
|
| 211 |
-
"הנחיות קריטיות:\n"
|
| 212 |
-
"1.
|
| 213 |
-
"2. **
|
| 214 |
-
"3. **איסור
|
| 215 |
-
"4. **
|
| 216 |
-
"5.
|
| 217 |
)
|
|
|
|
| 218 |
if context:
|
| 219 |
-
sys_msg += f"\nמידע טכני
|
| 220 |
else:
|
| 221 |
-
sys_msg += "\n(לא נמצא מידע ספציפי במערכת, ענה מהידע הכללי שלך)."
|
| 222 |
|
| 223 |
messages_payload = [msg.dict() for msg in request.messages]
|
| 224 |
|
|
|
|
| 84 |
|
| 85 |
def simplify_schematic_text(text: str) -> str:
|
| 86 |
"""
|
| 87 |
+
מנקה את הטקסט ומפרמטת אותו למבנה שהמודל לא יכול לטעות בו.
|
| 88 |
+
המטרה: למנוע מהמודל 'לחשוב' על רכיבים, אלא רק להקריא נתונים.
|
| 89 |
"""
|
| 90 |
+
|
| 91 |
+
# 1. הסרת מק"טים בסוגריים (למשל BOB_19002) - זה הגורם מס' 1 להזיות
|
| 92 |
text = re.sub(r'\([A-Za-z0-9_\-]+\)', '', text)
|
| 93 |
+
|
| 94 |
+
# 2. פישוט שמות פינים מסובכים
|
| 95 |
+
text = text.replace("D16_or_RX2", "RX2")
|
| 96 |
+
text = text.replace("D17_or_TX2", "TX2")
|
| 97 |
+
text = text.replace("3V3", "3.3V")
|
| 98 |
+
|
| 99 |
+
# 3. תרגום מונחים טכניים לעברית ברורה
|
|
|
|
| 100 |
replacements = {
|
| 101 |
"ESP32-DEVKITC-32D_ESP32-DEVKITC-30GPIO": "בקר ESP32",
|
|
|
|
| 102 |
"LevelConvertor": "ממיר רמות",
|
| 103 |
"NEXTION": "מסך",
|
|
|
|
| 104 |
"HMI": "מסך",
|
| 105 |
+
"TLM-SH_board": "לוח ראשי",
|
| 106 |
"StudentBoard": "לוח תלמיד",
|
| 107 |
+
"connects to": "--- מחובר אל --->", # חץ ויזואלי עוזר למודל להבין כיוון
|
| 108 |
"Pin": "פין"
|
| 109 |
}
|
| 110 |
|
| 111 |
for eng, heb in replacements.items():
|
| 112 |
text = text.replace(eng, heb)
|
| 113 |
|
| 114 |
+
# 4. מחיקת רעש (U1, U2, J1...)
|
| 115 |
text = re.sub(r'\b[UJ]\d+\b', '', text)
|
| 116 |
|
| 117 |
+
# 5. ניקוי כפילויות ורווחים
|
| 118 |
text = text.replace("Pin Pin", "פין")
|
| 119 |
text = text.replace("פין פין", "פין")
|
|
|
|
|
|
|
| 120 |
text = re.sub(r'\s+', ' ', text).strip()
|
| 121 |
|
| 122 |
return text
|
|
|
|
| 137 |
for row_id, content, topic in rows:
|
| 138 |
if row_id not in seen_ids:
|
| 139 |
score = 1
|
| 140 |
+
# זיהוי הקשר טכני
|
| 141 |
+
if "Schematic" in content and any(x in query for x in ["שרטוט", "חיבור", "פין", "לאן", "מחובר", "מסך", "ESP", "בקר"]):
|
| 142 |
score += 100
|
| 143 |
elif term.lower() in topic.lower():
|
| 144 |
score += 10
|
|
|
|
| 155 |
if results:
|
| 156 |
raw_content = results[0][1]
|
| 157 |
|
| 158 |
+
# טיפול מיוחד לשרטוטים
|
| 159 |
if "Schematic" in raw_content:
|
| 160 |
lines = raw_content.split('\n')
|
| 161 |
relevant_lines = []
|
| 162 |
+
|
| 163 |
+
# מילות מפתח מורחבות כדי לתפוס את כל השרשרת (למשל גם את הממיר, גם את המסך וגם את הבקר)
|
| 164 |
target_terms = [t.lower() for t in search_terms if len(t) > 1]
|
| 165 |
+
if "מסך" in query or "nextion" in query.lower():
|
| 166 |
+
target_terms.extend(["hmi", "convert", "level", "esp"])
|
| 167 |
+
|
| 168 |
for line in lines:
|
| 169 |
+
line_lower = line.lower()
|
| 170 |
+
if "connect" in line_lower:
|
| 171 |
+
# בדיקה אם השורה מכילה את אחד ממונחי החיפוש
|
| 172 |
+
if any(t in line_lower for t in target_terms):
|
| 173 |
+
relevant_lines.append(line.strip())
|
| 174 |
|
| 175 |
clean_text = simplify_schematic_text("\n".join(relevant_lines))
|
| 176 |
+
return f"להלן רשימת החיבורים המדויקת (העתק את שמות הפינים בדיוק כפי שהם כתובים):\n{clean_text}"
|
| 177 |
|
| 178 |
return raw_content
|
| 179 |
return ""
|
|
|
|
| 194 |
class ChatCompletionRequest(BaseModel):
|
| 195 |
messages: List[Message]
|
| 196 |
max_tokens: Optional[int] = 1024
|
| 197 |
+
temperature: Optional[float] = 0.1 # נמוך מאוד לדיוק מקסימלי
|
| 198 |
stream: Optional[bool] = False
|
| 199 |
|
| 200 |
@app.on_event("startup")
|
|
|
|
| 211 |
save_message("user", last_user_msg.content)
|
| 212 |
context = query_knowledge_base(last_user_msg.content)
|
| 213 |
|
| 214 |
+
# System Prompt אגרסיבי למניעת המצאות
|
| 215 |
sys_msg = (
|
| 216 |
"תפקידך: טכנאי מומחה לפרויקט 'בית חכם תלם'.\n"
|
| 217 |
+
"הנחיות קריטיות לתשובה:\n"
|
| 218 |
+
"1. **שפה**: עברית בלבד.\n"
|
| 219 |
+
"2. **נאמנות למקור**: העתק את שמות הפינים (כמו HV1, RX, D16) **בדיוק** כפי שהם מופיעים בטקסט למטה. אל תשנה אותם.\n"
|
| 220 |
+
"3. **איסור מוחלט**: לעולם אל תמיר שם של פין למספר! (לדוגמה: אם כתוב HV1, אסור לכתוב 'פין 7').\n"
|
| 221 |
+
"4. **דיוק ברכיבים**: שים לב למי הרכיב מחובר. אם המסך מחובר לממיר רמות, אל תגיד שהוא מחובר ישירות לבקר.\n"
|
| 222 |
+
"5. **מבנה התשובה**: הצג את החיבורים בצורה של רשימה מסודרת ופשוטה.\n"
|
| 223 |
)
|
| 224 |
+
|
| 225 |
if context:
|
| 226 |
+
sys_msg += f"\n--- מידע טכני מאומת (הסתמך רק על זה) ---\n{context}\n------------------------------------"
|
| 227 |
else:
|
| 228 |
+
sys_msg += "\n(לא נמצא מידע ספציפי במערכת, ענה בזהירות מהידע הכללי שלך)."
|
| 229 |
|
| 230 |
messages_payload = [msg.dict() for msg in request.messages]
|
| 231 |
|