naman1102's picture
Jina
4a3e964
raw
history blame
1.65 kB
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 []