File size: 2,701 Bytes
c8c21cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import re
from typing import Any, Dict, List

def classify_message(message: str) -> str:
    """Classify message type using LangChain approach."""
    message_lower = message.lower().strip()
    
    # Programming-related keywords
    code_keywords = [
        "write code", "python", "function", "class", "algorithm", 
        "programming", "code", "script", "loop", "variable", "import",
        "def ", "return", "if ", "for ", "while ", "try:", "except:",
        "list", "dict", "array", "dataframe", "numpy", "pandas"
    ]
    
    # Conversational keywords
    conversation_keywords = [
        "hi", "hello", "how are you", "what's up", "good morning",
        "good afternoon", "good evening", "bye", "goodbye", "thank you",
        "thanks", "help", "who are you", "what can you do"
    ]
    
    # Check for code-related content
    if any(keyword in message_lower for keyword in code_keywords):
        return "code"
    
    # Check for conversational content
    if any(keyword in message_lower for keyword in conversation_keywords):
        return "conversation"
    
    # Check for question marks or short messages
    if message_lower.endswith("?") or len(message_lower.split()) <= 5:
        return "conversation"
    
    # Default to conversation for ambiguous cases
    return "conversation"


def process_response(raw_response: str, response_type: str) -> Dict[str, Any]:
    """Process and format the model's response."""
    if response_type == "conversation":
        return {"response": raw_response.strip()}
    elif response_type == "code":
        code_match = re.search(r"```python\n(.*?)\n```", raw_response, re.DOTALL)
        if code_match:
            return {"generated_code": code_match.group(1).strip()}
        return {"generated_code": raw_response.strip()}
    elif response_type == "explanation":
        explanation = re.sub(r"```python\n.*?\n```", "", raw_response, flags=re.DOTALL).strip()
        return {"explanation": explanation}
    else:  # "both"
        code = None
        explanation = raw_response
        code_match = re.search(r"```python\n(.*?)\n```", raw_response, re.DOTALL)
        if code_match:
            code = code_match.group(1).strip()
            explanation = re.sub(r"```python\n.*?\n```", "", raw_response, flags=re.DOTALL).strip()
        explanation = re.sub(r"\[EXPLANATION\]\s*", "", explanation, flags=re.IGNORECASE).strip()
        explanation = re.sub(r"\[CODE\]\s*", "", explanation, flags=re.IGNORECASE).strip()
        return {
            "response": raw_response,
            "generated_code": code,
            "explanation": explanation
        }