-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
36 lines (33 loc) · 1.24 KB
/
Copy pathtools.py
File metadata and controls
36 lines (33 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
from dotenv import load_dotenv
load_dotenv()
from langchain.tools import tool
import requests
from bs4 import BeautifulSoup
from tavily import TavilyClient
from rich import print
tavily = TavilyClient(api_key=os.getenv("TAVILY_API_KEY"))
@tool
def web_search(query: str) -> str:
"""Search the web for recent and reliable information on a topic. Returns Titles, URLs and Snippets."""
results = tavily.search(
query=query,
max_results=5,
topic="news",
days=30,
)
output = []
for r in results['results']:
output.append(f"Title: {r['title']}\n URL: {r['url']}\n snippet:{r['content'][:300]}\n")
return "\n--------------------------------------------------------------------------\n".join(output)
@tool
def scrape_url(url: str) -> str:
"""Scrape and return clean text content from a given URL for deeper reading."""
try:
resp = requests.get(url, timeout=8, headers={"User-Agent": "Mozilla/5.0"})
soup = BeautifulSoup(resp.text, "html.parser")
for tag in soup(["script", "style", "nav", "footer"]):
tag.decompose()
return soup.get_text(separator=" ", strip=True)[:3000]
except Exception as e:
return f"Could not scrape URL: {str(e)}"