nikeshn commited on
Commit
a30afbd
·
verified ·
1 Parent(s): 835b166

Update src/api/agent.py

Browse files
Files changed (1) hide show
  1. src/api/agent.py +50 -15
src/api/agent.py CHANGED
@@ -1399,31 +1399,63 @@ async def _semantic_scholar_search(topic: str, limit: int = 8) -> List[dict]:
1399
 
1400
  async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1401
  """
1402
- Search OpenAlex (free, no API key) for peer-reviewed papers with abstracts.
1403
- Returns papers in the same dict format as _semantic_scholar_search.
1404
- Typically responds in < 2 seconds.
 
1405
  """
1406
  try:
1407
- from urllib.parse import quote as _q
 
 
 
 
 
 
1408
  params = {
1409
- "search": topic,
1410
- "filter": "has_abstract:true,type:article",
1411
- "sort": "cited_by_count:desc",
1412
- "per_page": limit,
1413
- "select": "id,title,authorships,publication_year,primary_location,abstract_inverted_index,doi,cited_by_count,open_access",
 
 
 
 
 
 
1414
  }
1415
- url = "https://api.openalex.org/works?" + "&".join(f"{k}={_q(str(v))}" for k, v in params.items())
1416
  headers = {"User-Agent": "LibBee/3.2 (mailto:libse@ku.ac.ae)"}
1417
 
1418
  async with httpx.AsyncClient(timeout=10) as client:
1419
  r = await client.get(url, headers=headers)
1420
  if r.status_code != 200:
1421
- logger.warning(f"OpenAlex returned {r.status_code}")
1422
  return []
1423
 
1424
  data = r.json()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1425
  results = []
1426
- for work in data.get("results", []):
1427
  title = work.get("title") or "Untitled"
1428
 
1429
  # Reconstruct abstract from inverted index
@@ -1450,7 +1482,7 @@ async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1450
  authors_str = ", ".join(author_names[:3])
1451
  if len(author_names) > 3:
1452
  authors_str += " et al."
1453
- first_last = (author_names[0].split()[-1] if author_names else "Unknown")
1454
 
1455
  year = str(work.get("publication_year") or "n.d.")
1456
  doi = (work.get("doi") or "").replace("https://doi.org/", "")
@@ -1460,7 +1492,10 @@ async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1460
  else (f"https://doi.org/{doi}" if doi else "")
1461
  or work.get("id", "https://openalex.org")
1462
  )
1463
- venue = ((work.get("primary_location") or {}).get("source") or {}).get("display_name") or ""
 
 
 
1464
 
1465
  results.append({
1466
  "title": title,
@@ -1474,7 +1509,7 @@ async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1474
  "_source": "OpenAlex",
1475
  })
1476
 
1477
- logger.info(f"OpenAlex: {len(results)} papers for '{topic[:50]}'")
1478
  return results
1479
 
1480
  except Exception as e:
 
1399
 
1400
  async def _openalex_search(topic: str, limit: int = 8) -> List[dict]:
1401
  """
1402
+ Search OpenAlex using title.search + abstract.search filters for precision.
1403
+ This targets only the title and abstract fields — far more relevant than the
1404
+ general `search` parameter which also hits author names, journals, institutions.
1405
+ Free, no API key, typically < 2s.
1406
  """
1407
  try:
1408
+ from urllib.parse import urlencode
1409
+
1410
+ # Build a clean search term — strip boolean operators, keep the core phrase
1411
+ clean = re.sub(r'\b(AND|OR|NOT)\b|[()"\']', ' ', topic, flags=re.IGNORECASE)
1412
+ clean = re.sub(r'\s+', ' ', clean).strip()[:200]
1413
+
1414
+ # Use title.search OR abstract.search — papers that match in either field
1415
  params = {
1416
+ "filter": (
1417
+ f"title.search:{clean}|abstract.search:{clean},"
1418
+ f"has_abstract:true,type:article,is_paratext:false"
1419
+ ),
1420
+ "sort": "relevance_score:desc",
1421
+ "per_page": str(limit),
1422
+ "select": ",".join([
1423
+ "id", "title", "authorships", "publication_year",
1424
+ "primary_location", "abstract_inverted_index",
1425
+ "doi", "cited_by_count", "open_access",
1426
+ ]),
1427
  }
1428
+ url = "https://api.openalex.org/works?" + urlencode(params)
1429
  headers = {"User-Agent": "LibBee/3.2 (mailto:libse@ku.ac.ae)"}
1430
 
1431
  async with httpx.AsyncClient(timeout=10) as client:
1432
  r = await client.get(url, headers=headers)
1433
  if r.status_code != 200:
1434
+ logger.warning(f"OpenAlex returned {r.status_code} for topic '{clean[:40]}'")
1435
  return []
1436
 
1437
  data = r.json()
1438
+ works = data.get("results", [])
1439
+
1440
+ # If title+abstract search returns nothing, fall back to general search
1441
+ if not works:
1442
+ logger.info(f"OpenAlex title+abstract search returned 0 — retrying with general search")
1443
+ params_fallback = {
1444
+ "search": clean,
1445
+ "filter": "has_abstract:true,type:article,is_paratext:false",
1446
+ "sort": "relevance_score:desc",
1447
+ "per_page": str(limit),
1448
+ "select": params["select"],
1449
+ }
1450
+ r2 = await client.get(
1451
+ "https://api.openalex.org/works?" + urlencode(params_fallback),
1452
+ headers=headers,
1453
+ )
1454
+ if r2.status_code == 200:
1455
+ works = r2.json().get("results", [])
1456
+
1457
  results = []
1458
+ for work in works:
1459
  title = work.get("title") or "Untitled"
1460
 
1461
  # Reconstruct abstract from inverted index
 
1482
  authors_str = ", ".join(author_names[:3])
1483
  if len(author_names) > 3:
1484
  authors_str += " et al."
1485
+ first_last = author_names[0].split()[-1] if author_names else "Unknown"
1486
 
1487
  year = str(work.get("publication_year") or "n.d.")
1488
  doi = (work.get("doi") or "").replace("https://doi.org/", "")
 
1492
  else (f"https://doi.org/{doi}" if doi else "")
1493
  or work.get("id", "https://openalex.org")
1494
  )
1495
+ venue = (
1496
+ (work.get("primary_location") or {})
1497
+ .get("source", {}) or {}
1498
+ ).get("display_name") or ""
1499
 
1500
  results.append({
1501
  "title": title,
 
1509
  "_source": "OpenAlex",
1510
  })
1511
 
1512
+ logger.info(f"OpenAlex: {len(results)} papers for '{clean[:50]}'")
1513
  return results
1514
 
1515
  except Exception as e: