File size: 10,853 Bytes
131c535
 
 
 
 
 
567e59a
131c535
 
 
 
9d10098
131c535
e2e3613
a2fec61
2b0eaa9
131c535
 
6c63b9d
873a0cf
131c535
 
 
 
6224458
131c535
 
 
 
 
 
 
 
 
9d10098
367b667
c92bff2
658cc03
9413451
6c63b9d
 
 
 
431d2fe
a2fec61
96f72a9
 
c9edaae
e2e3613
 
131c535
 
 
 
 
 
567e59a
 
131c535
 
 
96f72a9
131c535
 
 
 
96f72a9
131c535
96f72a9
131c535
5cdb914
131c535
 
e2e3613
9413451
6423a78
e2e3613
c9a2411
 
9413451
c9a2411
6423a78
3614b16
 
 
e2e3613
 
f29d911
6c63b9d
 
 
 
255c1e3
331d60c
 
 
 
 
873a0cf
6c63b9d
 
 
 
df48eb2
6c63b9d
 
 
873a0cf
 
 
6c63b9d
873a0cf
658cc03
e8f187f
255c1e3
873a0cf
f29d911
a3f45d1
c9edaae
131c535
 
c9edaae
e2e3613
6423a78
e2e3613
df48eb2
658cc03
 
131c535
e2e3613
5cdb914
e2e3613
55ab6d4
5cdb914
9413451
e2e3613
 
df48eb2
658cc03
afad8d4
c9edaae
e2e3613
 
df48eb2
 
 
 
 
 
 
131c535
3614b16
e2e3613
3614b16
 
96f72a9
e8f187f
96f72a9
 
 
331d60c
658cc03
 
 
df48eb2
 
 
 
 
 
 
 
331d60c
873a0cf
c9edaae
658cc03
e8f187f
c9edaae
 
131c535
 
 
 
 
 
 
 
5cdb914
131c535
 
 
 
 
 
 
a2fec61
6224458
131c535
 
 
 
 
9d10098
131c535
 
 
5cdb914
 
 
 
96f72a9
c9edaae
5cdb914
6224458
9d10098
6224458
 
 
 
 
 
 
9d10098
331d60c
5cdb914
6224458
258f6c3
6224458
258f6c3
567e59a
873a0cf
567e59a
 
258f6c3
567e59a
258f6c3
 
 
 
9d10098
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258f6c3
 
 
9d10098
 
 
 
 
 
 
 
 
 
 
 
 
258f6c3
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
import uvicorn
import json
import logging
import sqlite3
import os
import re
from typing import List, Optional, Literal
from fastapi import FastAPI, HTTPException
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
from pydantic import BaseModel
from huggingface_hub import InferenceClient

# --- Configuration ---
MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
PORT = int(os.environ.get("PORT", 8000))
HOST = "0.0.0.0"
DB_FILE = "chatbot.db"
JSON_SOURCE = "database.json"
HF_TOKEN = os.environ.get("HF_TOKEN")

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

app = FastAPI(title="Hebrew Smart Home Technician", version="10.4.0")

app.add_middleware(
    CORSMiddleware,
    allow_origins=["*"],
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

client = InferenceClient(model=MODEL_ID, token=HF_TOKEN)

# ---------------------------------
# --- מילון מונחים לחיפוש ---
SEARCH_MAP = {
    "בקר": ["ESP32", "Controller", "Main Board", "DevKit"],
    "מסך": ["Nextion", "Display", "Screen", "HMI", "Nextion Screen"],
    "ממיר": ["Level Shifter", "LevelConvertor", "BOB", "Converter"],
    "לוח": ["Student Board", "PCB", "Board"],
    "שרטוט": ["Schematic", "Connections", "Pin", "Net"],
    "חיבור": ["Connect", "Wire", "Signal", "RX", "TX", "HV", "LV"],
    "תקלה": ["Error", "Problem", "Fix"],
    "זרם": ["ACS712", "Sensor"],
    "מתח": ["3.3V", "5V", "VIN", "Power"]
}

def init_db():
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    cursor.execute('CREATE TABLE IF NOT EXISTS knowledge (id INTEGER PRIMARY KEY, topic TEXT, content TEXT)')
    cursor.execute('CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, role TEXT, content TEXT)')

    cursor.execute('SELECT count(*) FROM knowledge')
    if cursor.fetchone()[0] == 0 and os.path.exists(JSON_SOURCE):
        try:
            with open(JSON_SOURCE, 'r', encoding='utf-8') as f:
                data = json.load(f)
                count = 0
                for item in data:
                    if isinstance(item, dict) and 'content' in item:
                        cursor.execute('INSERT INTO knowledge (topic, content) VALUES (?, ?)', 
                                     (item.get('topic', 'General'), item['content']))
                        count += 1
                conn.commit()
                logger.info(f"✅ Loaded {count} items from database.json")
        except Exception as e:
            logger.error(f"❌ Error loading JSON: {e}")
    conn.close()

def expand_query(query: str) -> List[str]:
    words = re.sub(r'[^\w\s]', '', query).split()
    search_terms = set(words)
    for w in words:
        root = w[1:] if len(w) > 3 and w.startswith('ה') else w
        search_terms.add(root)
        for key, values in SEARCH_MAP.items():
            if key in root:
                search_terms.update(values)
        if w.isdigit():
            search_terms.add(w) 
            search_terms.add(f"D{w}")
    return list(search_terms)

def simplify_schematic_text(text: str) -> str:
    """
    Cleans up the English schematic text and translates key terms to Hebrew
    for better readability in the chat.
    """
    text = re.sub(r'\([A-Za-z0-9_\-]+\)', '', text)
    
    text = text.replace("D16_or_RX2", "RX2")
    text = text.replace("D17_or_TX2", "TX2")
    text = text.replace("3V3", "3.3V")
    
    replacements = {
        "Level Shifter": "ממיר רמות",
        "Student Board": "לוח תלמיד",
        "Nextion Screen": "מסך Nextion",
        "ESP32": "בקר ESP32",
        "connects to": "--- מחובר אל --->",
        "Pin": "פין",
        "GND": "הארקה (GND)",
        "VIN": "מתח כניסה (VIN)"
    }
    
    for eng, heb in replacements.items():
        text = re.sub(r'\b' + re.escape(eng) + r'\b', heb, text)

    text = text.replace("Pin Pin", "פין")
    text = text.replace("פין פין", "פין")
    text = re.sub(r'\s+', ' ', text).strip()
    
    return text

def query_knowledge_base(query: str) -> str:
    conn = sqlite3.connect(DB_FILE)
    cursor = conn.cursor()
    
    search_terms = expand_query(query)
    logger.info(f"🔍 Searching for: {search_terms}")
    
    is_general_schematic_query = any(x in query for x in ["הכל", "כל החיבורים", "שרטוט", "מה מחובר", "לאן", "פינים"])
    is_definition_query = any(x in query for x in ["מה זה", "הסבר", "מידע על", "תאר"])

    results = []
    seen_ids = set()
    
    for term in search_terms:
        cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE ? OR topic LIKE ?", (f'%{term}%', f'%{term}%'))
        rows = cursor.fetchall()
        for row_id, content, topic in rows:
            if row_id not in seen_ids:
                score = 1
                if "Schematic" in content:
                    score += 100 
                elif term.lower() in topic.lower():
                    score += 10
                results.append((score, content))
                seen_ids.add(row_id)
    
    if not results and is_general_schematic_query:
        cursor.execute("SELECT id, content, topic FROM knowledge WHERE content LIKE '%Schematic%'")
        row = cursor.fetchone()
        if row:
            results.append((100, row[1]))

    conn.close()
    
    results.sort(key=lambda x: x[0], reverse=True)
    
    if results:
        raw_content = results[0][1]
        
        if "Schematic" in raw_content:
            lines = raw_content.split('\n')
            relevant_lines = []
            
            if is_definition_query and not is_general_schematic_query:
                return simplify_schematic_text(raw_content)

            if is_general_schematic_query:
                 for line in lines:
                    if "connect" in line.lower():
                        relevant_lines.append(line.strip())
            else:
                target_terms = [t.lower() for t in search_terms if len(t) > 1]
                for line in lines:
                    if "connect" in line.lower() and any(t in line.lower() for t in target_terms):
                        relevant_lines.append(line.strip())
            
            clean_text = simplify_schematic_text("\n".join(relevant_lines))
            return f"להלן המידע הטכני שנמצא:\n{clean_text}"
            
        return raw_content
    return ""

def save_message(role: str, content: str):
    try:
        conn = sqlite3.connect(DB_FILE)
        cursor = conn.cursor()
        cursor.execute("INSERT INTO history (role, content) VALUES (?, ?)", (role, content))
        conn.commit()
        conn.close()
    except: pass

class Message(BaseModel):
    role: Literal["user", "assistant", "system"]
    content: str

class ChatCompletionRequest(BaseModel):
    messages: List[Message]
    max_tokens: Optional[int] = 1024
    temperature: Optional[float] = 0.4 # הועלה מעט כדי לאפשר ניסוח טבעי יותר
    stream: Optional[bool] = False

@app.on_event("startup")
async def startup_event():
    init_db()
    logger.info("✅ Server started in API Mode")

@app.post("/v1/chat/completions")
async def chat_completions(request: ChatCompletionRequest):
    last_user_msg = next((m for m in reversed(request.messages) if m.role == "user"), None)
    
    context = ""
    if last_user_msg:
        save_message("user", last_user_msg.content)
        context = query_knowledge_base(last_user_msg.content)

    # --- הנחיות מערכת מעודכנות: "המדריך המקצועי" ---
    sys_msg = (
        "תפקידך: טכנאי מומחה וחונך לפרויקט 'בית חכם תלם'.\n"
        "המטרה: לעזור למשתמש להבין את החיווט והמערכת בצורה נעימה, ברורה ומקצועית.\n\n"
        "הנחיות לכתיבת התשובה:\n"
        "1. **היה אנושי ומועיל:** אל תפלוט רשימה יבשה. דבר אל המשתמש בגובה העיניים (למשל: 'כדי לחבר את הממיר, שים לב לחיבורים הבאים...').\n"
        "2. **קבץ מידע:** אם יש מספר חיבורים הקשורים לתקשורת (RX/TX) או למתח (GND/VCC), נסה לאחד אותם למשפט ברור במקום שורות נפרדות.\n"
        "3. **דיוק טכני:** הסתמך אך ורק על המידע המופיע למטה. אל תמציא חיבורים שלא קיימים.\n"
        "4. השתמש בשמות הפינים באנגלית (כמו HV1, LV2, GND) כדי לשמור על דיוק.\n"
    )
    
    if context:
        sys_msg += f"\n--- מידע טכני מהמערכת (השתמש בזה כדי לענות) ---\n{context}\n------------------------------------"
    else:
        sys_msg += "\n(לא נמצא מידע ספציפי. נסה לענות על בסיס ידע כללי אם זה רלוונטי, או בקש מהמשתמש לפרט)."

    messages_payload = [msg.dict() for msg in request.messages]
    
    if messages_payload[0]['role'] == 'system':
        messages_payload[0]['content'] = sys_msg
    else:
        messages_payload.insert(0, {"role": "system", "content": sys_msg})

    if request.stream:
        async def stream_generator():
            full_text = ""
            try:
                for chunk in client.chat_completion(
                    messages=messages_payload, 
                    max_tokens=request.max_tokens, 
                    temperature=request.temperature, 
                    stream=True
                ):
                    delta = chunk.choices[0].delta.content
                    if delta:
                        full_text += delta
                        yield f"data: {json.dumps({'choices': [{'delta': {'content': delta}}]}, ensure_ascii=False)}\n\n"
                save_message("assistant", full_text)
                yield "data: [DONE]\n\n"
            except Exception as e:
                logger.error(f"API Error: {e}")
                yield f"data: {json.dumps({'error': str(e)})}\n\n"

        return StreamingResponse(stream_generator(), media_type="text/event-stream; charset=utf-8")
    
    try:
        response = client.chat_completion(
            messages=messages_payload, 
            max_tokens=request.max_tokens, 
            temperature=request.temperature, 
            stream=False
        )
        content = response.choices[0].message.content
        save_message("assistant", content)
        return response
    except Exception as e:
        logger.error(f"API Error: {e}")
        raise HTTPException(500, f"Hugging Face API Error: {str(e)}")

if __name__ == "__main__":
    uvicorn.run("server:app", host=HOST, port=PORT, reload=False)