78 lines
1.9 KiB
Python
78 lines
1.9 KiB
Python
import asyncio
|
|
from datetime import datetime
|
|
|
|
from ddgs import DDGS
|
|
|
|
|
|
async def web_search(query: str, max_results: int = 5):
|
|
"""
|
|
Durchsucht das Internet mit DuckDuckGo.
|
|
"""
|
|
|
|
try:
|
|
|
|
def search():
|
|
with DDGS() as ddgs:
|
|
return list(
|
|
ddgs.text(
|
|
query,
|
|
max_results=max_results,
|
|
)
|
|
)
|
|
|
|
results = await asyncio.to_thread(search)
|
|
|
|
if not results:
|
|
return "Keine Suchergebnisse gefunden."
|
|
|
|
output = (
|
|
f"Suchergebnisse für '{query}' "
|
|
f"(Stand {datetime.now():%d.%m.%Y %H:%M})\n\n"
|
|
)
|
|
|
|
for i, r in enumerate(results, 1):
|
|
|
|
title = r.get("title", "Kein Titel")
|
|
body = r.get("body", "")
|
|
href = r.get("href", "")
|
|
|
|
output += (
|
|
f"{i}. {title}\n"
|
|
f" {href}\n"
|
|
f" {body}\n\n"
|
|
)
|
|
|
|
return output
|
|
|
|
except Exception as e:
|
|
return f"Fehler bei der Websuche: {e}"
|
|
|
|
|
|
web_search_tool = {
|
|
"toolSpec": {
|
|
"name": "web_search",
|
|
"description": (
|
|
"Durchsucht das Internet nach aktuellen Informationen, "
|
|
"Nachrichten, Fakten, Wetter, Preisen und Ereignissen."
|
|
),
|
|
"inputSchema": {
|
|
"json": {
|
|
"type": "object",
|
|
"properties": {
|
|
"query": {
|
|
"type": "string",
|
|
"description": "Suchanfrage"
|
|
},
|
|
"max_results": {
|
|
"type": "integer",
|
|
"description": "Maximale Anzahl Ergebnisse",
|
|
"default": 5
|
|
}
|
|
},
|
|
"required": [
|
|
"query"
|
|
]
|
|
}
|
|
}
|
|
}
|
|
} |