add tavily_search
Browse files- my_tools.py +49 -2
my_tools.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
from smolagents import DuckDuckGoSearchTool, Tool
|
| 2 |
import wikipediaapi
|
| 3 |
|
| 4 |
class WikipediaSearchTool(Tool):
|
|
@@ -17,4 +17,51 @@ class WikipediaSearchTool(Tool):
|
|
| 17 |
return "No Wikipedia page found."
|
| 18 |
return page.summary[:1000]
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from smolagents import DuckDuckGoSearchTool, Tool,tool
|
| 2 |
import wikipediaapi
|
| 3 |
|
| 4 |
class WikipediaSearchTool(Tool):
|
|
|
|
| 17 |
return "No Wikipedia page found."
|
| 18 |
return page.summary[:1000]
|
| 19 |
|
| 20 |
+
@tool
|
| 21 |
+
def tavily_search(query: str) -> str:
|
| 22 |
+
"""
|
| 23 |
+
Search the web using Tavily API
|
| 24 |
+
Args:
|
| 25 |
+
query: The search query
|
| 26 |
+
Returns:
|
| 27 |
+
Search results as formatted text
|
| 28 |
+
"""
|
| 29 |
+
api_key = os.getenv("TAVILY_API_KEY")
|
| 30 |
+
if not api_key:
|
| 31 |
+
return "Tavily API key not found"
|
| 32 |
+
|
| 33 |
+
url = "https://api.tavily.com/search"
|
| 34 |
+
payload = {
|
| 35 |
+
"api_key": api_key,
|
| 36 |
+
"query": query,
|
| 37 |
+
"search_depth": "basic",
|
| 38 |
+
"include_answer": True,
|
| 39 |
+
"include_domains": [],
|
| 40 |
+
"exclude_domains": [],
|
| 41 |
+
"max_results": 5
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
response = requests.post(url, json=payload, timeout=10)
|
| 46 |
+
response.raise_for_status()
|
| 47 |
+
data = response.json()
|
| 48 |
+
|
| 49 |
+
results = []
|
| 50 |
+
if data.get("answer"):
|
| 51 |
+
results.append(f"Quick Answer: {data['answer']}")
|
| 52 |
+
|
| 53 |
+
for result in data.get("results", [])[:3]:
|
| 54 |
+
results.append(f"Title: {result.get('title', 'N/A')}")
|
| 55 |
+
results.append(f"URL: {result.get('url', 'N/A')}")
|
| 56 |
+
results.append(f"Content: {result.get('content', 'N/A')[:200]}...")
|
| 57 |
+
results.append("---")
|
| 58 |
+
|
| 59 |
+
return "\n".join(results)
|
| 60 |
+
except Exception as e:
|
| 61 |
+
return f"Tavily search error: {str(e)}"
|
| 62 |
+
|
| 63 |
+
my_tool_list = [
|
| 64 |
+
WikipediaSearchTool(),
|
| 65 |
+
DuckDuckGoSearchTool()
|
| 66 |
+
tavily_search,
|
| 67 |
+
]
|