kpalatel commited on
Commit
49405a0
·
verified ·
1 Parent(s): c488ee5

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +34 -16
agent.py CHANGED
@@ -1,4 +1,5 @@
1
  import os
 
2
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
3
 
4
  @tool
@@ -9,41 +10,58 @@ def calculate_math(expression: str) -> str:
9
  """
10
  try:
11
  allowed_chars = "0123456789+-*/(). "
12
- if all(c in allowed_chars for c in expression):
13
- return str(eval(expression))
 
14
  return "Error: Invalid characters in math expression."
15
  except Exception as e:
16
  return f"Error executing calculation: {str(e)}"
17
 
18
  class CourseEvaluationAgent:
19
  def __init__(self):
20
- print("[Agent System] Booting Engine...")
21
 
22
- # In this environment version, InferenceClientModel handles the Hub implementation.
23
- # It reads your HF_TOKEN secret natively behind the scenes.
24
  self.model = InferenceClientModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
25
 
26
- # Pass the fully instantiated concrete model to the model positional argument
27
  self.agent = CodeAgent(
28
  tools=[DuckDuckGoSearchTool(), calculate_math],
29
  model=self.model,
30
- max_steps=5, # Prevents execution hang ups or infinite loops
31
  verbosity_level=1
32
  )
33
 
34
- extra_instructions = (
35
- "\nCRITICAL INSTRUCTION: Be concise. When responding with your final answer, "
36
- "provide just the factual outcome or specific answer requested. "
37
- "Do not include unnecessary conversational filler."
 
 
 
 
 
38
  )
39
-
40
- if "system_prompt" in self.agent.prompt_templates:
41
- self.agent.prompt_templates["system_prompt"] += extra_instructions
42
 
43
  def run_task(self, question: str) -> str:
44
  try:
 
45
  output = self.agent.run(question)
46
- return str(output).strip()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
47
  except Exception as e:
48
  print(f"[Agent Error] Failed execution: {e}")
49
- return f"Error: {str(e)[:50]}"
 
1
  import os
2
+ import re
3
  from smolagents import CodeAgent, DuckDuckGoSearchTool, tool, InferenceClientModel
4
 
5
  @tool
 
10
  """
11
  try:
12
  allowed_chars = "0123456789+-*/(). "
13
+ cleaned = "".join(c for c in expression if c in allowed_chars)
14
+ if cleaned:
15
+ return str(eval(cleaned))
16
  return "Error: Invalid characters in math expression."
17
  except Exception as e:
18
  return f"Error executing calculation: {str(e)}"
19
 
20
  class CourseEvaluationAgent:
21
  def __init__(self):
22
+ print("[Agent System] Booting Upgraded Engine...")
23
 
24
+ # Use the highly capable Llama 3.3 70B model via serverless client
 
25
  self.model = InferenceClientModel(model_id="meta-llama/Llama-3.3-70B-Instruct")
26
 
27
+ # Initialize the CodeAgent
28
  self.agent = CodeAgent(
29
  tools=[DuckDuckGoSearchTool(), calculate_math],
30
  model=self.model,
31
+ max_steps=5,
32
  verbosity_level=1
33
  )
34
 
35
+ # STRICT PROMPT FIX: Completely override the prompt system template
36
+ # to guarantee the agent ONLY outputs the clean, final answer.
37
+ self.agent.prompt_templates["system_prompt"] = (
38
+ "You are an expert factual agent. You have access to a python code interpreter and web search.\n"
39
+ "CRITICAL RULES:\n"
40
+ "1. Run code or search to find the exact truth.\n"
41
+ "2. Your final response MUST contain ONLY the direct, raw answer text (e.g., '42', 'Paris', 'John Doe').\n"
42
+ "3. Absolutely DO NOT include conversational text like 'The answer is...', 'According to search...', or punctuation filler.\n"
43
+ "4. Be completely literal."
44
  )
 
 
 
45
 
46
  def run_task(self, question: str) -> str:
47
  try:
48
+ # Let the agent think and execute tools
49
  output = self.agent.run(question)
50
+ final_ans = str(output).strip()
51
+
52
+ # POST-PROCESSING CLEANER: If the model still outputs conversational fluff,
53
+ # we programmaticly clean common phrases before returning to the grader.
54
+ clean_patterns = [
55
+ r"^[Tt]he answer is\s*[:\-]?\s*",
56
+ r"^[Ff]inal [Aa]nswer\s*[:\-]?\s*",
57
+ r"^[\"']", # Strip starting quotes
58
+ r"[\"']$" # Strip ending quotes
59
+ ]
60
+ for pattern in clean_patterns:
61
+ final_ans = re.sub(pattern, "", final_ans)
62
+
63
+ return final_ans.strip()
64
+
65
  except Exception as e:
66
  print(f"[Agent Error] Failed execution: {e}")
67
+ return "Error calculating answer."