Spaces:
Running
Running
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -47,7 +47,7 @@ def _normalize_auth_header(token: str) -> str:
|
|
| 47 |
return f"Bearer {token}"
|
| 48 |
|
| 49 |
|
| 50 |
-
def _extract_api_results(payload: Dict[str, Any]) -> List[Dict[str,
|
| 51 |
# Try common response shapes
|
| 52 |
if not isinstance(payload, dict):
|
| 53 |
return []
|
|
@@ -94,7 +94,7 @@ def _extract_api_results(payload: Dict[str, Any]) -> List[Dict[str, str]]:
|
|
| 94 |
if snippet:
|
| 95 |
snippet = snippet[:WEB_SEARCH_SNIPPET_CHARS].strip()
|
| 96 |
|
| 97 |
-
results.append({"url": url, "title": title, "
|
| 98 |
|
| 99 |
# Keep API order, but prefer explicit position if provided
|
| 100 |
if any(r.get("position") is not None for r in results):
|
|
@@ -126,20 +126,16 @@ def _websearchapi_search(q: str) -> str:
|
|
| 126 |
data = resp.json()
|
| 127 |
results = _extract_api_results(data)
|
| 128 |
if not results:
|
| 129 |
-
return
|
| 130 |
|
| 131 |
output_lines: List[str] = []
|
| 132 |
-
for
|
| 133 |
-
title = item.get("title") or
|
| 134 |
-
url = item.get("url"
|
| 135 |
-
|
| 136 |
-
output_lines.append(
|
| 137 |
-
f"{idx}. {title}\n"
|
| 138 |
-
f" URL: {url}\n"
|
| 139 |
-
f" Summary: {snippet}"
|
| 140 |
-
)
|
| 141 |
|
| 142 |
-
return
|
| 143 |
|
| 144 |
|
| 145 |
def web_search(query: str) -> str:
|
|
|
|
| 47 |
return f"Bearer {token}"
|
| 48 |
|
| 49 |
|
| 50 |
+
def _extract_api_results(payload: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 51 |
# Try common response shapes
|
| 52 |
if not isinstance(payload, dict):
|
| 53 |
return []
|
|
|
|
| 94 |
if snippet:
|
| 95 |
snippet = snippet[:WEB_SEARCH_SNIPPET_CHARS].strip()
|
| 96 |
|
| 97 |
+
results.append({"url": url, "title": title, "description": snippet, "position": position})
|
| 98 |
|
| 99 |
# Keep API order, but prefer explicit position if provided
|
| 100 |
if any(r.get("position") is not None for r in results):
|
|
|
|
| 126 |
data = resp.json()
|
| 127 |
results = _extract_api_results(data)
|
| 128 |
if not results:
|
| 129 |
+
return "No results found."
|
| 130 |
|
| 131 |
output_lines: List[str] = []
|
| 132 |
+
for item in results[: max(1, WEB_SEARCH_RESULTS)]:
|
| 133 |
+
title = item.get("title") or ""
|
| 134 |
+
url = item.get("url") or ""
|
| 135 |
+
description = item.get("description") or ""
|
| 136 |
+
output_lines.append(f"title: {title}\nurl: {url}\ndescription: {description}")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 137 |
|
| 138 |
+
return "\n\n".join(output_lines)
|
| 139 |
|
| 140 |
|
| 141 |
def web_search(query: str) -> str:
|