trippyrocks commited on
Commit
db3dd09
·
verified ·
1 Parent(s): 671b58b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -45
app.py CHANGED
@@ -18,64 +18,57 @@ def my_custom_tool(arg1:str, arg2:int)-> str: #it's import to specify the return
18
  """
19
  return "What magic will you build ?"
20
 
21
- @tool
22
- def extended_calculator(operation: str) -> str:
23
- """An extended calculator that safely evaluates math expressions.
24
-
25
- Supports:
26
- - Basic operations: +, -, *, /, and parentheses.
27
- - Exponentiation: using either '^' or '**'.
28
- - Math functions: sqrt, sin, cos, tan, log, exp.
29
-
30
  Args:
31
- operation: A string mathematical expression (e.g., "2 + 2", "sqrt(16)", "2^3").
32
-
33
  Returns:
34
- str: The result of the calculation or an error message.
35
  """
36
  try:
37
- # Replace caret '^' with Python's exponentiation operator '**'
38
- operation = operation.replace("^", "**")
39
-
40
- # Define allowed names for math functions and constants.
41
- safe_env = {
42
- "sqrt": math.sqrt,
43
- "sin": math.sin,
44
- "cos": math.cos,
45
- "tan": math.tan,
46
- "log": math.log, # natural logarithm by default
47
- "exp": math.exp,
48
- "pi": math.pi,
49
- "e": math.e,
50
- "abs": abs,
51
- "pow": pow,
52
  }
 
 
 
 
 
 
 
 
 
 
53
 
54
- # Use a regex to allow only safe characters (digits, letters, operators, parentheses, commas, and periods)
55
- # This helps reject inputs with disallowed symbols.
56
- if not re.match(r'^[0-9a-zA-Z\s+\-*/().,]+$', operation):
57
- return "Error: Expression contains invalid characters."
 
 
 
 
 
 
 
58
 
59
- # Evaluate the expression using the restricted environment.
60
- result = eval(operation, {"__builtins__": None}, safe_env)
61
- return f"Result: {result}"
62
 
63
  except Exception as e:
64
- return f"Error calculating {operation}: {str(e)}"
65
 
66
  # Example usage:
67
  if __name__ == "__main__":
68
- expressions = [
69
- "2 + 2",
70
- "3 * (4 + 5)",
71
- "2^3", # using '^' for exponentiation
72
- "sqrt(16)",
73
- "sin(pi/2)",
74
- "log(e)"
75
- ]
76
 
77
- for expr in expressions:
78
- print(f"{expr} = {extended_calculator(expr)}")
79
  @tool
80
  def get_current_time_in_timezone(timezone: str) -> str:
81
  """A tool that fetches the current local time in a specified timezone.
 
18
  """
19
  return "What magic will you build ?"
20
 
21
+ def duckduckgo_search(query: str) -> str:
22
+ """
23
+ A simple search tool that queries DuckDuckGo's Instant Answer API and returns a result snippet.
24
+
 
 
 
 
 
25
  Args:
26
+ query (str): The search query string.
27
+
28
  Returns:
29
+ str: A summary from DuckDuckGo or an error message.
30
  """
31
  try:
32
+ url = "https://api.duckduckgo.com/"
33
+ params = {
34
+ "q": query,
35
+ "format": "json",
36
+ "no_html": 1,
37
+ "skip_disambig": 1,
 
 
 
 
 
 
 
 
 
38
  }
39
+ response = requests.get(url, params=params)
40
+ if response.status_code != 200:
41
+ return f"Error: Received status code {response.status_code} from DuckDuckGo."
42
+
43
+ data = response.json()
44
+
45
+ # Use the AbstractText if available as a summary.
46
+ abstract_text = data.get("AbstractText", "")
47
+ if abstract_text:
48
+ return f"Result: {abstract_text}"
49
 
50
+ # If no abstract is provided, try returning the first related topic's text.
51
+ related_topics = data.get("RelatedTopics", [])
52
+ for topic in related_topics:
53
+ # Some topics might be nested; check both levels.
54
+ if isinstance(topic, dict):
55
+ if "Text" in topic:
56
+ return f"Result: {topic['Text']}"
57
+ if "Topics" in topic:
58
+ for subtopic in topic["Topics"]:
59
+ if "Text" in subtopic:
60
+ return f"Result: {subtopic['Text']}"
61
 
62
+ return "No results found."
 
 
63
 
64
  except Exception as e:
65
+ return f"Error performing search: {str(e)}"
66
 
67
  # Example usage:
68
  if __name__ == "__main__":
69
+ search_query = "Python programming"
70
+ print(duckduckgo_search(search_query))
 
 
 
 
 
 
71
 
 
 
72
  @tool
73
  def get_current_time_in_timezone(timezone: str) -> str:
74
  """A tool that fetches the current local time in a specified timezone.