ai_se/SearchManager.py

25 lines
620 B
Python

from ddgs import DDGS
def perform_search(query: str) -> dict:
#sucht Zusatzinfos im Internet
try:
results = DDGS().text(query, max_results=3)
return results
except Exception as e:
return {"error":str(e), "results":[]}
def parse_results(results: list[dict]) -> list[dict]:
#Limitiert Inhalt der Suchergebnisse auf 500 Zeichen, damit qwen nicht zu langen Prompt erhält
return [
{
"url": r.get("href", ""), #
"snippet": r.get("body", "")[:500]
}
for r in results
if r.get("href", "").startswith('https://')
]