M8-T4: Shim fetches Ergebnisseiten selbst (raw_content >100 chars) — gptr-Scraper im Image ist broken (Scraped 0 pages)
Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
This commit is contained in:
@@ -1,19 +1,43 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Tiny shim between gpt-researcher's RETRIEVER=custom and SearXNG.
|
||||
"""Shim between gpt-researcher's RETRIEVER=custom and SearXNG.
|
||||
|
||||
gpt-researcher custom retriever does: GET <RETRIEVER_ENDPOINT>?query=<q>
|
||||
(+ RETRIEVER_ARG_* as extra params) and expects a JSON list of
|
||||
{"url", "raw_content"}. SearXNG wants /search?q=<q>&format=json and returns
|
||||
{"results": [...]}. This shim maps between the two. Listens on :8099,
|
||||
internal network only (no published port).
|
||||
gpt-researcher custom retriever: GET <RETRIEVER_ENDPOINT>?query=<q> (+ extra
|
||||
params), expects a JSON list of {"url", "raw_content"}. If raw_content is
|
||||
longer than 100 chars, gpt-researcher SKIPS its own (broken-in-image)
|
||||
scraper and uses raw_content directly. So this shim fetches each result page
|
||||
itself and returns the text — no scraping on the gptr side needed.
|
||||
|
||||
SearXNG side: /search?q=<q>&format=json -> {"results": [...]}.
|
||||
Listens on :8099, internal networks only (no published port).
|
||||
"""
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
import urllib.parse
|
||||
import urllib.request
|
||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||
|
||||
SEARXNG = os.environ.get("SHIM_SEARXNG_URL", "http://searxng:8080")
|
||||
UA = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 Chrome/131.0 Safari/537.36"
|
||||
MAX_RESULTS = int(os.environ.get("SHIM_MAX_RESULTS", "8"))
|
||||
FETCH_TIMEOUT = 10
|
||||
MAX_BODY = 200_000
|
||||
|
||||
|
||||
def fetch_text(url):
|
||||
try:
|
||||
req = urllib.request.Request(url, headers={"User-Agent": UA})
|
||||
with urllib.request.urlopen(req, timeout=FETCH_TIMEOUT) as resp:
|
||||
ctype = resp.headers.get("content-type", "")
|
||||
if "html" not in ctype and "text" not in ctype:
|
||||
return ""
|
||||
raw = resp.read(MAX_BODY).decode("utf-8", errors="replace")
|
||||
text = re.sub(r"(?is)<(script|style|nav|footer|header|svg)[^>]*>.*?</\1>", " ", raw)
|
||||
text = re.sub(r"(?s)<[^>]+>", " ", text)
|
||||
text = re.sub(r"\s+", " ", text).strip()
|
||||
return text[:8000]
|
||||
except Exception:
|
||||
return ""
|
||||
|
||||
|
||||
class Handler(BaseHTTPRequestHandler):
|
||||
@@ -27,18 +51,20 @@ class Handler(BaseHTTPRequestHandler):
|
||||
return
|
||||
url = f"{SEARXNG}/search?" + urllib.parse.urlencode({"q": q, "format": "json"})
|
||||
try:
|
||||
with urllib.request.urlopen(url, timeout=20) as resp:
|
||||
with urllib.request.urlopen(url, timeout=25) as resp:
|
||||
data = json.loads(resp.read())
|
||||
except Exception as e:
|
||||
self.send_response(502)
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps({"error": str(e)}).encode())
|
||||
return
|
||||
out = [
|
||||
{"url": r.get("url", ""), "raw_content": r.get("content", "")}
|
||||
for r in data.get("results", [])
|
||||
if r.get("url")
|
||||
]
|
||||
out = []
|
||||
for r in data.get("results", [])[:MAX_RESULTS]:
|
||||
u = r.get("url", "")
|
||||
if not u:
|
||||
continue
|
||||
content = fetch_text(u) or (r.get("content") or "")
|
||||
out.append({"url": u, "raw_content": content})
|
||||
body = json.dumps(out).encode()
|
||||
self.send_response(200)
|
||||
self.send_header("Content-Type", "application/json")
|
||||
|
||||
@@ -26,6 +26,9 @@
|
||||
owner: "{{ gpt_researcher_user }}"
|
||||
group: "{{ gpt_researcher_user }}"
|
||||
mode: "0644"
|
||||
notify:
|
||||
- Reload workload user manager
|
||||
- Restart gptr units
|
||||
|
||||
- name: Check whether the engine API key secret exists
|
||||
ansible.builtin.command:
|
||||
|
||||
Reference in New Issue
Block a user