tools/web_search.py aktualisiert
This commit is contained in:
+43
-31
@@ -1,66 +1,78 @@
|
|||||||
import requests
|
import asyncio
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
|
from ddgs import DDGS
|
||||||
|
|
||||||
|
|
||||||
async def web_search(query: str, max_results: int = 5):
|
async def web_search(query: str, max_results: int = 5):
|
||||||
"""
|
"""
|
||||||
Sucht im Internet und gibt aktuelle, relevante Ergebnisse zurück.
|
Durchsucht das Internet mit DuckDuckGo.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# DuckDuckGo (kostenlos, kein API-Key nötig)
|
|
||||||
url = "https://api.duckduckgo.com/"
|
|
||||||
params = {
|
|
||||||
"q": query,
|
|
||||||
"format": "json",
|
|
||||||
"no_html": 1,
|
|
||||||
"skip_disambig": 1
|
|
||||||
}
|
|
||||||
|
|
||||||
response = requests.get(url, params=params, timeout=8)
|
def search():
|
||||||
data = response.json()
|
with DDGS() as ddgs:
|
||||||
|
return list(
|
||||||
|
ddgs.text(
|
||||||
|
query,
|
||||||
|
max_results=max_results,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
results = []
|
results = await asyncio.to_thread(search)
|
||||||
for result in data.get("RelatedTopics", [])[:max_results]:
|
|
||||||
if 'Text' in result and 'FirstURL' in result:
|
|
||||||
results.append({
|
|
||||||
"title": result.get("Text", "")[:150],
|
|
||||||
"url": result.get("FirstURL", ""),
|
|
||||||
"snippet": result.get("Text", "")[:300]
|
|
||||||
})
|
|
||||||
|
|
||||||
if not results:
|
if not results:
|
||||||
return "Keine relevanten Suchergebnisse gefunden."
|
return "Keine Suchergebnisse gefunden."
|
||||||
|
|
||||||
|
output = (
|
||||||
|
f"Suchergebnisse für '{query}' "
|
||||||
|
f"(Stand {datetime.now():%d.%m.%Y %H:%M})\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
output = f"Suchergebnisse für '{query}' (Stand: {datetime.now().strftime('%d.%m.%Y %H:%M')}):\n\n"
|
|
||||||
for i, r in enumerate(results, 1):
|
for i, r in enumerate(results, 1):
|
||||||
output += f"{i}. {r['title']}\n"
|
|
||||||
output += f" {r['url']}\n"
|
title = r.get("title", "Kein Titel")
|
||||||
output += f" {r['snippet']}\n\n"
|
body = r.get("body", "")
|
||||||
|
href = r.get("href", "")
|
||||||
|
|
||||||
|
output += (
|
||||||
|
f"{i}. {title}\n"
|
||||||
|
f" {href}\n"
|
||||||
|
f" {body}\n\n"
|
||||||
|
)
|
||||||
|
|
||||||
return output
|
return output
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return f"Fehler bei der Internetsuche: {str(e)}"
|
return f"Fehler bei der Websuche: {e}"
|
||||||
|
|
||||||
|
|
||||||
# Tool-Beschreibung für MCP / Bedrock
|
|
||||||
web_search_tool = {
|
web_search_tool = {
|
||||||
|
"toolSpec": {
|
||||||
"name": "web_search",
|
"name": "web_search",
|
||||||
"description": "Sucht aktuelle Informationen im Internet. Nutze dies für Nachrichten, Wetter, Fakten, Preise, aktuelle Ereignisse usw.",
|
"description": (
|
||||||
|
"Durchsucht das Internet nach aktuellen Informationen, "
|
||||||
|
"Nachrichten, Fakten, Wetter, Preisen und Ereignissen."
|
||||||
|
),
|
||||||
"inputSchema": {
|
"inputSchema": {
|
||||||
"json": {
|
"json": {
|
||||||
"type": "object",
|
"type": "object",
|
||||||
"properties": {
|
"properties": {
|
||||||
"query": {
|
"query": {
|
||||||
"type": "string",
|
"type": "string",
|
||||||
"description": "Die Suchanfrage (z.B. 'Wetter Berlin heute', 'Aktueller Bitcoin Preis', 'Wer ist Bundeskanzler 2026')"
|
"description": "Suchanfrage"
|
||||||
},
|
},
|
||||||
"max_results": {
|
"max_results": {
|
||||||
"type": "integer",
|
"type": "integer",
|
||||||
"description": "Maximale Anzahl Ergebnisse (Standard: 5)",
|
"description": "Maximale Anzahl Ergebnisse",
|
||||||
"default": 5
|
"default": 5
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"required": ["query"]
|
"required": [
|
||||||
|
"query"
|
||||||
|
]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user