zenaight commited on
Commit ·
9dfa030
1
Parent(s): c452b29
Add end chat detection functionality in AI chat
Browse files- Introduced a new asynchronous function `detect_end_chat` to identify user intent to end the chat session based on specific phrases.
- Integrated this function into the chat state graph, allowing for a graceful termination of the session with a user-friendly response.
- This enhancement aims to improve user experience by providing clear communication when the chat session is concluded.
- ai_chat.py +16 -1
ai_chat.py
CHANGED
|
@@ -369,16 +369,31 @@ async def extract_and_search_properties(state):
|
|
| 369 |
await end_session(state["session_id"])
|
| 370 |
return state
|
| 371 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 372 |
# --- Build LangGraph ---
|
| 373 |
graph = StateGraph(ChatState)
|
| 374 |
graph.add_node("persona_update", RunnableLambda(extract_and_update_persona))
|
| 375 |
graph.add_node("intent_update", RunnableLambda(extract_and_update_intent))
|
| 376 |
graph.add_node("property_search", RunnableLambda(extract_and_search_properties))
|
|
|
|
| 377 |
graph.add_node("chat", RunnableLambda(chat_with_session_memory))
|
| 378 |
graph.set_entry_point("persona_update")
|
| 379 |
graph.add_edge("persona_update", "intent_update")
|
| 380 |
graph.add_edge("intent_update", "property_search")
|
| 381 |
-
graph.add_edge("property_search", "
|
|
|
|
| 382 |
graph.add_edge("chat", END)
|
| 383 |
chat_graph = graph.compile()
|
| 384 |
|
|
|
|
| 369 |
await end_session(state["session_id"])
|
| 370 |
return state
|
| 371 |
|
| 372 |
+
async def detect_end_chat(state):
|
| 373 |
+
"""
|
| 374 |
+
Detect if the user wants to end the chat session.
|
| 375 |
+
"""
|
| 376 |
+
user_message = state["user_message"].lower()
|
| 377 |
+
session_id = state["session_id"]
|
| 378 |
+
|
| 379 |
+
if any(phrase in user_message for phrase in ["thank you", "thanks", "bye", "goodbye", "end chat"]):
|
| 380 |
+
await end_session(session_id)
|
| 381 |
+
return {"response": "Thanks for chatting! I've ended this session. Goodbye!"}
|
| 382 |
+
|
| 383 |
+
return {"response": None}
|
| 384 |
+
|
| 385 |
# --- Build LangGraph ---
|
| 386 |
graph = StateGraph(ChatState)
|
| 387 |
graph.add_node("persona_update", RunnableLambda(extract_and_update_persona))
|
| 388 |
graph.add_node("intent_update", RunnableLambda(extract_and_update_intent))
|
| 389 |
graph.add_node("property_search", RunnableLambda(extract_and_search_properties))
|
| 390 |
+
graph.add_node("exit_check", RunnableLambda(detect_end_chat))
|
| 391 |
graph.add_node("chat", RunnableLambda(chat_with_session_memory))
|
| 392 |
graph.set_entry_point("persona_update")
|
| 393 |
graph.add_edge("persona_update", "intent_update")
|
| 394 |
graph.add_edge("intent_update", "property_search")
|
| 395 |
+
graph.add_edge("property_search", "exit_check")
|
| 396 |
+
graph.add_edge("exit_check", "chat")
|
| 397 |
graph.add_edge("chat", END)
|
| 398 |
chat_graph = graph.compile()
|
| 399 |
|