Spaces:
Sleeping
Sleeping
File size: 1,817 Bytes
713bdac f66d8b7 713bdac f66d8b7 713bdac f66d8b7 713bdac f66d8b7 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
from tavily import TavilyClient
from utils import clean_text
from langchain_core.tools import tool
import os
from constants import TAVILY_KEY
@tool
def internet_search(query: str, max_results: int = 10) -> list[dict]:
"""
Search the Internet for the given query and return a list of results.
Args:
query (str): The search query string.
max_results (int, optional): Maximum number of search results to return. Default is 10.
Returns:
list[dict]: A list of dictionaries containing search results. Each dictionary typically includes:
- 'title': The title of the result.
- 'href': The URL of the result.
- 'body': A short snippet or description.
Raises:
ValueError: If the query is empty or only whitespace.
"""
if not query.strip():
raise ValueError("Query must not be empty.")
client = TavilyClient(api_key=TAVILY_KEY)
search_results = client.search(query=query, max_results=max_results)
results = []
for r in search_results["results"]:
results.append({
"title": r.get("title", ""),
"href": r.get("url", ""),
"body": r.get("content", "")
})
return results
if __name__ == "__main__":
query = 'Alexandre Gazola'
try:
results = internet_search.invoke(
{
"query": query,
"max_results": 5
}
)
for i, result in enumerate(results, 1):
print(f"\nResult {i}:")
print(f"Title: {clean_text(result.get('title'))}")
print(f"URL: {clean_text(result.get('href'))}")
print(f"Snippet: {clean_text(result.get('body'))}")
except ValueError as e:
print(f"Error: {e}")
|