added websearch tool
This commit is contained in:
@@ -0,0 +1,66 @@
|
|||||||
|
import requests
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
async def web_search(query: str, max_results: int = 5):
|
||||||
|
"""
|
||||||
|
Sucht im Internet und gibt aktuelle, relevante Ergebnisse zurück.
|
||||||
|
"""
|
||||||
|
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)
|
||||||
|
data = response.json()
|
||||||
|
|
||||||
|
results = []
|
||||||
|
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:
|
||||||
|
return "Keine relevanten Suchergebnisse gefunden."
|
||||||
|
|
||||||
|
output = f"Suchergebnisse für '{query}' (Stand: {datetime.now().strftime('%d.%m.%Y %H:%M')}):\n\n"
|
||||||
|
for i, r in enumerate(results, 1):
|
||||||
|
output += f"{i}. {r['title']}\n"
|
||||||
|
output += f" {r['url']}\n"
|
||||||
|
output += f" {r['snippet']}\n\n"
|
||||||
|
|
||||||
|
return output
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
return f"Fehler bei der Internetsuche: {str(e)}"
|
||||||
|
|
||||||
|
|
||||||
|
# Tool-Beschreibung für MCP / Bedrock
|
||||||
|
web_search_tool = {
|
||||||
|
"name": "web_search",
|
||||||
|
"description": "Sucht aktuelle Informationen im Internet. Nutze dies für Nachrichten, Wetter, Fakten, Preise, aktuelle Ereignisse usw.",
|
||||||
|
"inputSchema": {
|
||||||
|
"json": {
|
||||||
|
"type": "object",
|
||||||
|
"properties": {
|
||||||
|
"query": {
|
||||||
|
"type": "string",
|
||||||
|
"description": "Die Suchanfrage (z.B. 'Wetter Berlin heute', 'Aktueller Bitcoin Preis', 'Wer ist Bundeskanzler 2026')"
|
||||||
|
},
|
||||||
|
"max_results": {
|
||||||
|
"type": "integer",
|
||||||
|
"description": "Maximale Anzahl Ergebnisse (Standard: 5)",
|
||||||
|
"default": 5
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"required": ["query"]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user