Hanokh commited on
Commit
7369929
·
verified ·
1 Parent(s): 05180a6

Redirect to DuckDuckGo as a tool

Browse files
Files changed (1) hide show
  1. app.py +34 -24
app.py CHANGED
@@ -4,41 +4,51 @@ import requests
4
  import inspect
5
  import pandas as pd
6
 
 
 
 
 
7
  # (Keep Constants as is)
8
  # --- Constants ---
9
  DEFAULT_API_URL = "https://agents-course-unit4-scoring.hf.space"
10
 
11
  # --- Basic Agent Definition ---
12
  # ----- THIS IS WERE YOU CAN BUILD WHAT YOU WANT ------
 
 
13
  class BasicAgent:
14
  def __init__(self):
15
  print("BasicAgent initialized.")
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
  def __call__(self, question: str) -> str:
18
- # Agent's reasoning process (thoughts)
19
- thoughts = self.reason(question)
20
- final_answer = self.extract_answer(thoughts)
21
- response = (
22
- f"THOUGHTS: {thoughts}\n"
23
- f"FINAL ANSWER: {final_answer}"
24
- )
25
- print(f"Agent response:\n{response[:150]}...")
26
- return response
27
-
28
- def reason(self, question: str) -> str:
29
- # Put chain-of-thought reasoning here,
30
- # e.g., use LLM or heuristics to break down the problem.
31
- # Example placeholder for demonstration:
32
- return "Analyzing the problem step by step, considering all constraints, and isolating the answer format. Selected the most direct solution."
33
-
34
- def extract_answer(self, thoughts: str) -> str:
35
- # Normally, use selected answer from reasoning.
36
- # For now, this placeholder returns demo result.
37
- # Replace demo logic with answer extraction, format as per rules:
38
- # - if a number is needed, just plain digits (no commas or units unless specified)
39
- # - if string, no articles or abbreviations, digits written out unless specified
40
- # - if list, elements comma separated, each following corresponding rules
41
- return "42"
42
 
43
  def run_and_submit_all( profile: gr.OAuthProfile | None):
44
  """
 
4
  import inspect
5
  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 ---
13
  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
  """