Spaces:
Sleeping
Sleeping
Update server.py
Browse files
server.py
CHANGED
|
@@ -13,6 +13,7 @@ from fastapi.responses import StreamingResponse
|
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
|
|
|
| 16 |
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 17 |
PORT = int(os.environ.get("PORT", 8000))
|
| 18 |
HOST = "0.0.0.0"
|
|
@@ -22,7 +23,7 @@ JSON_SOURCE = "database.json"
|
|
| 22 |
logging.basicConfig(level=logging.INFO)
|
| 23 |
logger = logging.getLogger(__name__)
|
| 24 |
|
| 25 |
-
app = FastAPI(title="Hebrew RAG Chatbot", version="5.
|
| 26 |
|
| 27 |
app.add_middleware(
|
| 28 |
CORSMiddleware,
|
|
@@ -36,7 +37,18 @@ model = None
|
|
| 36 |
tokenizer = None
|
| 37 |
device = "cpu"
|
| 38 |
|
| 39 |
-
# ---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
def init_db():
|
| 41 |
if os.path.exists(DB_FILE): os.remove(DB_FILE)
|
| 42 |
conn = sqlite3.connect(DB_FILE)
|
|
@@ -60,22 +72,57 @@ def init_db():
|
|
| 60 |
logger.error(f"❌ Error loading JSON: {e}")
|
| 61 |
conn.close()
|
| 62 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
def query_knowledge_base(query: str) -> str:
|
| 64 |
conn = sqlite3.connect(DB_FILE)
|
| 65 |
cursor = conn.cursor()
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
results = []
|
|
|
|
| 69 |
|
| 70 |
-
for
|
| 71 |
-
|
|
|
|
| 72 |
rows = cursor.fetchall()
|
| 73 |
-
for
|
| 74 |
-
if
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
conn.close()
|
| 78 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
def save_message(role: str, content: str):
|
| 81 |
try:
|
|
@@ -136,18 +183,18 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 136 |
context = query_knowledge_base(last_user_msg.content)
|
| 137 |
|
| 138 |
if context:
|
| 139 |
-
# הנחיה מדויקת להשתמש במידע הקיים
|
| 140 |
sys_msg = (
|
| 141 |
-
"System: You are a
|
| 142 |
"Instructions:\n"
|
| 143 |
"1. Answer ONLY in Hebrew.\n"
|
| 144 |
-
"2. Use the
|
| 145 |
-
"3.
|
| 146 |
-
f"
|
| 147 |
)
|
| 148 |
else:
|
| 149 |
sys_msg = "System: You are a helpful assistant. Answer strictly in Hebrew. If you don't know, say so politely."
|
| 150 |
|
|
|
|
| 151 |
if request.messages[0].role == "system":
|
| 152 |
request.messages[0].content = sys_msg
|
| 153 |
else:
|
|
@@ -161,11 +208,11 @@ async def chat_completions(request: ChatCompletionRequest):
|
|
| 161 |
gen_kwargs = dict(
|
| 162 |
inputs=prompt_ids,
|
| 163 |
max_new_tokens=request.max_tokens,
|
| 164 |
-
temperature=0.2,
|
| 165 |
top_p=0.9,
|
| 166 |
do_sample=True,
|
| 167 |
pad_token_id=tokenizer.eos_token_id,
|
| 168 |
-
repetition_penalty=1.
|
| 169 |
)
|
| 170 |
|
| 171 |
if request.stream:
|
|
|
|
| 13 |
from pydantic import BaseModel
|
| 14 |
from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
|
| 15 |
|
| 16 |
+
# --- Configuration ---
|
| 17 |
MODEL_ID = "Qwen/Qwen2.5-1.5B-Instruct"
|
| 18 |
PORT = int(os.environ.get("PORT", 8000))
|
| 19 |
HOST = "0.0.0.0"
|
|
|
|
| 23 |
logging.basicConfig(level=logging.INFO)
|
| 24 |
logger = logging.getLogger(__name__)
|
| 25 |
|
| 26 |
+
app = FastAPI(title="Hebrew RAG Chatbot", version="5.5.0")
|
| 27 |
|
| 28 |
app.add_middleware(
|
| 29 |
CORSMiddleware,
|
|
|
|
| 37 |
tokenizer = None
|
| 38 |
device = "cpu"
|
| 39 |
|
| 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():
|
| 53 |
if os.path.exists(DB_FILE): os.remove(DB_FILE)
|
| 54 |
conn = sqlite3.connect(DB_FILE)
|
|
|
|
| 72 |
logger.error(f"❌ Error loading JSON: {e}")
|
| 73 |
conn.close()
|
| 74 |
|
| 75 |
+
def expand_query(query: str) -> List[str]:
|
| 76 |
+
"""מפרק את השאילתה למילות מפתח ומוסיף מילים נרדפות טכניות"""
|
| 77 |
+
words = query.replace("?", "").replace(".", "").split()
|
| 78 |
+
search_terms = set()
|
| 79 |
+
|
| 80 |
+
for w in words:
|
| 81 |
+
# תיקון קריטי: לא מסננים מילים קצרות אם הן מכילות מספרים (כמו 18, D5)
|
| 82 |
+
if len(w) < 2 and not any(char.isdigit() for char in w):
|
| 83 |
+
continue
|
| 84 |
+
|
| 85 |
+
search_terms.add(w)
|
| 86 |
+
|
| 87 |
+
# הוספת מילים נרדפות
|
| 88 |
+
for hebrew_key, english_vals in SEARCH_SYNONYMS.items():
|
| 89 |
+
if hebrew_key in w:
|
| 90 |
+
for val in english_vals:
|
| 91 |
+
search_terms.add(val)
|
| 92 |
+
|
| 93 |
+
return list(search_terms)
|
| 94 |
+
|
| 95 |
def query_knowledge_base(query: str) -> str:
|
| 96 |
conn = sqlite3.connect(DB_FILE)
|
| 97 |
cursor = conn.cursor()
|
| 98 |
+
|
| 99 |
+
search_terms = expand_query(query)
|
| 100 |
+
logger.info(f"🔍 Searching for: {search_terms}")
|
| 101 |
+
|
| 102 |
results = []
|
| 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 |
+
|
| 125 |
+
return "\n---\n".join(final_content)
|
| 126 |
|
| 127 |
def save_message(role: str, content: str):
|
| 128 |
try:
|
|
|
|
| 183 |
context = query_knowledge_base(last_user_msg.content)
|
| 184 |
|
| 185 |
if context:
|
|
|
|
| 186 |
sys_msg = (
|
| 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. If asking about a Pin connection, state exactly what component and pin it connects to based on the text.\n"
|
| 192 |
+
f"\nContext:\n{context}"
|
| 193 |
)
|
| 194 |
else:
|
| 195 |
sys_msg = "System: You are a helpful assistant. Answer strictly in Hebrew. If you don't know, say so politely."
|
| 196 |
|
| 197 |
+
# Force system message update
|
| 198 |
if request.messages[0].role == "system":
|
| 199 |
request.messages[0].content = sys_msg
|
| 200 |
else:
|
|
|
|
| 208 |
gen_kwargs = dict(
|
| 209 |
inputs=prompt_ids,
|
| 210 |
max_new_tokens=request.max_tokens,
|
| 211 |
+
temperature=0.2,
|
| 212 |
top_p=0.9,
|
| 213 |
do_sample=True,
|
| 214 |
pad_token_id=tokenizer.eos_token_id,
|
| 215 |
+
repetition_penalty=1.05
|
| 216 |
)
|
| 217 |
|
| 218 |
if request.stream:
|