Commit ·
35f14e6
1
Parent(s): 4a2e58c
implement stratified ranking
Browse files- agents/acquisition_agent.py +2 -8
- services/discovery_service.py +36 -7
agents/acquisition_agent.py
CHANGED
|
@@ -12,17 +12,11 @@ from loguru import logger
|
|
| 12 |
from models.state import Paper, Passage, State
|
| 13 |
from utils.pdf_extractor import PdfExtractionError, extract_text_from_pdf_file
|
| 14 |
|
| 15 |
-
# UPDATE THIS BLOCK
|
| 16 |
HEADERS = {
|
| 17 |
-
# Modern User-Agent (Chrome 120+)
|
| 18 |
-
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36",
|
| 19 |
-
# Mimic coming from Google Scholar
|
| 20 |
"Referer": "https://scholar.google.com/",
|
| 21 |
-
# Standard browser Accept headers
|
| 22 |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
| 23 |
"Accept-Language": "en-US,en;q=0.9",
|
| 24 |
-
|
| 25 |
-
"Connection": "keep-alive",
|
| 26 |
"Upgrade-Insecure-Requests": "1",
|
| 27 |
"Sec-Fetch-Dest": "document",
|
| 28 |
"Sec-Fetch-Mode": "navigate",
|
|
@@ -81,7 +75,7 @@ class AcquisitionAgent:
|
|
| 81 |
|
| 82 |
start_all = time.monotonic()
|
| 83 |
|
| 84 |
-
async with AsyncSession(impersonate="
|
| 85 |
# 1. First, add abstracts for ALL finalists (fast, synchronous)
|
| 86 |
for paper in finalists:
|
| 87 |
paper_id = paper.paperId
|
|
|
|
| 12 |
from models.state import Paper, Passage, State
|
| 13 |
from utils.pdf_extractor import PdfExtractionError, extract_text_from_pdf_file
|
| 14 |
|
|
|
|
| 15 |
HEADERS = {
|
|
|
|
|
|
|
|
|
|
| 16 |
"Referer": "https://scholar.google.com/",
|
|
|
|
| 17 |
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
|
| 18 |
"Accept-Language": "en-US,en;q=0.9",
|
| 19 |
+
# 'Accept-Encoding' is handled automatically by curl_cffi
|
|
|
|
| 20 |
"Upgrade-Insecure-Requests": "1",
|
| 21 |
"Sec-Fetch-Dest": "document",
|
| 22 |
"Sec-Fetch-Mode": "navigate",
|
|
|
|
| 75 |
|
| 76 |
start_all = time.monotonic()
|
| 77 |
|
| 78 |
+
async with AsyncSession(impersonate="chrome120", headers=HEADERS) as session:
|
| 79 |
# 1. First, add abstracts for ALL finalists (fast, synchronous)
|
| 80 |
for paper in finalists:
|
| 81 |
paper_id = paper.paperId
|
services/discovery_service.py
CHANGED
|
@@ -258,13 +258,42 @@ class DiscoveryService:
|
|
| 258 |
f"🔍 DETECTED INTENT: {intent_type} | APPLYING WEIGHTS: {weights}"
|
| 259 |
)
|
| 260 |
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
timing_breakdown["composite_scoring"] = (
|
| 269 |
time.perf_counter() - stage_start
|
| 270 |
)
|
|
|
|
| 258 |
f"🔍 DETECTED INTENT: {intent_type} | APPLYING WEIGHTS: {weights}"
|
| 259 |
)
|
| 260 |
|
| 261 |
+
# Stratified Ranking Logic
|
| 262 |
+
sub_queries = optimized_queries.get("sub_queries", [])
|
| 263 |
+
if (
|
| 264 |
+
sub_queries
|
| 265 |
+
and isinstance(sub_queries, list)
|
| 266 |
+
and len(sub_queries) > 1
|
| 267 |
+
):
|
| 268 |
+
logger.info(
|
| 269 |
+
"Applying Stratified Ranking for {} sub-queries",
|
| 270 |
+
len(sub_queries),
|
| 271 |
+
)
|
| 272 |
+
slots = 50 // len(sub_queries)
|
| 273 |
+
finalists_set = set()
|
| 274 |
+
finalists = []
|
| 275 |
+
|
| 276 |
+
for sub_q in sub_queries:
|
| 277 |
+
sub_results = await asyncio.to_thread(
|
| 278 |
+
self.ranker.rank_papers,
|
| 279 |
+
paper_objects,
|
| 280 |
+
sub_q,
|
| 281 |
+
slots,
|
| 282 |
+
weights,
|
| 283 |
+
)
|
| 284 |
+
for p in sub_results:
|
| 285 |
+
if p.paperId not in finalists_set:
|
| 286 |
+
finalists_set.add(p.paperId)
|
| 287 |
+
finalists.append(p)
|
| 288 |
+
else:
|
| 289 |
+
finalists = await asyncio.to_thread(
|
| 290 |
+
self.ranker.rank_papers,
|
| 291 |
+
paper_objects,
|
| 292 |
+
optimized_queries["final_rephrase"],
|
| 293 |
+
50, # top_k for Base+Bonus strategy
|
| 294 |
+
weights,
|
| 295 |
+
)
|
| 296 |
+
|
| 297 |
timing_breakdown["composite_scoring"] = (
|
| 298 |
time.perf_counter() - stage_start
|
| 299 |
)
|