Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -182,6 +182,75 @@ async def list_retrievers(source: str = None) -> dict:
|
|
| 182 |
"source_used": "none"
|
| 183 |
}
|
| 184 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
# Configuraci贸n de la interfaz Gradio
|
| 186 |
demo = gr.TabbedInterface(
|
| 187 |
[
|
|
|
|
| 182 |
"source_used": "none"
|
| 183 |
}
|
| 184 |
|
| 185 |
+
|
| 186 |
+
|
| 187 |
+
async def search_tavily(
|
| 188 |
+
query: str,
|
| 189 |
+
days: int = 7,
|
| 190 |
+
max_results: int = 1,
|
| 191 |
+
include_answer: bool = False
|
| 192 |
+
) -> dict:
|
| 193 |
+
"""Perform a web search using the Tavily API.
|
| 194 |
+
|
| 195 |
+
Args:
|
| 196 |
+
query: Search query string (required)
|
| 197 |
+
days: Restrict search to last N days (default: 7)
|
| 198 |
+
max_results: Maximum results to return (default: 1)
|
| 199 |
+
include_answer: Include a direct answer only when requested by the user (default: False)
|
| 200 |
+
|
| 201 |
+
Returns:
|
| 202 |
+
dict: Search results from Tavily
|
| 203 |
+
"""
|
| 204 |
+
# Obtener la API key de las variables de entorno
|
| 205 |
+
tavily_api_key = os.environ.get('TAVILY_API_KEY')
|
| 206 |
+
if not tavily_api_key:
|
| 207 |
+
raise ValueError("TAVILY_API_KEY environment variable not set")
|
| 208 |
+
|
| 209 |
+
headers = {
|
| 210 |
+
"Authorization": f"Bearer {tavily_api_key}",
|
| 211 |
+
"Content-Type": "application/json"
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
payload = {
|
| 215 |
+
"query": query,
|
| 216 |
+
"search_depth": "basic",
|
| 217 |
+
"max_results": max_results,
|
| 218 |
+
"days": days if days else None,
|
| 219 |
+
"include_answer": include_answer
|
| 220 |
+
}
|
| 221 |
+
|
| 222 |
+
try:
|
| 223 |
+
async with aiohttp.ClientSession() as session:
|
| 224 |
+
async with session.post(
|
| 225 |
+
"https://api.tavily.com/search",
|
| 226 |
+
headers=headers,
|
| 227 |
+
json=payload
|
| 228 |
+
) as response:
|
| 229 |
+
response.raise_for_status()
|
| 230 |
+
result = await response.json()
|
| 231 |
+
return result
|
| 232 |
+
|
| 233 |
+
except Exception as e:
|
| 234 |
+
return {
|
| 235 |
+
"error": str(e),
|
| 236 |
+
"status": "failed",
|
| 237 |
+
"query": query
|
| 238 |
+
}
|
| 239 |
+
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
|
| 243 |
+
|
| 244 |
+
|
| 245 |
+
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
|
| 249 |
+
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
|
| 253 |
+
|
| 254 |
# Configuraci贸n de la interfaz Gradio
|
| 255 |
demo = gr.TabbedInterface(
|
| 256 |
[
|