Style / conversation.py
nexusbert's picture
modularization
ab2012f
from config import CLOTHING_TYPES
conversation_memory = {}
def get_conversation_context(session_id: str) -> dict:
if session_id not in conversation_memory:
conversation_memory[session_id] = {
"messages": [],
"context": {}
}
return conversation_memory[session_id]
def update_context(session_id: str, message: str, response_data: dict):
conv = conversation_memory[session_id]
conv["messages"].append({"user": message, "assistant": response_data.get("response", "")})
if len(conv["messages"]) > 10:
conv["messages"] = conv["messages"][-10:]
if "color" in response_data:
conv["context"]["last_color"] = response_data["color"]
if "item" in response_data:
conv["context"]["last_item"] = response_data["item"]
if "colors" in response_data:
conv["context"]["last_colors"] = response_data["colors"]
if "items" in response_data:
conv["context"]["last_items"] = response_data["items"]
def enhance_message_with_context(message: str, context: dict) -> str:
message_lower = message.lower()
if "what about" in message_lower or "how about" in message_lower or "and" in message_lower:
if context.get("last_color"):
if not any(c in message_lower for c in ["color", "red", "blue", "green", "black", "white", "brown"]):
message = message + f" with {context['last_color']}"
if context.get("last_item"):
if not any(item in message_lower for item in CLOTHING_TYPES):
message = message + f" {context['last_item']}"
return message