AlexTrinityBlock's picture
feat(tools): add http_get tool for web content fetching
c36efba
import httpx
from colorama import Fore, Style # type: ignore[import]
from langchain_core.tools import tool
HTTP_HEADERS = {
"User-Agent": (
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
"AppleWebKit/537.36 (KHTML, like Gecko) "
"Chrome/120.0.0.0 Safari/537.36"
)
}
@tool
def http_get(url: str) -> str:
"""Send an HTTP GET request to the given URL and return the response text.
Useful for fetching web pages, API responses, or any text-based content
from the internet.
Args:
url: The URL to send the GET request to.
Returns:
The response body as text, or an error message on failure.
"""
try:
print(f"{Fore.CYAN}[HTTP GET] {url}{Style.RESET_ALL}")
resp = httpx.get(url, headers=HTTP_HEADERS, follow_redirects=True, timeout=60)
resp.raise_for_status()
print(f"{Fore.CYAN}[HTTP GET] Status: {resp.status_code} | "
f"Length: {len(resp.text)} chars{Style.RESET_ALL}")
return resp.text
except Exception as e:
return f"Error fetching URL: {e}"