Update app.py
Browse files
app.py
CHANGED
|
@@ -14,6 +14,8 @@ from bs4 import BeautifulSoup
|
|
| 14 |
from pydantic import BaseModel, Field
|
| 15 |
import nest_asyncio
|
| 16 |
import requests
|
|
|
|
|
|
|
| 17 |
|
| 18 |
#from agents.extensions.models.litellm_model import LitellmModel
|
| 19 |
|
|
@@ -29,26 +31,44 @@ nest_asyncio.apply()
|
|
| 29 |
#Tools
|
| 30 |
|
| 31 |
@function_tool
|
| 32 |
-
def
|
| 33 |
"""
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
Returns:
|
| 38 |
-
str: Formatted search results.
|
| 39 |
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
try:
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
formatted.append(f"**Title**: {result['title']}\n**URL**: {result['url']}\n**Content**: {result['content']}\n")
|
| 47 |
|
| 48 |
-
return
|
| 49 |
|
| 50 |
except Exception as e:
|
| 51 |
-
return f"Error
|
| 52 |
|
| 53 |
|
| 54 |
@function_tool
|
|
|
|
| 14 |
from pydantic import BaseModel, Field
|
| 15 |
import nest_asyncio
|
| 16 |
import requests
|
| 17 |
+
from tavily import TavilyClient
|
| 18 |
+
import re
|
| 19 |
|
| 20 |
#from agents.extensions.models.litellm_model import LitellmModel
|
| 21 |
|
|
|
|
| 31 |
#Tools
|
| 32 |
|
| 33 |
@function_tool
|
| 34 |
+
def visit_website(url: str) -> str:
|
| 35 |
"""
|
| 36 |
+
Extracts the main readable contents of a website at the given URL,
|
| 37 |
+
formats as markdown, and returns it as a string.
|
| 38 |
+
If there is an error, returns a concise error message.
|
|
|
|
|
|
|
| 39 |
"""
|
| 40 |
+
headers = {
|
| 41 |
+
"User-Agent": (
|
| 42 |
+
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) "
|
| 43 |
+
"AppleWebKit/537.36 (KHTML, like Gecko) "
|
| 44 |
+
"Chrome/91.0.4472.124 Safari/537.36"
|
| 45 |
+
)
|
| 46 |
+
}
|
| 47 |
try:
|
| 48 |
+
response = requests.get(url, headers=headers, timeout=10)
|
| 49 |
+
response.raise_for_status()
|
| 50 |
+
|
| 51 |
+
html_content = response.text
|
| 52 |
+
soup = BeautifulSoup(html_content, "html.parser")
|
| 53 |
+
|
| 54 |
+
# Remove unwanted tags for clarity
|
| 55 |
+
for tag in soup(["script", "style", "nav", "header", "footer", "aside", "meta"]):
|
| 56 |
+
tag.decompose()
|
| 57 |
+
|
| 58 |
+
# Extract main content; fallback to all text if .body missing
|
| 59 |
+
main_content = soup.body if soup.body else soup
|
| 60 |
+
markdown_text = markdownify(
|
| 61 |
+
str(main_content),
|
| 62 |
+
strip=["img", "iframe", "script", "meta", "button", "input", "svg"]
|
| 63 |
+
)
|
| 64 |
|
| 65 |
+
max_length = 5000 # Reduce if hitting timeouts or agent tool output limits
|
| 66 |
+
markdown_text = re.sub(r"\n\s*\n", "\n\n", markdown_text[:max_length])
|
|
|
|
| 67 |
|
| 68 |
+
return markdown_text.strip() if markdown_text else "No readable text found on this page."
|
| 69 |
|
| 70 |
except Exception as e:
|
| 71 |
+
return f"Error fetching the website: {e}"
|
| 72 |
|
| 73 |
|
| 74 |
@function_tool
|