pxrth1308 commited on
Commit
126edbe
·
verified ·
1 Parent(s): 81917a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -19,6 +19,53 @@ class BasicAgent:
19
  print(f"Agent returning fixed answer: {fixed_answer}")
20
  return fixed_answer
21
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def run_and_submit_all( profile: gr.OAuthProfile | None):
23
  """
24
  Fetches all questions, runs the BasicAgent on them, submits all answers,
 
19
  print(f"Agent returning fixed answer: {fixed_answer}")
20
  return fixed_answer
21
 
22
+
23
+
24
+ def calculator_tool(expression: str) -> str:
25
+ """Evaluates complex mathematical expressions. Input should be a single string expression, e.g., '10 * (2 + 3) / 5'."""
26
+ print(f"--- TOOL CALL: Calculator with expression: {expression} ---")
27
+ try:
28
+ # Use sympify for safe and robust parsing/evaluation of math
29
+ result = sympify(expression).evalf()
30
+ return str(result)
31
+ except Exception as e:
32
+ return f"ERROR: Could not evaluate expression '{expression}'. Error: {e}"
33
+
34
+ def search_tool(query: str) -> str:
35
+ """Performs a web search using Serper.dev (Google results)."""
36
+ print(f"--- TOOL CALL: Search with query: {query} ---")
37
+
38
+ api_key = os.environ.get("SERPER_API_KEY")
39
+ if not api_key:
40
+ return "ERROR: SERPER_API_KEY environment variable not set."
41
+
42
+ try:
43
+ url = "https://google.serper.dev/search"
44
+ headers = {
45
+ "X-API-KEY": api_key,
46
+ "Content-Type": "application/json"
47
+ }
48
+ payload = {"q": query}
49
+
50
+ resp = requests.post(url, headers=headers, json=payload, timeout=10)
51
+ resp.raise_for_status()
52
+ data = resp.json()
53
+
54
+ # Extract snippets from top results
55
+ if "organic" in data:
56
+ snippets = [item.get("snippet", "") for item in data["organic"][:3]]
57
+ return " | ".join(snippets) if snippets else "No results found."
58
+ else:
59
+ return "No search results found."
60
+ except Exception as e:
61
+ return f"ERROR: Search request failed. {e}"
62
+
63
+
64
+ # A dictionary to map tool names to the actual functions
65
+ AVAILABLE_TOOLS = {
66
+ "calculator": calculator_tool,
67
+ "search": search_tool,
68
+ }
69
  def run_and_submit_all( profile: gr.OAuthProfile | None):
70
  """
71
  Fetches all questions, runs the BasicAgent on them, submits all answers,