File size: 9,638 Bytes
22e9366 27c6634 22e9366 e955d78 22e9366 e955d78 22e9366 e955d78 22e9366 e955d78 ad651e3 e955d78 22e9366 e955d78 22e9366 e955d78 22e9366 e955d78 22e9366 e955d78 22e9366 e955d78 22e9366 ad651e3 22e9366 e955d78 22e9366 e955d78 22e9366 ad651e3 22e9366 | 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 | import os
import time
import math
import hashlib
import json
import logging
from datetime import datetime
from typing import Optional
from dotenv import load_dotenv
from sentence_transformers import SentenceTransformer
from sklearn.metrics.pairwise import cosine_similarity
import numpy as np
from src.state import Paper, WebResult
load_dotenv()
logger = logging.getLogger(__name__)
# ---------------------------------------------------------------------------
# Embedding model β loaded once at module level (CPU, fast)
# ---------------------------------------------------------------------------
_embedder: Optional[SentenceTransformer] = None
def get_embedder() -> SentenceTransformer:
global _embedder
if _embedder is None:
_embedder = SentenceTransformer("all-MiniLM-L6-v2")
return _embedder
# ---------------------------------------------------------------------------
# Disk cache β prevents re-fetching on eval loop crashes
# ---------------------------------------------------------------------------
_CACHE_DIR = os.environ.get(
"RECON_CACHE_DIR",
os.path.join(os.path.dirname(os.path.dirname(__file__)), "data", "cache")
)
os.makedirs(_CACHE_DIR, exist_ok=True)
def _cache_key(text: str) -> str:
return hashlib.md5(text.encode()).hexdigest()
def _cache_get(key: str) -> Optional[list]:
path = os.path.join(_CACHE_DIR, f"{key}.json")
if os.path.exists(path):
with open(path) as f:
return json.load(f)
return None
def _cache_set(key: str, data: list) -> None:
path = os.path.join(_CACHE_DIR, f"{key}.json")
with open(path, "w") as f:
json.dump(data, f)
# ---------------------------------------------------------------------------
# Recency scoring β three formulas for ablation study
# ---------------------------------------------------------------------------
CURRENT_YEAR = datetime.now().year
def recency_score(year: int, decay_config: str = "linear") -> float:
"""
Returns a 0β1 recency score for a paper given its publication year.
decay_config: "none" | "linear" | "log"
"""
if year is None or year == 0:
return 0.0
age = max(0, CURRENT_YEAR - year)
if decay_config == "none":
return 1.0
elif decay_config == "linear":
return max(0.0, 1.0 - (age / 20.0))
elif decay_config == "log":
return max(0.0, 1.0 - math.log1p(age) / math.log1p(20))
else:
return max(0.0, 1.0 - (age / 20.0)) # default to linear
def authority_score(citation_count: int) -> float:
"""Normalize citation count to 0β1 using log scale."""
if citation_count <= 0:
return 0.0
return min(1.0, math.log1p(citation_count) / math.log1p(10000))
def hybrid_score(
semantic_sim: float,
year: int,
citation_count: int,
decay_config: str = "linear",
) -> float:
"""
final_score = semantic_sim Γ 0.5 + recency Γ 0.3 + authority Γ 0.2
Weights chosen by ablation study (see eval/).
"""
r = recency_score(year, decay_config)
a = authority_score(citation_count)
return round(semantic_sim * 0.5 + r * 0.3 + a * 0.2, 4)
# ---------------------------------------------------------------------------
# Semantic Scholar search
# ---------------------------------------------------------------------------
def search_semantic_scholar(
query: str,
limit: int = 5,
decay_config: str = "linear",
use_cache: bool = True,
) -> list[Paper]:
"""
Search Semantic Scholar via direct HTTP request (avoids pagination bug).
Returns a list of Paper objects sorted by hybrid_score descending.
"""
cache_key = _cache_key(f"s2v2_{query}_{limit}")
if use_cache:
cached = _cache_get(cache_key)
if cached:
logger.info(f"S2 cache hit: {query[:50]}")
return [Paper(**p) for p in cached]
import requests
s2_key = os.getenv("S2_API_KEY")
headers = {"x-api-key": s2_key} if s2_key else {}
params = {
"query": query,
"limit": limit,
"fields": "title,abstract,year,citationCount,authors,references,paperId,externalIds",
}
time.sleep(3) # rate limit guard
try:
response = requests.get(
"https://api.semanticscholar.org/graph/v1/paper/search",
headers=headers,
params=params,
timeout=15,
)
response.raise_for_status()
data = response.json()
except Exception as e:
logger.warning(f"S2 search failed for '{query}': {e}")
return []
raw_papers = data.get("data", [])
if not raw_papers:
return []
embedder = get_embedder()
query_vec = embedder.encode([query])
papers = []
for r in raw_papers:
abstract = r.get("abstract") or ""
if not abstract:
abstract = r.get("title") or "No abstract available"
abstract_vec = embedder.encode([abstract])
sim = float(cosine_similarity(query_vec, abstract_vec)[0][0])
year = r.get("year") or 0
citations = r.get("citationCount") or 0
authors = [a["name"] for a in r.get("authors") or []]
references = [
ref["paperId"] for ref in (r.get("references") or [])
if ref.get("paperId")
]
doi = (r.get("externalIds") or {}).get("DOI", "") or ""
paper = Paper(
title=r.get("title") or "Untitled",
abstract=abstract,
year=year,
citation_count=citations,
paper_id=r.get("paperId") or "",
authors=authors,
references=references,
doi=doi,
hybrid_score=hybrid_score(sim, year, citations, decay_config),
source="semantic_scholar",
)
papers.append(paper)
papers.sort(key=lambda p: p.hybrid_score, reverse=True)
if use_cache:
_cache_set(cache_key, [p.__dict__ for p in papers])
return papers
# ---------------------------------------------------------------------------
# DuckDuckGo web search (with Tavily fallback)
# ---------------------------------------------------------------------------
def search_web(
query: str,
limit: int = 5,
use_cache: bool = True,
) -> list[WebResult]:
"""
Search the web via DuckDuckGo. Falls back to Tavily if DDG fails.
Returns a list of WebResult objects.
"""
cache_key = _cache_key(f"web_{query}_{limit}")
if use_cache:
cached = _cache_get(cache_key)
if cached:
logger.info(f"Web cache hit: {query[:50]}")
return [WebResult(**r) for r in cached]
results = _ddg_search(query, limit)
if not results:
logger.warning(f"DDG failed for '{query}', trying Tavily fallback")
results = _tavily_search(query, limit)
if use_cache and results:
_cache_set(cache_key, [r.__dict__ for r in results])
return results
def _ddg_search(query: str, limit: int) -> list[WebResult]:
try:
from ddgs import DDGS
time.sleep(1)
# Force English results, safesearch off, recent results
search_query = f"{query} research paper arxiv"
with DDGS() as ddgs:
raw = list(ddgs.text(
search_query,
max_results=limit,
region="wt-wt", # worldwide β avoids regional override
safesearch="off",
))
results = []
for r in raw:
year = _infer_year(r.get("body", ""))
results.append(WebResult(
url=r.get("href", ""),
snippet=r.get("body", "")[:500],
title=r.get("title", ""),
inferred_year=year,
source="duckduckgo",
))
return results
except Exception as e:
logger.warning(f"DDG error: {e}")
return []
def _tavily_search(query: str, limit: int) -> list[WebResult]:
tavily_key = os.getenv("TAVILY_API_KEY")
if not tavily_key:
return []
try:
from tavily import TavilyClient
client = TavilyClient(api_key=tavily_key)
response = client.search(query, max_results=limit)
results = []
for r in response.get("results", []):
year = _infer_year(r.get("content", ""))
results.append(WebResult(
url=r.get("url", ""),
snippet=r.get("content", "")[:500],
title=r.get("title", ""),
inferred_year=year,
source="tavily",
))
return results
except Exception as e:
logger.warning(f"Tavily error: {e}")
return []
def _infer_year(text: str) -> Optional[int]:
"""Try to extract a 4-digit year (2000β2026) from a text snippet."""
import re
matches = re.findall(r"\b(20[0-2][0-9])\b", text)
if matches:
years = [int(y) for y in matches]
return max(years)
return None
# ---------------------------------------------------------------------------
# Citation graph builder
# ---------------------------------------------------------------------------
def build_citation_graph(papers: list[Paper]) -> dict:
"""
Build a citation graph from retrieved papers.
Returns {paper_id: [list of referenced paper_ids that are also in our set]}
Only includes edges where both source and target are in our retrieved set.
"""
paper_ids = {p.paper_id for p in papers}
graph = {}
for p in papers:
graph[p.paper_id] = [
ref for ref in p.references
if ref in paper_ids
]
return graph |