Nottybro commited on
Commit
893fc58
·
verified ·
1 Parent(s): 92a11ef

fix: jina web search with Bearer auth header

Browse files
Files changed (1) hide show
  1. acra.py +12 -7
acra.py CHANGED
@@ -33,31 +33,36 @@ def adaptive_chunk(text, max_tok=512):
33
  return chunks or [text]
34
 
35
  def web_search(query: str, max_results: int = 5) -> List[dict]:
36
- """Web search via Jina AI s.jina.ai no API key needed, works from cloud."""
37
  try:
38
  import urllib.parse
39
  encoded = urllib.parse.quote(query)
 
40
  r = httpx.get(
41
  f"https://s.jina.ai/?q={encoded}",
42
- headers={"Accept": "application/json", "X-Respond-With": "no-content"},
 
 
 
 
43
  timeout=20.0,
44
  follow_redirects=True
45
  )
46
  if r.status_code != 200:
47
  print(f"Jina returned {r.status_code}: {r.text[:200]}")
48
  return []
49
- data = r.json()
50
  items = data.get("data", [])
51
- results = []
52
  for item in items[:max_results]:
53
- snippet = item.get("description") or item.get("content","")
54
  if snippet:
55
- results.append({
56
  "title": item.get("title", ""),
57
  "snippet": snippet[:500],
58
  "url": item.get("url", "")
59
  })
60
- return results
61
  except Exception as e:
62
  print(f"Web search error: {e}")
63
  return []
 
33
  return chunks or [text]
34
 
35
  def web_search(query: str, max_results: int = 5) -> List[dict]:
36
+ """Web search via Jina AI — requires JINA_API_KEY secret in Space."""
37
  try:
38
  import urllib.parse
39
  encoded = urllib.parse.quote(query)
40
+ jina_key = os.environ.get("JINA_API_KEY", "")
41
  r = httpx.get(
42
  f"https://s.jina.ai/?q={encoded}",
43
+ headers={
44
+ "Authorization": f"Bearer {jina_key}",
45
+ "Accept": "application/json",
46
+ "X-Retain-Images": "none",
47
+ },
48
  timeout=20.0,
49
  follow_redirects=True
50
  )
51
  if r.status_code != 200:
52
  print(f"Jina returned {r.status_code}: {r.text[:200]}")
53
  return []
54
+ data = r.json()
55
  items = data.get("data", [])
56
+ out = []
57
  for item in items[:max_results]:
58
+ snippet = item.get("description") or item.get("content", "")
59
  if snippet:
60
+ out.append({
61
  "title": item.get("title", ""),
62
  "snippet": snippet[:500],
63
  "url": item.get("url", "")
64
  })
65
+ return out
66
  except Exception as e:
67
  print(f"Web search error: {e}")
68
  return []