Sgridda commited on
Commit
4eca884
·
1 Parent(s): 798c1c2

updated model config and added logs

Browse files
Files changed (1) hide show
  1. main.py +38 -7
main.py CHANGED
@@ -85,20 +85,31 @@ def run_ai_inference(diff: str) -> str:
85
  if not model or not tokenizer:
86
  raise RuntimeError("Model is not loaded.")
87
 
 
88
  messages = [
89
  {
90
  "role": "system",
91
- "content": """You are an expert code reviewer. Your task is to analyze a pull request diff and provide constructive feedback.\nAnalyze the provided diff and identify potential issues, suggest improvements, or point out good practices.\n\nIMPORTANT: Respond with a JSON array of comment objects. Each object must have three fields: 'file_path', 'line_number', and 'comment_text'.\nThe 'file_path' should be the full path of the file being changed.\nThe 'line_number' must be an integer corresponding to the line number in the *new* version of the file where the comment applies.\nThe 'comment_text' should be your concise and clear review comment.\n\nExample response format:\n[\n {\n "file_path": "src/utils/helpers.py",\n "line_number": 42,\n "comment_text": "This function could be simplified by using a list comprehension."\n }\n]\n\nDo not add any introductory text or explanations outside of the JSON array.\n"""
92
  },
93
  {
94
  "role": "user",
95
- "content": f"Here is the diff to review:\n\n```diff\n{diff}\n```"
96
  }
97
  ]
98
 
99
  inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
100
 
101
- outputs = model.generate(inputs, max_new_tokens=1024, do_sample=False, top_k=50, num_return_sequences=1, eos_token_id=tokenizer.eos_token_id)
 
 
 
 
 
 
 
 
 
 
102
 
103
  response_text = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
104
  return response_text.strip()
@@ -109,10 +120,15 @@ def parse_ai_response(response_text: str) -> list[ReviewComment]:
109
  """
110
  print(f"Raw AI Response:\n---\n{response_text}\n---")
111
 
112
- json_match = re.search(r'\[.*\]', response_text, re.DOTALL)
113
  if not json_match:
114
  print("Warning: Could not find a JSON array in the AI response.")
115
- return []
 
 
 
 
 
116
 
117
  json_string = json_match.group(0)
118
 
@@ -123,7 +139,12 @@ def parse_ai_response(response_text: str) -> list[ReviewComment]:
123
  except (json.JSONDecodeError, TypeError, KeyError) as e:
124
  print(f"Error parsing JSON from AI response: {e}")
125
  print(f"Invalid JSON string: {json_string}")
126
- return []
 
 
 
 
 
127
 
128
  # ----------------------------
129
  # 6. The API Endpoint
@@ -134,13 +155,23 @@ async def get_code_review(request: ReviewRequest):
134
  if not request.diff:
135
  raise HTTPException(status_code=400, detail="Diff content cannot be empty.")
136
 
 
 
 
 
137
  try:
 
138
  ai_response_text = run_ai_inference(request.diff)
 
 
 
139
  parsed_comments = parse_ai_response(ai_response_text)
 
 
140
  return ReviewResponse(comments=parsed_comments)
141
 
142
  except Exception as e:
143
- print(f"An unexpected error occurred: {e}")
144
  raise HTTPException(status_code=500, detail="An internal error occurred while processing the review.")
145
 
146
  # ----------------------------
 
85
  if not model or not tokenizer:
86
  raise RuntimeError("Model is not loaded.")
87
 
88
+ # Simplified, shorter prompt for faster inference
89
  messages = [
90
  {
91
  "role": "system",
92
+ "content": "You are a code reviewer. Analyze the diff and return a JSON array of comments with file_path, line_number, and comment_text fields. Be concise."
93
  },
94
  {
95
  "role": "user",
96
+ "content": f"Review this diff:\n{diff[:1000]}" # Limit diff size for faster processing
97
  }
98
  ]
99
 
100
  inputs = tokenizer.apply_chat_template(messages, add_generation_prompt=True, return_tensors="pt")
101
 
102
+ # Optimized generation parameters for speed
103
+ outputs = model.generate(
104
+ inputs,
105
+ max_new_tokens=256, # Reduced from 1024
106
+ do_sample=False,
107
+ temperature=0.1, # Lower temperature for more focused output
108
+ num_return_sequences=1,
109
+ eos_token_id=tokenizer.eos_token_id,
110
+ pad_token_id=tokenizer.eos_token_id,
111
+ use_cache=True # Enable KV cache for faster generation
112
+ )
113
 
114
  response_text = tokenizer.decode(outputs[0][len(inputs[0]):], skip_special_tokens=True)
115
  return response_text.strip()
 
120
  """
121
  print(f"Raw AI Response:\n---\n{response_text}\n---")
122
 
123
+ json_match = re.search(r'\[.*?\]', response_text, re.DOTALL)
124
  if not json_match:
125
  print("Warning: Could not find a JSON array in the AI response.")
126
+ # Return a simple fallback comment
127
+ return [ReviewComment(
128
+ file_path="unknown",
129
+ line_number=1,
130
+ comment_text="AI review completed - no specific issues found."
131
+ )]
132
 
133
  json_string = json_match.group(0)
134
 
 
139
  except (json.JSONDecodeError, TypeError, KeyError) as e:
140
  print(f"Error parsing JSON from AI response: {e}")
141
  print(f"Invalid JSON string: {json_string}")
142
+ # Return a simple fallback comment
143
+ return [ReviewComment(
144
+ file_path="unknown",
145
+ line_number=1,
146
+ comment_text="AI review completed - response format issue."
147
+ )]
148
 
149
  # ----------------------------
150
  # 6. The API Endpoint
 
155
  if not request.diff:
156
  raise HTTPException(status_code=400, detail="Diff content cannot be empty.")
157
 
158
+ import time
159
+ start_time = time.time()
160
+ print(f"Starting review request at {start_time}")
161
+
162
  try:
163
+ print("Running AI inference...")
164
  ai_response_text = run_ai_inference(request.diff)
165
+ print(f"AI inference completed in {time.time() - start_time:.2f} seconds")
166
+
167
+ print("Parsing AI response...")
168
  parsed_comments = parse_ai_response(ai_response_text)
169
+ print(f"Total processing time: {time.time() - start_time:.2f} seconds")
170
+
171
  return ReviewResponse(comments=parsed_comments)
172
 
173
  except Exception as e:
174
+ print(f"An unexpected error occurred after {time.time() - start_time:.2f} seconds: {e}")
175
  raise HTTPException(status_code=500, detail="An internal error occurred while processing the review.")
176
 
177
  # ----------------------------