-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.py
More file actions
37 lines (30 loc) · 1.13 KB
/
Copy pathtools.py
File metadata and controls
37 lines (30 loc) · 1.13 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
from langchain.tools import tool
import requests
from bs4 import BeautifulSoup
from tavily import TavilyClient
import os
from dotenv import load_dotenv
from rich import print
load_dotenv()
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)
out = []
for r in results['results']:
out.append(
f"Title: {r['title']}\nURL: {r['url']}\nSnippet: {r['content'][:300]}\n"
)
return "\n----\n".join(out)
@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)}"