File size: 2,345 Bytes
325b94c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# import os
# from tavily import TavilyClient
# from crewai.tools import tool
# from modules import TRUSTED_SITES

# search_tool = TavilyClient(api_key=os.environ["TAVILY_API_KEY"])


# def is_recent(result, min_year=2021):
#     """Validate result date is >= min_year if available."""
#     pub_date = result.get("published_date")
#     if not pub_date:
#         return False  # skip if no date
#     try:
#         year = int(pub_date.split("-")[0])
#         return year >= min_year
#     except Exception:
#         return False


# @tool
# def search_engine_tool(query: str):
#     """Execute a focused web search for the given query.

#     - Prioritize **scholarly resources** (academic papers, books, journals, trusted educational sites).
#     - Filters results to only those published/updated >= 2021.
#     - Include **Arabic sources** when contextually relevant, but prioritize **English academic sources**.
#     - Retrieve only **diverse and high-quality** results (avoid spam, low-quality blogs, or purely commercial pages).
#     - For each result, ensure useful metadata is returned:
#         * url
#         * title
#         * short content/summary
#         * relevance score
#     - Use a maximum of 5 results per query to keep output precise and useful.
#     """
#     # Step 1: Normal Tavily search
#     # tavily_results = search_tool.search(query)

#     site_query = f"{query} after:2020"
#     tavily_results = search_tool.search(query=site_query, max_results=10)
#     # Step 2: Force search inside trusted domains
#     trusted_results = []
#     for site in TRUSTED_SITES:
#         site_query = f"site:{site} {query}"
#         try:
#             res = search_tool.search(site_query)
#             if res and "results" in res:
#                 trusted_results.extend(res["results"])
#         except Exception as e:
#             print(f"Skipping {site}: {e}")

#     # Step 3: Merge + deduplicate
#     all_results = tavily_results.get("results", []) + trusted_results
#     seen = set()
#     filtered = []
#     for r in all_results:
#         if r["url"] not in seen:
#             seen.add(r["url"])
#             filtered.append(r)

#     # Step 4: Limit to 5 best results
#     final_results = sorted(filtered, key=lambda x: x.get("score", 0), reverse=True)[:5]

#     return {"results": final_results}