File size: 1,645 Bytes
2bf962d 4a3e964 2bf962d a9c5bda 4a3e964 |
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 |
from typing import List
from duckduckgo_search import DDGS
import requests
import os
# --- Simple Search Tool ---
def simple_search(query: str, max_results: int = 3) -> List[str]:
"""
Perform a DuckDuckGo search and return a list of strings summarizing the top results.
"""
with DDGS() as ddgs:
raw_results = list(ddgs.text(query, max_results=max_results))
results = []
for r in raw_results:
try:
title = r.get("title", "")
link = r.get("href") or r.get("link", "")
summary = f"{title} - {link}"
results.append(summary)
except Exception as e:
print("Skipping malformed search result:", r, "Error:", e)
return results
# --- Jina Search Tool ---
def jina_search_tool(query: str, api_key: str) -> List[str]:
"""
Perform a web search using Jina AI's s.jina.ai endpoint and retrieve clean, LLM-friendly content.
"""
api_endpoint = f"https://s.jina.ai/{query.replace(' ', '+')}"
headers = {
"Authorization": f"Bearer {api_key}",
"Accept": "application/json",
"User-Agent": "Mozilla/5.0"
}
try:
response = requests.get(api_endpoint, headers=headers, timeout=10)
if response.status_code == 200:
data = response.json()
contents = [item.get("content", "") for item in data.get("results", [])]
return contents
else:
print(f"Failed to fetch search results: Status code {response.status_code}")
return []
except Exception as e:
print(f"Error fetching search results: {e}")
return []
|