DEADLOCK007X commited on
Commit
4980b54
·
1 Parent(s): 35d31e1

Refine evaluation prompt and extraction for DeepSeek instruct model

Browse files
Files changed (1) hide show
  1. tinyllama_inference.py +13 -16
tinyllama_inference.py CHANGED
@@ -15,7 +15,7 @@ def load_model():
15
  return tokenizer, model
16
 
17
  def evaluate_code(question, code):
18
- # Refined prompt with explicit rating criteria.
19
  prompt = f"""You are an expert code evaluator.
20
  Evaluate the following solution for the given problem.
21
  Rate the solution as follows:
@@ -38,27 +38,24 @@ Your response:"""
38
  inputs = tokenizer(prompt, return_tensors="pt")
39
  outputs = model.generate(
40
  **inputs,
41
- max_new_tokens=120, # Increase token allowance for a complete evaluation
42
- temperature=0.1, # A low temperature for more deterministic output
43
  pad_token_id=tokenizer.eos_token_id,
44
- do_sample=True # Enable sampling to allow some creativity
45
  )
46
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
- print("Raw model response:", response_text) # Debug: inspect raw output
48
 
49
- # Extract all JSON objects using non-greedy regex and select the one with expected keys
50
  matches = re.findall(r'\{.*?\}', response_text)
51
- result = None
52
- for m in matches:
53
  try:
54
- temp = json.loads(m)
55
- if isinstance(temp, dict) and "stars" in temp and "feedback" in temp:
56
- result = temp
57
- break
58
- except Exception:
59
- continue
60
- if result is None:
61
- result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract valid JSON from AI response."}
62
 
63
  return result
64
 
 
15
  return tokenizer, model
16
 
17
  def evaluate_code(question, code):
18
+ # Refined prompt with explicit instructions and a "Your response:" line.
19
  prompt = f"""You are an expert code evaluator.
20
  Evaluate the following solution for the given problem.
21
  Rate the solution as follows:
 
38
  inputs = tokenizer(prompt, return_tensors="pt")
39
  outputs = model.generate(
40
  **inputs,
41
+ max_new_tokens=100, # Increase token allowance if needed
42
+ temperature=0.2, # Allow some creativity, but mostly deterministic
43
  pad_token_id=tokenizer.eos_token_id,
44
+ do_sample=True
45
  )
46
  response_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
47
+ print("Raw model response:", response_text) # Debug output
48
 
49
+ # Extract all JSON objects (non-greedy) and use the last one
50
  matches = re.findall(r'\{.*?\}', response_text)
51
+ if matches:
52
+ json_text = matches[-1] # Pick the last JSON block
53
  try:
54
+ result = json.loads(json_text)
55
+ except Exception as e:
56
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to parse AI response."}
57
+ else:
58
+ result = {"stars": 0, "feedback": "Evaluation failed. Unable to extract JSON from AI response."}
 
 
 
59
 
60
  return result
61