gaster game help response

parent 97fd9871
...@@ -92,20 +92,28 @@ class QueryHandler: ...@@ -92,20 +92,28 @@ class QueryHandler:
logger.warning(f"Error getting conversation context for {student_id}: {e}") logger.warning(f"Error getting conversation context for {student_id}: {e}")
return "لا يمكن الحصول على سياق المحادثة." return "لا يمكن الحصول على سياق المحادثة."
def classify_query_type(self, query: str, student_info: Dict[str, Any], student_id: str) -> str: def classify_query_type(self, query: str, student_info: Dict[str, Any], student_id: str) -> str:
"""Enhanced query classification using LLM with conversation context""" """
Enhanced query classification. It first checks for a specific, rule-based
pattern for 'game_help' and then falls back to the LLM for other cases.
"""
q_lower = query.lower()
if "game context:" in q_lower and "user query:" in q_lower:
logger.info("Query classified as 'game_help' by rule-based check.")
print("Query classified as 'game_help' by rule-based check.")
return "game_help"
if not self.openai_service.is_available(): if not self.openai_service.is_available():
return "specific_content" return "specific_content"
is_arabic: bool = student_info.get('is_arabic', True) is_arabic: bool = student_info.get('is_arabic', True)
grade: int = student_info.get('grade', 4) grade: int = student_info.get('grade', 4)
# Get recent conversation context
conversation_context = self.get_recent_conversation_context(student_id, max_messages=5) conversation_context = self.get_recent_conversation_context(student_id, max_messages=5)
q_lower = query.lower()
classification_prompt = f""" classification_prompt = f"""
صنف السؤال التالي إلى إحدى الفئات التالية، مع مراعاة سياق المحادثة الأخيرة: صنف السؤال التالي إلى إحدى الفئات التالية، مع مراعاة سياق المحادثة الأخيرة:
...@@ -113,9 +121,7 @@ class QueryHandler: ...@@ -113,9 +121,7 @@ class QueryHandler:
2. "overview" - أسئلة عن نظرة عامة على المنهج أو المحتوى الكامل 2. "overview" - أسئلة عن نظرة عامة على المنهج أو المحتوى الكامل
3. "navigation" - أسئلة عن وحدة أو مفهوم معين 3. "navigation" - أسئلة عن وحدة أو مفهوم معين
4. "specific_content" - أسئلة محددة عن موضوع علمي معين 4. "specific_content" - أسئلة محددة عن موضوع علمي معين
5. "game_help:" - أسئلة عن مساعدة او تفاعل في لعبة تعليمية بيكون شكل الرسالة كدا دايما
game context: <context text>
user query: <query text>
{conversation_context} {conversation_context}
السؤال الحالي: "{query}" السؤال الحالي: "{query}"
...@@ -127,12 +133,8 @@ class QueryHandler: ...@@ -127,12 +133,8 @@ class QueryHandler:
- إذا كان الطالب يتحدث عن موضوع علمي معين وسأل سؤال متعلق به، فهو "specific_content" - إذا كان الطالب يتحدث عن موضوع علمي معين وسأل سؤال متعلق به، فهو "specific_content"
- إذا كان السؤال غامض، اعتمد على السياق لتحديد القصد الحقيقي - إذا كان السؤال غامض، اعتمد على السياق لتحديد القصد الحقيقي
- مثال: إذا كان يتحدث عن النباتات وسأل "كيف تأكل؟" فهو يقصد تغذية النباتات وليس الطعام العادي - مثال: إذا كان يتحدث عن النباتات وسأل "كيف تأكل؟" فهو يقصد تغذية النباتات وليس الطعام العادي
- لو السؤال فيه الصيغة دي:
"game context:" وبعدها "user query:"، فالتصنيف الصحيح دايمًا يكون "game_help" مهما كان المحتوى
""" """
classification_prompt += "\n\nرد فقط بكلمة واحدة من الفئات المحددة أعلاه" classification_prompt += "\n\nرد فقط بكلمة واحدة من الفئات المحددة أعلاه"
try: try:
...@@ -144,24 +146,23 @@ class QueryHandler: ...@@ -144,24 +146,23 @@ class QueryHandler:
) )
classification: str = response.choices[0].message.content.strip().lower().strip('"').strip("'") classification: str = response.choices[0].message.content.strip().lower().strip('"').strip("'")
valid_classes = { valid_classes = {
"general_chat", "overview", "navigation", "specific_content", "game_help" "general_chat", "overview", "navigation", "specific_content"
} }
if classification in valid_classes: if classification in valid_classes:
logger.info(f"Query classified as: {classification} for query: '{query}'") logger.info(f"Query classified as: {classification} by LLM for query: '{query}'")
print(f"Query classified as: {classification} for query: '{query}'") print(f"Query classified as: {classification} by LLM for query: '{query}'")
return classification return classification
else: else:
logger.warning( logger.warning(
f"Unexpected classification '{classification}' for query '{query}', " f"Unexpected LLM classification '{classification}' for query '{query}', "
"defaulting to 'specific_content'" "defaulting to 'specific_content'"
) )
return "specific_content" return "specific_content"
except Exception as e: except Exception as e:
logger.warning(f"Error in query classification: {e}, defaulting to 'specific_content'") logger.warning(f"Error in LLM query classification: {e}, defaulting to 'specific_content'")
return "specific_content" return "specific_content"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment