Spaces:
Sleeping
Sleeping
Create WebSearchTool.py
Browse files- tools/WebSearchTool.py +17 -0
tools/WebSearchTool.py
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import requests
|
| 2 |
+
import os
|
| 3 |
+
|
| 4 |
+
TAVILY_API_KEY = "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")
|
| 5 |
+
|
| 6 |
+
class WebSearchTool(Tool):
|
| 7 |
+
name = "web_search"
|
| 8 |
+
description = "Search the web and return relevant results."
|
| 9 |
+
inputs = {"query": {"type": "string"}}
|
| 10 |
+
output_type = "string"
|
| 11 |
+
|
| 12 |
+
def forward(self, query: str) -> str:
|
| 13 |
+
url = "https://api.tavily.com/search"
|
| 14 |
+
payload = {"api_key": TAVILY_API_KEY, "query": query}
|
| 15 |
+
r = requests.post(url, json=payload)
|
| 16 |
+
data = r.json()
|
| 17 |
+
return data["answer"]
|