File size: 1,315 Bytes
0fd7e61 |
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 |
from typing import List, Optional
from tavily import TavilyClient
from langchain_core.tools import tool
import time
client =TavilyClient('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')
def search(query: str, topic: Optional[str] = None, max_results: int = 5) -> List[str]:
"""
Perform a Tavily search and return only the extracted content from results.
Args:
query (str): The search query string.
topic (Optional[str]): (Optional) Topic/domain to refine the search.
max_results (int): Maximum number of search results to retrieve.
Returns:
List[str]: A list of content snippets extracted from the search results.
"""
try:
response = client.search(
query=query,
topic=topic,
max_results=max_results,
search_depth="advanced",
)
# Extract only "content" fields safely
contents = [
result.get("content", "").strip()
for result in response.get("results", [])
if result.get("content")
]
return contents if contents else ["No content found."]
except Exception as e:
return [f"Search failed: {str(e)}"]
def get_time():
return time.time()
print(get_time())
|