ciyidogan commited on
Commit
ddb14f5
·
verified ·
1 Parent(s): c31edec

Update chat_handler.py

Browse files
Files changed (1) hide show
  1. chat_handler.py +56 -17
chat_handler.py CHANGED
@@ -1,5 +1,5 @@
1
  """
2
- Flare – Chat Handler (v1.6 · detaylı log)
3
  ==========================================
4
  """
5
 
@@ -247,7 +247,10 @@ async def _handle_parameter_followup(session: Session, user_input: str, version)
247
  prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
248
  raw = await spark_generate(session, prompt, user_input)
249
 
250
- if not raw.startswith("#PARAMETERS:"):
 
 
 
251
  # Increment miss count
252
  session.missing_ask_count += 1
253
  log(f"⚠️ No parameters extracted, miss count: {session.missing_ask_count}")
@@ -257,12 +260,6 @@ async def _handle_parameter_followup(session: Session, user_input: str, version)
257
  return "Üzgünüm, istediğiniz bilgileri anlayamadım. Başka bir konuda yardımcı olabilir miyim?"
258
  return "Üzgünüm, anlayamadım. Lütfen tekrar söyler misiniz?"
259
 
260
- # Process parameters
261
- success = _process_parameters(session, intent_config, raw)
262
- if not success:
263
- log("❌ Parameter processing failed")
264
- return "Girdiğiniz bilgilerde bir hata var. Lütfen kontrol edip tekrar deneyin."
265
-
266
  # Check if we have all required parameters
267
  missing = _get_missing_parameters(session, intent_config)
268
  log(f"📊 Still missing params: {missing}")
@@ -292,13 +289,14 @@ async def _extract_parameters(session: Session, intent_config, user_input: str)
292
  prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
293
  raw = await spark_generate(session, prompt, user_input)
294
 
295
- if raw.startswith("#PARAMETERS:"):
296
- success = _process_parameters(session, intent_config, raw)
297
- if success:
298
- missing = _get_missing_parameters(session, intent_config)
299
- log(f"📊 After extraction, missing: {missing}")
 
300
  else:
301
- log("⚠️ No #PARAMETERS tag in response")
302
 
303
  if missing:
304
  # Still missing parameters
@@ -323,10 +321,47 @@ def _get_missing_parameters(session: Session, intent_config) -> List[str]:
323
  return missing
324
 
325
  def _process_parameters(session: Session, intent_config, raw: str) -> bool:
326
- """Process parameter extraction response"""
327
  try:
328
- json_str = raw[len("#PARAMETERS:"):]
329
- log(f"🔍 Parsing parameters JSON: {json_str[:200]}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
330
  data = json.loads(json_str)
331
 
332
  extracted = data.get("extracted", [])
@@ -361,6 +396,10 @@ def _process_parameters(session: Session, intent_config, raw: str) -> bool:
361
 
362
  return any_valid
363
 
 
 
 
 
364
  except Exception as e:
365
  log(f"❌ Parameter processing error: {e}")
366
  return False
 
1
  """
2
+ Flare – Chat Handler (v1.7 · parameter parsing düzeltmesi)
3
  ==========================================
4
  """
5
 
 
247
  prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
248
  raw = await spark_generate(session, prompt, user_input)
249
 
250
+ # Try parsing with or without #PARAMETERS: prefix
251
+ success = _process_parameters(session, intent_config, raw)
252
+
253
+ if not success:
254
  # Increment miss count
255
  session.missing_ask_count += 1
256
  log(f"⚠️ No parameters extracted, miss count: {session.missing_ask_count}")
 
260
  return "Üzgünüm, istediğiniz bilgileri anlayamadım. Başka bir konuda yardımcı olabilir miyim?"
261
  return "Üzgünüm, anlayamadım. Lütfen tekrar söyler misiniz?"
262
 
 
 
 
 
 
 
263
  # Check if we have all required parameters
264
  missing = _get_missing_parameters(session, intent_config)
265
  log(f"📊 Still missing params: {missing}")
 
289
  prompt = build_parameter_prompt(intent_config, missing, user_input, session.chat_history)
290
  raw = await spark_generate(session, prompt, user_input)
291
 
292
+ # Try processing with flexible parsing
293
+ success = _process_parameters(session, intent_config, raw)
294
+
295
+ if success:
296
+ missing = _get_missing_parameters(session, intent_config)
297
+ log(f"📊 After extraction, missing: {missing}")
298
  else:
299
+ log("⚠️ Failed to extract parameters from response")
300
 
301
  if missing:
302
  # Still missing parameters
 
321
  return missing
322
 
323
  def _process_parameters(session: Session, intent_config, raw: str) -> bool:
324
+ """Process parameter extraction response with flexible parsing"""
325
  try:
326
+ # Try to parse JSON, handling both with and without #PARAMETERS: prefix
327
+ json_str = raw
328
+ if raw.startswith("#PARAMETERS:"):
329
+ json_str = raw[len("#PARAMETERS:"):]
330
+ log(f"🔍 Found #PARAMETERS: prefix, removing it")
331
+
332
+ # Clean up any trailing content after JSON
333
+ # Find the closing brace for the JSON object
334
+ brace_count = 0
335
+ json_end = -1
336
+ in_string = False
337
+ escape_next = False
338
+
339
+ for i, char in enumerate(json_str):
340
+ if escape_next:
341
+ escape_next = False
342
+ continue
343
+
344
+ if char == '\\':
345
+ escape_next = True
346
+ continue
347
+
348
+ if char == '"' and not escape_next:
349
+ in_string = not in_string
350
+ continue
351
+
352
+ if not in_string:
353
+ if char == '{':
354
+ brace_count += 1
355
+ elif char == '}':
356
+ brace_count -= 1
357
+ if brace_count == 0:
358
+ json_end = i + 1
359
+ break
360
+
361
+ if json_end > 0:
362
+ json_str = json_str[:json_end]
363
+ log(f"🔍 Cleaned JSON string: {json_str[:200]}")
364
+
365
  data = json.loads(json_str)
366
 
367
  extracted = data.get("extracted", [])
 
396
 
397
  return any_valid
398
 
399
+ except json.JSONDecodeError as e:
400
+ log(f"❌ JSON parsing error: {e}")
401
+ log(f"❌ Failed to parse: {raw[:200]}")
402
+ return False
403
  except Exception as e:
404
  log(f"❌ Parameter processing error: {e}")
405
  return False