Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
import wikipediaapi
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
|
| 5 |
|
| 6 |
@tool
|
|
@@ -88,3 +89,37 @@ def search_wikipedia(page_title: str, language: str) -> str:
|
|
| 88 |
|
| 89 |
except Exception as e:
|
| 90 |
return f"Error retrieving Wikipedia content: {str(e)}"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
import wikipediaapi
|
| 3 |
import pandas as pd
|
| 4 |
+
import requests
|
| 5 |
|
| 6 |
|
| 7 |
@tool
|
|
|
|
| 89 |
|
| 90 |
except Exception as e:
|
| 91 |
return f"Error retrieving Wikipedia content: {str(e)}"
|
| 92 |
+
|
| 93 |
+
@tool
|
| 94 |
+
def duckduckgo_search(query: str) -> str:
|
| 95 |
+
"""Use DuckDuckGo to search the web for up-to-date information.
|
| 96 |
+
Args:
|
| 97 |
+
query: The query to search for on the web
|
| 98 |
+
"""
|
| 99 |
+
url = "https://api.duckduckgo.com/"
|
| 100 |
+
params = {
|
| 101 |
+
"q": query,
|
| 102 |
+
"format": "json",
|
| 103 |
+
"no_redirect": 1,
|
| 104 |
+
"no_html": 1,
|
| 105 |
+
"skip_disambig": 1,
|
| 106 |
+
}
|
| 107 |
+
|
| 108 |
+
try:
|
| 109 |
+
response = requests.get(url, params=params)
|
| 110 |
+
data = response.json()
|
| 111 |
+
|
| 112 |
+
# Try the most useful fields
|
| 113 |
+
if data.get("AbstractText"):
|
| 114 |
+
return data["AbstractText"]
|
| 115 |
+
elif data.get("Answer"):
|
| 116 |
+
return data["Answer"]
|
| 117 |
+
elif data.get("RelatedTopics"):
|
| 118 |
+
# Return some related results
|
| 119 |
+
results = data["RelatedTopics"][:3]
|
| 120 |
+
return "\n".join(rt.get("Text", "") for rt in results if "Text" in rt)
|
| 121 |
+
else:
|
| 122 |
+
return "No good results found."
|
| 123 |
+
|
| 124 |
+
except Exception as e:
|
| 125 |
+
return f"Search failed: {e}"
|