From 8cb33c781001548bc5bac4f9b4e61cd4e48b6753 Mon Sep 17 00:00:00 2001 From: spektr Date: Thu, 19 Nov 2026 13:48:42 +0100 Subject: [PATCH] =?UTF-8?q?M8-T4:=20Shim=20fetches=20Ergebnisseiten=20selb?= =?UTF-8?q?st=20(raw=5Fcontent=20>100=20chars)=20=E2=80=94=20gptr-Scraper?= =?UTF-8?q?=20im=20Image=20ist=20broken=20(Scraped=200=20pages)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Generated by Mistral Vibe. Co-Authored-By: Mistral Vibe --- .../ai/GNAI-023-PLAN_GPT-Researcher-IAP.md | 7 +++ .../gpt_researcher/files/searxng_shim.py | 50 ++++++++++++++----- .../roles/gpt_researcher/tasks/main.yml | 3 ++ 3 files changed, 48 insertions(+), 12 deletions(-) diff --git a/docs/platform/llm-server/ai/GNAI-023-PLAN_GPT-Researcher-IAP.md b/docs/platform/llm-server/ai/GNAI-023-PLAN_GPT-Researcher-IAP.md index 3f5f893..b089eb7 100644 --- a/docs/platform/llm-server/ai/GNAI-023-PLAN_GPT-Researcher-IAP.md +++ b/docs/platform/llm-server/ai/GNAI-023-PLAN_GPT-Researcher-IAP.md @@ -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 Egress-Netz. Verifiziert: Shim-Query aus dem Backend-Container liefert 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. diff --git a/llm-server/llm/ansible/roles/gpt_researcher/files/searxng_shim.py b/llm-server/llm/ansible/roles/gpt_researcher/files/searxng_shim.py index 11e70c6..02d0e6e 100644 --- a/llm-server/llm/ansible/roles/gpt_researcher/files/searxng_shim.py +++ b/llm-server/llm/ansible/roles/gpt_researcher/files/searxng_shim.py @@ -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 ?query= -(+ RETRIEVER_ARG_* as extra params) and expects a JSON list of -{"url", "raw_content"}. SearXNG wants /search?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 ?query= (+ 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=&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)[^>]*>.*?", " ", 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") diff --git a/llm-server/llm/ansible/roles/gpt_researcher/tasks/main.yml b/llm-server/llm/ansible/roles/gpt_researcher/tasks/main.yml index cf1017c..a9bc508 100644 --- a/llm-server/llm/ansible/roles/gpt_researcher/tasks/main.yml +++ b/llm-server/llm/ansible/roles/gpt_researcher/tasks/main.yml @@ -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: