fjarsra commited on
Commit
a1521e4
Β·
verified Β·
1 Parent(s): 6228994

Update app/main.py

Browse files
Files changed (1) hide show
  1. app/main.py +41 -30
app/main.py CHANGED
@@ -165,40 +165,46 @@ def get_recommendations(user: schemas.UserProfile):
165
 
166
  @app.post("/chat/process", response_model=schemas.ChatResponse)
167
  async def process_chat(req: schemas.ChatRequest):
168
- role_data = skill_manager.get_role_data(req.role)
169
  available_skill_names = []
170
- if role_data:
171
- available_skill_names = [s['name'] for s in role_data['sub_skills']]
172
 
173
- # --- [Keyword Search Logic Tetap Ada] ---
174
- found_keywords = find_keywords_in_text(req.message)
175
- if found_keywords:
176
- keyword_context = ", ".join(found_keywords)
177
- dataset_status = "FOUND"
178
- else:
179
- keyword_context = "NONE"
180
- dataset_status = "NOT_FOUND"
181
-
182
- # --- [UPDATE BARU: Ektrak Silabus Lengkap] ---
183
- # Kita buat string rapi berisi Skill + Topik-topiknya
184
- found_keywords = find_keywords_in_text(req.message)
185
 
186
- # Siapkan context string untuk dikirim ke LLM
187
  if found_keywords:
188
- # Jika ketemu: "User bertanya tentang: Python, SQL"
189
  keyword_context = ", ".join(found_keywords)
190
  dataset_status = "FOUND"
191
- else:
192
- # Jika tidak ketemu
193
- keyword_context = "NONE"
194
- dataset_status = "NOT_FOUND"
195
 
196
- # 2. Router
197
- intent = await llm_engine.process_user_intent(req.message, available_skill_names)
 
 
 
 
 
198
 
199
- action = intent.get('action')
200
- # PERUBAHAN 1: Ambil List skills, bukan single skill
201
  detected_skills_list = intent.get('detected_skills', [])
 
 
 
 
 
 
 
 
 
202
 
203
  final_reply = ""
204
  response_data = None
@@ -269,12 +275,17 @@ async def process_chat(req: schemas.ChatRequest):
269
  final_reply = "Siap! Berikut adalah ringkasan progress belajar kamu sejauh ini. Silakan dicek di dashboard ya! πŸ“ŠπŸš€"
270
 
271
  elif action == "CASUAL_CHAT":
272
- final_reply = await llm_engine.casual_chat(
273
- req.message,
274
- [m.dict() for m in req.history],
275
- keyword_context,
276
- dataset_status
 
 
 
277
  )
 
 
278
 
279
  return schemas.ChatResponse(
280
  reply=final_reply,
 
165
 
166
  @app.post("/chat/process", response_model=schemas.ChatResponse)
167
  async def process_chat(req: schemas.ChatRequest):
 
168
  available_skill_names = []
169
+ role_data = None
 
170
 
171
+ # Cek apakah Role ada isinya? (Safety check)
172
+ if req.role and req.role.strip() != "":
173
+ role_data = skill_manager.get_role_data(req.role)
174
+ if role_data:
175
+ available_skill_names = [s['name'] for s in role_data['sub_skills']]
176
+
177
+ found_keywords = find_keywords_in_text(req.message) # Asumsi fungsi ini ada
178
+
179
+ keyword_context = "NONE"
180
+ dataset_status = "NOT_FOUND"
 
 
181
 
 
182
  if found_keywords:
 
183
  keyword_context = ", ".join(found_keywords)
184
  dataset_status = "FOUND"
185
+
186
+ # [PENTING] Konversi History ke Dict agar tidak error di LLM
187
+ history_dicts = [m.model_dump() if hasattr(m, 'model_dump') else m.dict() for m in req.history]
 
188
 
189
+ # Kirim parameter lengkap ke Router
190
+ intent = await llm_engine.process_user_intent(
191
+ user_text=req.message,
192
+ available_skills=available_skill_names,
193
+ user_role=req.role,
194
+ history=history_dicts # Tambahan agar AI ingat konteks
195
+ )
196
 
197
+ action = intent.get('action', 'CASUAL_CHAT')
 
198
  detected_skills_list = intent.get('detected_skills', [])
199
+
200
+ user_role_is_empty = not req.role or req.role.strip() == ""
201
+ restricted_actions = ["START_EXAM", "GET_RECOMMENDATION", "CHECK_PROGRESS"]
202
+
203
+ if action in restricted_actions and user_role_is_empty:
204
+ print(f"DEBUG: Role Kosong mencoba {action} -> BELOKKAN KE CASUAL_CHAT")
205
+ action = "CASUAL_CHAT"
206
+
207
+ # ============================================================
208
 
209
  final_reply = ""
210
  response_data = None
 
275
  final_reply = "Siap! Berikut adalah ringkasan progress belajar kamu sejauh ini. Silakan dicek di dashboard ya! πŸ“ŠπŸš€"
276
 
277
  elif action == "CASUAL_CHAT":
278
+
279
+ print(f"DEBUG ROLE STATUS: '{req.role}' -> is_empty={user_role_is_empty}")
280
+ history_dicts = [m.model_dump() if hasattr(m, 'model_dump') else m.dict() for m in req.history]
281
+
282
+ reply_text = await llm_engine.casual_chat(
283
+ user_text=req.message,
284
+ history=history_dicts,
285
+ is_role_empty=user_role_is_empty
286
  )
287
+
288
+ final_reply = reply_text
289
 
290
  return schemas.ChatResponse(
291
  reply=final_reply,