Hanokh commited on
Commit
00b1b2b
·
verified ·
1 Parent(s): 7369929

tools approach

Browse files
Files changed (1) hide show
  1. app.py +44 -34
app.py CHANGED
@@ -6,7 +6,7 @@ import pandas as pd
6
 
7
  import re
8
  import requests
9
- from duckduckgo_search import DDGS
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
@@ -14,41 +14,51 @@ DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
17
-
18
-
19
- class BasicAgent:
20
- def __init__(self):
21
- print("BasicAgent initialized.")
22
- self.searcher = DDGS()
23
-
24
- def web_search(self, query: str, max_results: int = 3) -> str:
25
- """Perform a DuckDuckGo search and return snippets."""
26
  try:
27
- results = list(self.searcher.text(query, max_results=max_results))
28
- if not results:
29
- return "No search results found."
30
- # Concatenate top snippets
31
- snippets = [r.get("body", "") for r in results if "body" in r]
32
- return " ".join(snippets[:3])
33
- except Exception as e:
34
- return f"Search error: {e}"
35
-
36
- def __call__(self, question: str) -> str:
37
- print(f"Agent received question (first 50 chars): {question[:50]}...")
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
- q = question.strip()
40
-
41
- # If it looks like a math problem, handle locally
42
- if re.search(r"\d+\s*[\+\-\*\/]\s*\d+", q):
43
- try:
44
- return str(eval(q))
45
- except Exception:
46
- pass
47
-
48
- # Otherwise: do a web search
49
- print("Delegating to web search...")
50
- snippets = self.web_search(q, max_results=3)
51
- return snippets if snippets else "I could not find an answer."
 
 
 
 
 
52
 
53
  def run_and_submit_all( profile: gr.OAuthProfile | None):
54
  """
 
6
 
7
  import re
8
  import requests
9
+
10
 
11
  # (Keep Constants as is)
12
  # --- Constants ---
 
14
 
15
  # --- Basic Agent Definition ---
16
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
17
+ class MathTool:
18
+ def run(self, query):
 
 
 
 
 
 
 
19
  try:
20
+ return str(eval(query))
21
+ except Exception:
22
+ return "Math error."
23
+
24
+ class GoogleSearchTool:
25
+ def __init__(self, api_key, engine_id):
26
+ self.api_key = api_key
27
+ self.engine_id = engine_id
28
+
29
+ def run(self, query):
30
+ url = "https://www.googleapis.com/customsearch/v1"
31
+ params = {"key": self.api_key, "cx": self.engine_id, "q": query}
32
+ r = requests.get(url, params=params).json()
33
+ items = r.get("items", [])
34
+ return " ".join([item["snippet"] for item in items[:3]])
35
+
36
+ class WikipediaTool:
37
+ def run(self, query):
38
+ url = f"https://en.wikipedia.org/api/rest_v1/page/summary/{query}"
39
+ r = requests.get(url)
40
+ if r.status_code == 200:
41
+ return r.json().get("extract", "")
42
+ return "No wiki entry found."
43
 
44
+ class BasicAgent:
45
+ def __init__(self, tools):
46
+ self.tools = tools
47
+
48
+ def __call__(self, question: str):
49
+ print(f"Reasoning on: {question[:50]}...")
50
+
51
+ # Decide which tool to use
52
+ if re.search(r"\d+\s*[\+\-\*\/]\s*\d+", question):
53
+ return self.tools["math"].run(question)
54
+
55
+ if "who" in question.lower() or "what" in question.lower():
56
+ wiki_ans = self.tools["wiki"].run(question)
57
+ if wiki_ans:
58
+ return wiki_ans
59
+
60
+ # Default: search
61
+ return self.tools["google"].run(question)
62
 
63
  def run_and_submit_all( profile: gr.OAuthProfile | None):
64
  """