Download search_tool.py from zrxarchit/inference: direct link, hf CLI and curl.
- Browser
- Download file 2.09 kB
-
https://huggingface.co/spaces/zrxarchit/inference/resolve/main/search_tool.py
- Command line
-
hf download hf://spaces/zrxarchit/inference/search_tool.py
-
curl -L -o search_tool.py https://huggingface.co/spaces/zrxarchit/inference/resolve/main/search_tool.py
2.09 kB
| #!/usr/bin/env python3 | |
| import os | |
| import sys | |
| import json | |
| import requests | |
| def search(query: str, count: int = 5, summary: bool = True, freshness: str = "noLimit") -> dict: | |
| api_key = os.environ.get("LANGSEARCH_API_KEY", "").strip() | |
| if not api_key: | |
| raise SystemExit("LANGSEARCH_API_KEY environment variable not set") | |
| url = "https://api.langsearch.com/v1/web-search" | |
| headers = { | |
| "Authorization": f"Bearer {api_key}", | |
| "Content-Type": "application/json" | |
| } | |
| payload = { | |
| "query": query, | |
| "freshness": freshness, | |
| "summary": summary, | |
| "count": min(count, 10) | |
| } | |
| try: | |
| response = requests.post(url, headers=headers, json=payload, timeout=30) | |
| response.raise_for_status() | |
| data = response.json() | |
| if data.get("code") == 200: | |
| results = [] | |
| web_pages = data.get("data", {}).get("webPages", {}).get("value", []) | |
| for page in web_pages: | |
| results.append({ | |
| "name": page.get("name", ""), | |
| "url": page.get("url", ""), | |
| "snippet": page.get("snippet", ""), | |
| "summary": page.get("summary", "") if summary else "" | |
| }) | |
| return {"success": True, "query": query, "results": results} | |
| else: | |
| return {"success": False, "error": data.get("msg", "Unknown error")} | |
| except requests.RequestException as e: | |
| return {"success": False, "error": str(e)} | |
| if __name__ == "__main__": | |
| if len(sys.argv) < 2: | |
| print("Usage: search_tool.py '<query>' [count] [summary] [freshness]") | |
| print("Example: search_tool.py 'Apple M4 chip' 5 true noLimit") | |
| sys.exit(1) | |
| query = sys.argv[1] | |
| count = int(sys.argv[2]) if len(sys.argv) > 2 else 5 | |
| summary = sys.argv[3].lower() == "true" if len(sys.argv) > 3 else True | |
| freshness = sys.argv[4] if len(sys.argv) > 4 else "noLimit" | |
| result = search(query, count, summary, freshness) | |
| print(json.dumps(result, indent=2)) | |