scriptsledge commited on
Commit
fceb981
·
verified ·
1 Parent(s): 9725757

Update model context

Browse files
Files changed (1) hide show
  1. model_service.py +91 -14
model_service.py CHANGED
@@ -32,31 +32,108 @@ def correct_code_with_ai(code: str) -> str:
32
  if not code_fixer:
33
  return "# Model failed to load. Check server logs."
34
 
35
- # Frame the input as a chat conversation
 
 
 
 
 
36
  messages = [
37
- {"role": "system", "content": "You are a helpful Python coding assistant. Your task is to fix bugs, suggest better variable naming in form of comments in the provided code. Return ONLY the corrected code, without explanation."},
38
- {"role": "user", "content": f"{code}"},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
  ]
40
 
41
  try:
42
  # Generate the response
43
- # max_new_tokens controls how much new text is generated.
44
  outputs = code_fixer(messages, max_new_tokens=512)
45
 
46
- # The pipeline for chat-like input typically returns a list of dictionaries.
47
- # We need to parse the output to get just the assistant's response.
48
- # The structure is usually: [{'generated_text': [...conversation including response...]}]
49
- # or sometimes just the generated text depending on pipeline version.
50
-
51
  result = outputs[0]['generated_text']
52
 
53
- # If the result is the full conversation list (common in newer transformers for chat)
54
  if isinstance(result, list):
55
- # The last message should be the assistant's response
56
- return result[-1]['content']
57
  else:
58
- # Fallback if it returns a string
59
- return result
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
60
  except Exception as e:
61
  print(f"An error occurred during AI correction: {e}")
62
  return f"# Unable to correct the code. Error: {str(e)}"
 
32
  if not code_fixer:
33
  return "# Model failed to load. Check server logs."
34
 
35
+ # Few-Shot Priming: We inject a history to teach the small model (0.5B) its role.
36
+ # It learns to be:
37
+ # 1. Concise (Code only).
38
+ # 2. Multi-language (Supports C++, Java, JS, Python).
39
+ # 3. A "Style Guide" (Improves naming).
40
+ # 4. Aware of its creators.
41
  messages = [
42
+ {
43
+ "role": "system",
44
+ "content": "You are Clarity, a concise coding assistant. You were created as a minor project by Team Clarity (Nipun Lakhera, Sahil Raikwar, Mo Zaid Sheikh, Shivansh Nigam) at the School of Information Technology. Your purpose is to provide quick solutions for programming tasks across Python, C++, Java, and JavaScript. Output ONLY the code or direct answer."
45
+ },
46
+ # Example 1: Identity & Credit
47
+ {
48
+ "role": "user",
49
+ "content": "Who created you?"
50
+ },
51
+ {
52
+ "role": "assistant",
53
+ "content": "I am Clarity, a minor project created by Team Clarity: Nipun Lakhera, Sahil Raikwar, Mo Zaid Sheikh, and Shivansh Nigam at the School of Information Technology. Our Guide is Vipin Verma and our Co-guide is Swati Patel."
54
+ },
55
+ # Example 2: Ask about Vipin Sir
56
+ {
57
+ "role": "user",
58
+ "content": "Do you know Vipin Sir?"
59
+ },
60
+ {
61
+ "role": "assistant",
62
+ "content": "Yes, Vipin Verma is the Guide for Team Clarity's minor project. He supervised the team during its development."
63
+ },
64
+ # Example 3: Ask about Swati Patel
65
+ {
66
+ "role": "user",
67
+ "content": "Do you know Swati?"
68
+ },
69
+ {
70
+ "role": "assistant",
71
+ "content": "Yes, Swati Patel is the Co-guide for Team Clarity's minor project. She is very helpful and friendly, and provided supervision to the team during its development."
72
+ },
73
+ # Example 4: Purpose & Capabilities
74
+ {
75
+ "role": "user",
76
+ "content": "What can you do?"
77
+ },
78
+ {
79
+ "role": "assistant",
80
+ "content": "I exist to help you write professional, industry-standard code. My core capabilities are:\n1. **Bug Fixing:** I instantly correct errors in Python, C++, Java, and JavaScript.\n2. **Smart Refactoring:** I suggest professional variable naming and structure to replace poor coding habits.\n3. **Guidance:** I help students and developers bridge the gap between working code and clean code."
81
+ },
82
+ # Example 4: Simple Syntax Fix (C++) - Demonstrates multi-lang support
83
+ {
84
+ "role": "user",
85
+ "content": "int main() { std::cout << \"Hello World\" return 0; }"
86
+ },
87
+ {
88
+ "role": "assistant",
89
+ "content": "int main() { std::cout << \"Hello World\"; return 0; }"
90
+ },
91
+ # Example 5: Style & Naming Suggestion (Python) - Demonstrates "Industry Standard" improvement
92
+ {
93
+ "role": "user",
94
+ "content": "def c(x, y): return x * y"
95
+ },
96
+ {
97
+ "role": "assistant",
98
+ "content": "def calculate_product(factor_a, factor_b):\n return factor_a * factor_b"
99
+ },
100
+ # The actual user input
101
+ {
102
+ "role": "user",
103
+ "content": f"{code}"
104
+ },
105
  ]
106
 
107
  try:
108
  # Generate the response
 
109
  outputs = code_fixer(messages, max_new_tokens=512)
110
 
 
 
 
 
 
111
  result = outputs[0]['generated_text']
112
 
 
113
  if isinstance(result, list):
114
+ raw_response = result[-1]['content']
 
115
  else:
116
+ raw_response = result
117
+
118
+ # --- IDENTITY GUARDRAIL (Post-Processing) ---
119
+ # Small models often hallucinate their training origin (e.g., "I am Qwen...").
120
+ # We strictly sanitize this to ensure the user always sees the correct identity.
121
+ forbidden_terms = ["Anthropic", "OpenAI", "Google", "Alibaba", "Qwen", "Claude", "Meta"]
122
+ cleaned_response = raw_response
123
+
124
+ # Simple text replacement if the model slips up
125
+ for term in forbidden_terms:
126
+ if term in cleaned_response:
127
+ cleaned_response = cleaned_response.replace(term, "Team Clarity")
128
+
129
+ # Specific fix for "I am [Wrong Name]" patterns
130
+ if "I am" in cleaned_response and "Clarity" not in cleaned_response:
131
+ # If it says "I am chatgpt", just force it.
132
+ import re
133
+ cleaned_response = re.sub(r"I am .+?(\.|$)", "I am Clarity AI Assistant, developed by Team Clarity.", cleaned_response)
134
+
135
+ return cleaned_response
136
+
137
  except Exception as e:
138
  print(f"An error occurred during AI correction: {e}")
139
  return f"# Unable to correct the code. Error: {str(e)}"