Download tools.py from agents-course/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F252/tools.py
- Command line
-
hf download hf://spaces/agents-course/Final_Assignment_Template@refs/pr/252/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/agents-course/Final_Assignment_Template/resolve/refs%2Fpr%2F252/tools.py
1.32 kB
| 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()) | |