feat: parallelize lookup (Wiki+SearXNG) and include Wikipedia thumbnails
Runs the Wikipedia summary and SearXNG search concurrently and returns both when available, so current-event questions aren't masked by a generic role article from Wikipedia. When the Wikipedia summary includes a thumbnail, it is cached through the existing image pipeline and surfaced as an embeddable markdown snippet alongside the extract. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -23,57 +23,91 @@ logger = logging.getLogger(__name__)
|
||||
required=["query"],
|
||||
)
|
||||
async def lookup_tool(*, user_id, arguments, **_ctx):
|
||||
import asyncio
|
||||
|
||||
from fabledassistant.config import Config
|
||||
from fabledassistant.services.wikipedia import wiki_summary
|
||||
|
||||
query = arguments.get("query", "")
|
||||
query = arguments.get("query", "").strip()
|
||||
if not query:
|
||||
return {"success": False, "error": "query is required"}
|
||||
|
||||
# 1. Try Wikipedia first
|
||||
wiki = await wiki_summary(query)
|
||||
searxng_enabled = Config.searxng_enabled()
|
||||
|
||||
async def _searxng_results() -> list[dict]:
|
||||
if not searxng_enabled:
|
||||
return []
|
||||
from fabledassistant.services.research import _search_searxng
|
||||
return await _search_searxng(query) or []
|
||||
|
||||
wiki, search_results = await asyncio.gather(
|
||||
wiki_summary(query),
|
||||
_searxng_results(),
|
||||
)
|
||||
|
||||
wiki_payload: dict | None = None
|
||||
if wiki:
|
||||
wiki_payload = {
|
||||
"title": wiki["title"],
|
||||
"extract": wiki["extract"],
|
||||
"url": wiki["url"],
|
||||
}
|
||||
thumb_url = wiki.get("thumbnail_url") or ""
|
||||
if thumb_url:
|
||||
from fabledassistant.services.images import fetch_and_store_image
|
||||
record = await fetch_and_store_image(
|
||||
url=thumb_url,
|
||||
title=wiki["title"],
|
||||
source_domain="en.wikipedia.org",
|
||||
)
|
||||
if record:
|
||||
wiki_payload["image"] = {
|
||||
"embed": f"![{wiki['title']}](/api/images/{record.id})",
|
||||
"citation": f"*Source: [Wikipedia]({wiki['url']})*",
|
||||
}
|
||||
|
||||
web_payload: list[dict] = []
|
||||
if search_results:
|
||||
from fabledassistant.services.rss import _fetch_full_article
|
||||
|
||||
top = [r for r in search_results[:2] if r.get("url")]
|
||||
contents = await asyncio.gather(
|
||||
*(_fetch_full_article(r["url"]) for r in top),
|
||||
return_exceptions=True,
|
||||
)
|
||||
for r, content in zip(top, contents, strict=False):
|
||||
content_text = content if isinstance(content, str) else ""
|
||||
web_payload.append({
|
||||
"url": r["url"],
|
||||
"title": r.get("title", r["url"]),
|
||||
"snippet": r.get("snippet", ""),
|
||||
"content": content_text[:4000],
|
||||
})
|
||||
|
||||
if not wiki_payload and not web_payload:
|
||||
return {
|
||||
"success": True,
|
||||
"type": "lookup",
|
||||
"source": "wikipedia",
|
||||
"data": wiki,
|
||||
"data": {
|
||||
"query": query,
|
||||
"message": "No results found. You can answer from your own knowledge.",
|
||||
},
|
||||
}
|
||||
|
||||
# 2. Fall back to SearXNG + article fetch
|
||||
if Config.searxng_enabled():
|
||||
from fabledassistant.services.research import _search_searxng
|
||||
from fabledassistant.services.rss import _fetch_full_article
|
||||
|
||||
results = await _search_searxng(query)
|
||||
if results:
|
||||
articles: list[dict] = []
|
||||
for r in results[:2]:
|
||||
url = r.get("url", "")
|
||||
if not url:
|
||||
continue
|
||||
content = await _fetch_full_article(url)
|
||||
articles.append({
|
||||
"url": url,
|
||||
"title": r.get("title", url),
|
||||
"snippet": r.get("snippet", ""),
|
||||
"content": (content or "")[:4000],
|
||||
})
|
||||
if articles:
|
||||
return {
|
||||
"success": True,
|
||||
"type": "lookup",
|
||||
"source": "web",
|
||||
"data": {"query": query, "results": articles},
|
||||
}
|
||||
|
||||
# 3. No sources available
|
||||
data: dict = {
|
||||
"query": query,
|
||||
"wikipedia": wiki_payload,
|
||||
"web": web_payload,
|
||||
}
|
||||
if wiki_payload and wiki_payload.get("image"):
|
||||
data["image_instructions"] = (
|
||||
"If an image is relevant to your reply, embed it by writing the wikipedia.image.embed "
|
||||
"field verbatim, then the citation field on the next line. Otherwise omit it."
|
||||
)
|
||||
return {
|
||||
"success": True,
|
||||
"type": "lookup",
|
||||
"source": "none",
|
||||
"data": {
|
||||
"query": query,
|
||||
"message": "No results found. You can answer from your own knowledge.",
|
||||
},
|
||||
"data": data,
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -12,11 +12,21 @@ _TIMEOUT = 5.0
|
||||
_USER_AGENT = "FabledAssistant/1.0 (https://fabledsword.com)"
|
||||
|
||||
|
||||
def _extract_thumbnail(data: dict) -> str:
|
||||
"""Prefer the 320px thumbnail (kinder to cache/bandwidth) over originalimage."""
|
||||
thumb = data.get("thumbnail", {}).get("source", "")
|
||||
if thumb:
|
||||
return thumb
|
||||
return data.get("originalimage", {}).get("source", "")
|
||||
|
||||
|
||||
async def wiki_summary(query: str) -> dict | None:
|
||||
"""Fetch a Wikipedia summary for a given title/query.
|
||||
|
||||
Does a direct title lookup via the REST v1 summary endpoint.
|
||||
Returns a dict with title, extract, and url on success; None on any failure.
|
||||
Returns a dict with title, extract, url, and thumbnail_url on success;
|
||||
thumbnail_url is an empty string when the article has no image.
|
||||
Returns None on any failure.
|
||||
"""
|
||||
title = url_quote(query.replace(" ", "_"))
|
||||
url = f"{_SUMMARY_URL}/{title}"
|
||||
@@ -37,6 +47,7 @@ async def wiki_summary(query: str) -> dict | None:
|
||||
"title": data.get("title", query),
|
||||
"extract": extract,
|
||||
"url": data.get("content_urls", {}).get("desktop", {}).get("page", ""),
|
||||
"thumbnail_url": _extract_thumbnail(data),
|
||||
}
|
||||
except Exception as exc:
|
||||
logger.debug("wiki_summary failed for %r: %s", query, exc)
|
||||
@@ -91,6 +102,7 @@ async def wiki_search(query: str, limit: int = 3) -> list[dict]:
|
||||
"url": sdata.get("content_urls", {})
|
||||
.get("desktop", {})
|
||||
.get("page", ""),
|
||||
"thumbnail_url": _extract_thumbnail(sdata),
|
||||
}
|
||||
)
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user