ContiAI / tools /tavily.py
ziadsameh32's picture
Add login page
325b94c
Raw
History Blame Contribute Delete
2.35 kB
# 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}