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:
@@ -228,3 +228,10 @@ Internet → NPM (CT 103, 443) → auth_request-Subrequest an oauth2-proxy
|
|||||||
ohne publish im LLM-Netz (Alias `gptr-searxng-shim`), ruft SearXNG im
|
ohne publish im LLM-Netz (Alias `gptr-searxng-shim`), ruft SearXNG im
|
||||||
Egress-Netz. Verifiziert: Shim-Query aus dem Backend-Container liefert
|
Egress-Netz. Verifiziert: Shim-Query aus dem Backend-Container liefert
|
||||||
echte Ergebnisse; research.yml idempotent.
|
echte Ergebnisse; research.yml idempotent.
|
||||||
|
|
||||||
|
- **Befund 19.11. (Scraped 0 pages):** der Scraper im gptr-Image ist kaputt
|
||||||
|
(`Error processing None: 'NoneType' ... endswith`, `Scraped data size: 0`).
|
||||||
|
Workaround: der Shim fetches die Ergebnisseiten selbst und liefert
|
||||||
|
`raw_content` mit vollem Seiten-Text (>100 chars) — dann scrapt
|
||||||
|
gpt-researcher nicht selbst. `SHIM_MAX_RESULTS=8`, Fetch-Cap 200k/Seite,
|
||||||
|
Timeout 10 s. Verifiziert: shim count 8 mit Seiteninhalt.
|
||||||
|
|||||||
@@ -1,19 +1,43 @@
|
|||||||
#!/usr/bin/env python3
|
#!/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>
|
gpt-researcher custom retriever: GET <RETRIEVER_ENDPOINT>?query=<q> (+ extra
|
||||||
(+ RETRIEVER_ARG_* as extra params) and expects a JSON list of
|
params), expects a JSON list of {"url", "raw_content"}. If raw_content is
|
||||||
{"url", "raw_content"}. SearXNG wants /search?q=<q>&format=json and returns
|
longer than 100 chars, gpt-researcher SKIPS its own (broken-in-image)
|
||||||
{"results": [...]}. This shim maps between the two. Listens on :8099,
|
scraper and uses raw_content directly. So this shim fetches each result page
|
||||||
internal network only (no published port).
|
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 json
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import urllib.parse
|
import urllib.parse
|
||||||
import urllib.request
|
import urllib.request
|
||||||
from http.server import BaseHTTPRequestHandler, HTTPServer
|
from http.server import BaseHTTPRequestHandler, HTTPServer
|
||||||
|
|
||||||
SEARXNG = os.environ.get("SHIM_SEARXNG_URL", "http://searxng:8080")
|
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):
|
class Handler(BaseHTTPRequestHandler):
|
||||||
@@ -27,18 +51,20 @@ class Handler(BaseHTTPRequestHandler):
|
|||||||
return
|
return
|
||||||
url = f"{SEARXNG}/search?" + urllib.parse.urlencode({"q": q, "format": "json"})
|
url = f"{SEARXNG}/search?" + urllib.parse.urlencode({"q": q, "format": "json"})
|
||||||
try:
|
try:
|
||||||
with urllib.request.urlopen(url, timeout=20) as resp:
|
with urllib.request.urlopen(url, timeout=25) as resp:
|
||||||
data = json.loads(resp.read())
|
data = json.loads(resp.read())
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.send_response(502)
|
self.send_response(502)
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
self.wfile.write(json.dumps({"error": str(e)}).encode())
|
self.wfile.write(json.dumps({"error": str(e)}).encode())
|
||||||
return
|
return
|
||||||
out = [
|
out = []
|
||||||
{"url": r.get("url", ""), "raw_content": r.get("content", "")}
|
for r in data.get("results", [])[:MAX_RESULTS]:
|
||||||
for r in data.get("results", [])
|
u = r.get("url", "")
|
||||||
if 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()
|
body = json.dumps(out).encode()
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header("Content-Type", "application/json")
|
self.send_header("Content-Type", "application/json")
|
||||||
|
|||||||
@@ -26,6 +26,9 @@
|
|||||||
owner: "{{ gpt_researcher_user }}"
|
owner: "{{ gpt_researcher_user }}"
|
||||||
group: "{{ gpt_researcher_user }}"
|
group: "{{ gpt_researcher_user }}"
|
||||||
mode: "0644"
|
mode: "0644"
|
||||||
|
notify:
|
||||||
|
- Reload workload user manager
|
||||||
|
- Restart gptr units
|
||||||
|
|
||||||
- name: Check whether the engine API key secret exists
|
- name: Check whether the engine API key secret exists
|
||||||
ansible.builtin.command:
|
ansible.builtin.command:
|
||||||
|
|||||||
Reference in New Issue
Block a user