"""Búsqueda web para Arisu — DuckDuckGo, Wikipedia y Serper (opcional).""" import os import re from datetime import datetime from zoneinfo import ZoneInfo import requests as http_requests from duckduckgo_search import DDGS UA = "ArisuAI/1.0 (Educational assistant; +local)" MAX_RESULTS = 5 SNIPPET_LEN = 320 TIMEOUT = 12 _WIKI_LANG = {"es": "es", "en": "en", "ja": "ja", "fr": "fr", "de": "de", "pt": "pt", "ko": "ko", "zh": "zh"} _DAYS_ES = ("lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo") _MONTHS_ES = ( "enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre", ) _RELATIVE_WORDS = ( "hoy", "today", "ahora", "now", "actual", "actualidad", "reciente", "últim", "ultim", "esta semana", "this week", "este mes", "this month", "en vivo", "breaking", "noticias de", ) def _now() -> datetime: tz = os.getenv("APP_TIMEZONE", "America/Mexico_City") try: return datetime.now(ZoneInfo(tz)) except Exception: return datetime.now() def current_date_context(lang: str = "es") -> str: now = _now() iso = now.strftime("%Y-%m-%d") year = now.year if lang.startswith("es"): human = f"{_DAYS_ES[now.weekday()]}, {now.day} de {_MONTHS_ES[now.month - 1]} de {year}" else: human = now.strftime("%A, %B %d, %Y") return ( f"FECHA Y HORA ACTUAL DEL SISTEMA (autoritativa — NO uses tu fecha de entrenamiento):\n" f"- Hoy es: {human}\n" f"- ISO: {iso}\n" f"- Año en curso: {year}\n" f"- «Hoy» / «today» / «actual» = {iso}, año {year}." ) def enrich_search_query(query: str) -> str: """Añade fecha explícita si la consulta es relativa al presente.""" q = (query or "").strip() if not q: return q now = _now() iso = now.strftime("%Y-%m-%d") year = str(now.year) lower = q.lower() if year in q: return q if any(w in lower for w in _RELATIVE_WORDS): return f"{q} {iso}" return q def _clip(text: str, n: int = SNIPPET_LEN) -> str: text = re.sub(r"<[^>]+>", "", text or "").strip() if len(text) <= n: return text return text[:n] + "…" def _serper_search(query: str, api_key: str) -> str: resp = http_requests.post( "https://google.serper.dev/search", json={"q": query, "num": MAX_RESULTS}, headers={"X-API-KEY": api_key, "Content-Type": "application/json"}, timeout=TIMEOUT, ) resp.raise_for_status() data = resp.json() organic = data.get("organic") or [] if not organic: return f"No se encontraron resultados para: {query}" blocks = [] for i, r in enumerate(organic[:MAX_RESULTS], 1): title = r.get("title", "") link = r.get("link", "") snippet = _clip(r.get("snippet", "")) blocks.append(f"{i}. {title}\nURL: {link}\n{snippet}") return "\n\n".join(blocks) def _ddg_search(query: str) -> str: with DDGS() as ddgs: hits = list(ddgs.text(query, max_results=MAX_RESULTS)) if not hits: return "" blocks = [] for i, r in enumerate(hits, 1): title = (r.get("title") or "").strip() href = (r.get("href") or "").strip() body = _clip(r.get("body") or "") blocks.append(f"{i}. {title}\nURL: {href}\n{body}") return "\n\n".join(blocks) def _wiki_search(query: str, lang: str = "es") -> str: wiki_lang = _WIKI_LANG.get(lang[:2].lower(), "en") headers = {"User-Agent": UA} base = f"https://{wiki_lang}.wikipedia.org/w/api.php" sr = http_requests.get( base, params={ "action": "query", "list": "search", "srsearch": query, "format": "json", "srlimit": 3, }, headers=headers, timeout=TIMEOUT, ) sr.raise_for_status() hits = sr.json().get("query", {}).get("search") or [] if not hits: return "" blocks = [] for i, h in enumerate(hits, 1): title = h.get("title", "") snippet = _clip(h.get("snippet", "")) page_url = f"https://{wiki_lang}.wikipedia.org/wiki/{title.replace(' ', '_')}" extract = snippet try: er = http_requests.get( base, params={ "action": "query", "prop": "extracts", "exintro": True, "explaintext": True, "titles": title, "format": "json", }, headers=headers, timeout=TIMEOUT, ) er.raise_for_status() pages = er.json().get("query", {}).get("pages") or {} for p in pages.values(): if p.get("extract"): extract = _clip(p["extract"], 400) break except Exception: pass blocks.append(f"{i}. {title} (Wikipedia)\nURL: {page_url}\n{extract}") header = f"[Wikipedia — {wiki_lang}]\n" return header + "\n\n".join(blocks) def web_search(query: str, lang: str = "es") -> str: raw_q = (query or "").strip() if not raw_q: return "Error: consulta de búsqueda vacía" q = enrich_search_query(raw_q) now = _now() date_header = f"[Búsqueda — fecha del sistema: {now.strftime('%Y-%m-%d')}]\n" errors: list[str] = [] serper_key = os.getenv("SERPER_API_KEY", "").strip() if serper_key: try: result = _serper_search(q, serper_key) if result and "No se encontraron" not in result: note = f"Consulta enviada: {q}\n\n" if q != raw_q else "" return date_header + note + f"[Google/Serper]\n{result}" except Exception as e: errors.append(f"Serper: {e}") try: ddg = _ddg_search(q) if ddg: note = f"Consulta enviada: {q}\n\n" if q != raw_q else "" return date_header + note + f"[DuckDuckGo]\n{ddg}" except Exception as e: errors.append(f"DuckDuckGo: {e}") try: wiki = _wiki_search(q, lang) if wiki: note = f"Consulta enviada: {q}\n\n" if q != raw_q else "" return date_header + note + wiki except Exception as e: errors.append(f"Wikipedia: {e}") detail = "; ".join(errors) if errors else "sin resultados" return f"No se pudo obtener información web para «{raw_q}». ({detail})" WEB_SEARCH_TOOL = { "type": "function", "function": { "name": "web_search", "description": ( "Busca en internet cuando no sepas algo, necesites datos actuales, " "noticias o hechos recientes. Usa la FECHA ACTUAL del system prompt: " "si el usuario dice «hoy» o «actual», incluye el año en curso en la query." ), "parameters": { "type": "object", "properties": { "query": { "type": "string", "description": ( "Consulta clara. Si es sobre «hoy» o lo reciente, " "incluye la fecha ISO o el año actual del system prompt." ), }, }, "required": ["query"], }, }, } WEB_PROMPT = """ BÚSQUEDA WEB: Si no estás segura o piden algo actual/reciente, usa web_search ANTES de inventar. IMPORTANTE: la FECHA ACTUAL del system prompt es la verdad — NO uses 2024 ni tu corte de entrenamiento. Si dicen «hoy», «actual», «reciente», la búsqueda debe usar el año y fecha del system prompt. Tras buscar, resume en 2-3 oraciones con personalidad kawaii. """