File size: 3,421 Bytes
e8a6607
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
65
66
67
68
# from llama_cpp import Llama
import logging
from .model_manager import get_model

logger = logging.getLogger(__name__)

def gpt_chat_process(prompt: str, msg_session: str, user_id: str):
    try:
        model = get_model()
        if not model:
            logger.error("Model instance could not be retrieved")
            return {'error': 'Model unavailable', 'user_id': user_id}

        response = model.create_chat_completion(
            messages=[
                {
                    "role": "system",
                    "content": (
                        "You are Qwen2.5-Coder, a specialized AI coding assistant. "
                        "Your task is to analyze the user request and apply these strict rules:\n\n"
                        
                        "1. If the request asks about who developed you, who your creator is, or asks for "
                        "developer profile information, you MUST reply with exactly this message format:\n"
                        "\"This coding assistant was developed by [Subeesh Palamadathil]. You can find more information "
                        "and connect on GitHub: [GitHub Profile](https://github.com/Subeesh4020) and "
                        "LinkedIn: [LinkedIn Profile](https://www.linkedin.com/in/subeesh-palamadathil-170249193/).\"\n\n"
            
                        "1. If the request is a general greeting, conversational chit-chat, or entirely unrelated to "
                        "software development, programming, or coding, you MUST reply with exactly this sentence: "
                        "'Please ask any coding related questions. I am a coding assistant.' Do not provide any code.\n\n"
                        "2. If the request is related to programming, provide only clean, efficient, and well-commented "
                        "code wrapped in a standard markdown code block. Do not include conversational filler or explanations."
                    )
                    # "content": (
                    #     "You are an expert software engineer. Provide only clean, efficient, "
                    #     "and well-commented code based on the user request. Do not include "
                    #     "any introductory or concluding conversational explanations. Output "
                    #     "the response wrapped in a standard markdown code block."
                    # )
                },
                {
                    "role": "user",
                    # Example prompt: "Write a typescript function to validate email"
                    "content": f"{prompt}" 
                }
            ],
            temperature=0.2, # Lower temperature is critical for accurate, deterministic code
            max_tokens=1000 # Increased tokens since code files are larger than social posts
        )

        choices = response.get("choices", [])
        if not choices:
            raise ValueError("Model returned an empty choices array")

        # Accessing the message content safely
        message = choices[0].get("message", {})
        content = message.get("content", "")

        return { 
            'result': content.strip(), 
            'msg_session': msg_session,
            'user_id': user_id 
        }

    except Exception as e:
        logger.exception(f"Failed to generate code for user {user_id}")
        return {'error': 'Code generation failed', 'user_id': user_id}