Spaces:
Building
Building
Update chat_handler.py
Browse files- chat_handler.py +81 -25
chat_handler.py
CHANGED
@@ -369,10 +369,10 @@ async def chat(req: ChatRequest, x_session_id: str = Header(...)):
|
|
369 |
|
370 |
# Check what parameters are missing
|
371 |
missing_params = [
|
372 |
-
p for p in intent_config.parameters
|
373 |
if p.required and p.variable_name not in session.variables
|
374 |
]
|
375 |
-
|
376 |
if not missing_params:
|
377 |
# All required parameters collected, execute API
|
378 |
response = await _execute_api_call(session, intent_config)
|
@@ -380,16 +380,31 @@ async def chat(req: ChatRequest, x_session_id: str = Header(...)):
|
|
380 |
return {"response": response, "intent": intent_name, "state": "completed"}
|
381 |
else:
|
382 |
# Need to collect more parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
383 |
param_prompt = build_parameter_prompt(
|
384 |
-
|
385 |
-
|
386 |
-
session.chat_history,
|
387 |
-
|
|
|
|
|
|
|
|
|
|
|
388 |
)
|
|
|
389 |
param_question = await llm_generate(session, param_prompt, req.message)
|
390 |
clean_question = _trim_response(param_question)
|
391 |
session.add_message("assistant", clean_question)
|
392 |
-
return {"response": clean_question, "intent": intent_name, "state": "collecting_params"}
|
|
|
393 |
else:
|
394 |
log_info(f"⚠️ Unknown intent: {intent_name}")
|
395 |
|
@@ -422,7 +437,7 @@ async def chat(req: ChatRequest, x_session_id: str = Header(...)):
|
|
422 |
|
423 |
# Check what parameters are still missing
|
424 |
missing_params = [
|
425 |
-
p for p in intent_config.parameters
|
426 |
if p.required and p.variable_name not in session.variables
|
427 |
]
|
428 |
|
@@ -433,11 +448,24 @@ async def chat(req: ChatRequest, x_session_id: str = Header(...)):
|
|
433 |
return {"response": response, "intent": session.current_intent, "state": "completed"}
|
434 |
else:
|
435 |
# Still need more parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
436 |
param_prompt = build_parameter_prompt(
|
437 |
-
|
438 |
-
|
439 |
-
session.chat_history,
|
440 |
-
|
|
|
|
|
|
|
|
|
|
|
441 |
)
|
442 |
param_question = await llm_generate(session, param_prompt, req.message)
|
443 |
clean_question = _trim_response(param_question)
|
@@ -514,7 +542,7 @@ async def handle_new_message(session: Session, user_input: str) -> str:
|
|
514 |
|
515 |
# Check what parameters are missing
|
516 |
missing_params = [
|
517 |
-
p for p in intent_config.parameters
|
518 |
if p.required and p.variable_name not in session.variables
|
519 |
]
|
520 |
|
@@ -523,11 +551,23 @@ async def handle_new_message(session: Session, user_input: str) -> str:
|
|
523 |
return await _execute_api_call(session, intent_config)
|
524 |
else:
|
525 |
# Need to collect more parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
526 |
param_prompt = build_parameter_prompt(
|
527 |
-
|
528 |
-
|
529 |
-
session.chat_history,
|
530 |
-
|
|
|
|
|
|
|
|
|
|
|
531 |
)
|
532 |
param_question = await llm_generate(session, param_prompt, user_input)
|
533 |
return _trim_response(param_question)
|
@@ -538,7 +578,7 @@ async def handle_new_message(session: Session, user_input: str) -> str:
|
|
538 |
except Exception as e:
|
539 |
log_error("❌ Error in handle_new_message", e)
|
540 |
return "Bir hata oluştu. Lütfen tekrar deneyin."
|
541 |
-
|
542 |
async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
543 |
"""Handle parameter collection followup - for WebSocket"""
|
544 |
try:
|
@@ -549,11 +589,15 @@ async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
|
549 |
|
550 |
intent_config = session.intent_config
|
551 |
|
552 |
-
# Get project config
|
553 |
project = next((p for p in cfg.projects if p.name == session.project_name), None)
|
554 |
if not project:
|
555 |
return "Proje konfigürasyonu bulunamadı."
|
556 |
|
|
|
|
|
|
|
|
|
557 |
# Try to extract parameters from user message
|
558 |
param_prompt = f"""
|
559 |
Extract parameters from user message: "{user_input}"
|
@@ -574,7 +618,7 @@ async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
|
574 |
|
575 |
# Check what parameters are still missing
|
576 |
missing_params = [
|
577 |
-
p for p in intent_config.parameters
|
578 |
if p.required and p.variable_name not in session.variables
|
579 |
]
|
580 |
|
@@ -583,11 +627,23 @@ async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
|
583 |
return await _execute_api_call(session, intent_config)
|
584 |
else:
|
585 |
# Still need more parameters
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
586 |
param_prompt = build_parameter_prompt(
|
587 |
-
|
588 |
-
|
589 |
-
session.chat_history,
|
590 |
-
|
|
|
|
|
|
|
|
|
|
|
591 |
)
|
592 |
param_question = await llm_generate(session, param_prompt, user_input)
|
593 |
return _trim_response(param_question)
|
@@ -596,7 +652,7 @@ async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
|
596 |
log_error("❌ Error in handle_parameter_followup", e)
|
597 |
session.reset_flow()
|
598 |
return "Bir hata oluştu. Lütfen tekrar deneyin."
|
599 |
-
|
600 |
def get_user_friendly_error(error_type: str, context: dict = None) -> str:
|
601 |
"""Get user-friendly error messages"""
|
602 |
error_messages = {
|
|
|
369 |
|
370 |
# Check what parameters are missing
|
371 |
missing_params = [
|
372 |
+
p.name for p in intent_config.parameters
|
373 |
if p.required and p.variable_name not in session.variables
|
374 |
]
|
375 |
+
|
376 |
if not missing_params:
|
377 |
# All required parameters collected, execute API
|
378 |
response = await _execute_api_call(session, intent_config)
|
|
|
380 |
return {"response": response, "intent": intent_name, "state": "completed"}
|
381 |
else:
|
382 |
# Need to collect more parameters
|
383 |
+
# Get parameter collection config
|
384 |
+
cfg = ConfigProvider.get()
|
385 |
+
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config", {})
|
386 |
+
max_params = collection_config.get("max_params_per_question", 2)
|
387 |
+
|
388 |
+
# Decide which parameters to ask
|
389 |
+
params_to_ask = missing_params[:max_params]
|
390 |
+
|
391 |
param_prompt = build_parameter_prompt(
|
392 |
+
version=version,
|
393 |
+
intent_config=intent_config,
|
394 |
+
chat_history=session.chat_history,
|
395 |
+
collected_params=session.variables,
|
396 |
+
missing_params=missing_params,
|
397 |
+
params_to_ask=params_to_ask,
|
398 |
+
max_params=max_params,
|
399 |
+
project_locale=project.default_locale,
|
400 |
+
unanswered_params=session.unanswered_parameters
|
401 |
)
|
402 |
+
|
403 |
param_question = await llm_generate(session, param_prompt, req.message)
|
404 |
clean_question = _trim_response(param_question)
|
405 |
session.add_message("assistant", clean_question)
|
406 |
+
return {"response": clean_question, "intent": intent_name, "state": "collecting_params"}
|
407 |
+
|
408 |
else:
|
409 |
log_info(f"⚠️ Unknown intent: {intent_name}")
|
410 |
|
|
|
437 |
|
438 |
# Check what parameters are still missing
|
439 |
missing_params = [
|
440 |
+
p.name for p in intent_config.parameters
|
441 |
if p.required and p.variable_name not in session.variables
|
442 |
]
|
443 |
|
|
|
448 |
return {"response": response, "intent": session.current_intent, "state": "completed"}
|
449 |
else:
|
450 |
# Still need more parameters
|
451 |
+
# Get parameter collection config
|
452 |
+
cfg = ConfigProvider.get()
|
453 |
+
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config", {})
|
454 |
+
max_params = collection_config.get("max_params_per_question", 2)
|
455 |
+
|
456 |
+
# Decide which parameters to ask
|
457 |
+
params_to_ask = missing_params[:max_params]
|
458 |
+
|
459 |
param_prompt = build_parameter_prompt(
|
460 |
+
version=version,
|
461 |
+
intent_config=intent_config,
|
462 |
+
chat_history=session.chat_history,
|
463 |
+
collected_params=session.variables,
|
464 |
+
missing_params=missing_params,
|
465 |
+
params_to_ask=params_to_ask,
|
466 |
+
max_params=max_params,
|
467 |
+
project_locale=project.default_locale,
|
468 |
+
unanswered_params=session.unanswered_parameters
|
469 |
)
|
470 |
param_question = await llm_generate(session, param_prompt, req.message)
|
471 |
clean_question = _trim_response(param_question)
|
|
|
542 |
|
543 |
# Check what parameters are missing
|
544 |
missing_params = [
|
545 |
+
p.name for p in intent_config.parameters
|
546 |
if p.required and p.variable_name not in session.variables
|
547 |
]
|
548 |
|
|
|
551 |
return await _execute_api_call(session, intent_config)
|
552 |
else:
|
553 |
# Need to collect more parameters
|
554 |
+
cfg = ConfigProvider.get()
|
555 |
+
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config", {})
|
556 |
+
max_params = collection_config.get("max_params_per_question", 2)
|
557 |
+
|
558 |
+
# Decide which parameters to ask
|
559 |
+
params_to_ask = missing_params[:max_params]
|
560 |
+
|
561 |
param_prompt = build_parameter_prompt(
|
562 |
+
version=version,
|
563 |
+
intent_config=intent_config,
|
564 |
+
chat_history=session.chat_history,
|
565 |
+
collected_params=session.variables,
|
566 |
+
missing_params=missing_params,
|
567 |
+
params_to_ask=params_to_ask,
|
568 |
+
max_params=max_params,
|
569 |
+
project_locale=project.default_locale,
|
570 |
+
unanswered_params=session.unanswered_parameters
|
571 |
)
|
572 |
param_question = await llm_generate(session, param_prompt, user_input)
|
573 |
return _trim_response(param_question)
|
|
|
578 |
except Exception as e:
|
579 |
log_error("❌ Error in handle_new_message", e)
|
580 |
return "Bir hata oluştu. Lütfen tekrar deneyin."
|
581 |
+
|
582 |
async def handle_parameter_followup(session: Session, user_input: str) -> str:
|
583 |
"""Handle parameter collection followup - for WebSocket"""
|
584 |
try:
|
|
|
589 |
|
590 |
intent_config = session.intent_config
|
591 |
|
592 |
+
# Get project config and version
|
593 |
project = next((p for p in cfg.projects if p.name == session.project_name), None)
|
594 |
if not project:
|
595 |
return "Proje konfigürasyonu bulunamadı."
|
596 |
|
597 |
+
version = session.get_version_config()
|
598 |
+
if not version:
|
599 |
+
return "Versiyon konfigürasyonu bulunamadı."
|
600 |
+
|
601 |
# Try to extract parameters from user message
|
602 |
param_prompt = f"""
|
603 |
Extract parameters from user message: "{user_input}"
|
|
|
618 |
|
619 |
# Check what parameters are still missing
|
620 |
missing_params = [
|
621 |
+
p.name for p in intent_config.parameters # p.name olmalı, sadece p değil
|
622 |
if p.required and p.variable_name not in session.variables
|
623 |
]
|
624 |
|
|
|
627 |
return await _execute_api_call(session, intent_config)
|
628 |
else:
|
629 |
# Still need more parameters
|
630 |
+
cfg = ConfigProvider.get()
|
631 |
+
collection_config = cfg.global_config.llm_provider.settings.get("parameter_collection_config", {})
|
632 |
+
max_params = collection_config.get("max_params_per_question", 2)
|
633 |
+
|
634 |
+
# Decide which parameters to ask
|
635 |
+
params_to_ask = missing_params[:max_params]
|
636 |
+
|
637 |
param_prompt = build_parameter_prompt(
|
638 |
+
version=version,
|
639 |
+
intent_config=intent_config,
|
640 |
+
chat_history=session.chat_history,
|
641 |
+
collected_params=session.variables,
|
642 |
+
missing_params=missing_params,
|
643 |
+
params_to_ask=params_to_ask,
|
644 |
+
max_params=max_params,
|
645 |
+
project_locale=project.default_locale,
|
646 |
+
unanswered_params=session.unanswered_parameters
|
647 |
)
|
648 |
param_question = await llm_generate(session, param_prompt, user_input)
|
649 |
return _trim_response(param_question)
|
|
|
652 |
log_error("❌ Error in handle_parameter_followup", e)
|
653 |
session.reset_flow()
|
654 |
return "Bir hata oluştu. Lütfen tekrar deneyin."
|
655 |
+
|
656 |
def get_user_friendly_error(error_type: str, context: dict = None) -> str:
|
657 |
"""Get user-friendly error messages"""
|
658 |
error_messages = {
|