Spaces:
Configuration error
Configuration error
Download tools.py from manaankit/hf-final-assignment-agents-course: direct link, hf CLI and curl.
- Browser
- Download file 2.88 kB
-
https://huggingface.co/spaces/manaankit/hf-final-assignment-agents-course/resolve/main/tools.py
- Command line
-
hf download hf://spaces/manaankit/hf-final-assignment-agents-course/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/manaankit/hf-final-assignment-agents-course/resolve/main/tools.py
2.88 kB
| from langchain_core.tools import tool | |
| from langchain_community.document_loaders import WikipediaLoader, YoutubeLoader | |
| from langchain_community.tools import DuckDuckGoSearchResults | |
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| import os | |
| def multiply(a: int, b: int) -> int: | |
| """Multiply two numbers. | |
| Args: | |
| a: first int | |
| b: second int | |
| """ | |
| return a * b | |
| def add(a: int, b: int) -> int: | |
| """Add two numbers. | |
| Args: | |
| a: first int | |
| b: second int | |
| """ | |
| return a + b | |
| def subtract(a: int, b: int) -> int: | |
| """Subtract two numbers. | |
| Args: | |
| a: first int | |
| b: second int | |
| """ | |
| return a - b | |
| def divide(a: int, b: int) -> float: | |
| """Divide two numbers. | |
| Args: | |
| a: first int | |
| b: second int | |
| """ | |
| if b == 0: | |
| raise ValueError("Cannot divide by zero.") | |
| return a / b | |
| def modulus(a: int, b: int) -> int: | |
| """Get the modulus of two numbers. | |
| Args: | |
| a: first int | |
| b: second int | |
| """ | |
| return a % b | |
| def wiki_search(query: str) -> dict: | |
| """Search Wikipedia for a query and return maximum 2 results. | |
| Args: | |
| query: The search query.""" | |
| search_docs = WikipediaLoader(query=query, load_max_docs=2).load() | |
| formatted_search_docs = "\n\n---\n\n".join( | |
| [ | |
| f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>' | |
| for doc in search_docs | |
| ]) | |
| return {"wiki_results": formatted_search_docs} | |
| def web_search_tavily(query: str) -> dict: | |
| """Search Tavily for a query and return maximum 3 results. | |
| Args: | |
| query: The search query.""" | |
| search_docs = TavilySearchResults(max_results=3).invoke({'query': query}) | |
| formatted_search_docs = "\n\n---\n\n".join( | |
| [ | |
| f'<Document>\n{doc.get("content", "")}\n</Document>' | |
| for doc in search_docs | |
| ]) | |
| return {"web_results": formatted_search_docs} | |
| def web_search_duckduckgo(query: str) -> dict: | |
| """Search DuckDuckGo for a query and return maximum 3 results. | |
| Args: | |
| query: The search query.""" | |
| search_docs = DuckDuckGoSearchResults(output_format="list", max_results=3).invoke(input=query) | |
| formatted_search_docs = "\n\n---\n\n".join( | |
| [ | |
| f'<Document>\n{doc.get("content", "")}\n</Document>' | |
| for doc in search_docs | |
| ]) | |
| return {"web_results": formatted_search_docs} | |
| def youtube_loader(youtube_url: str) -> dict: | |
| """Elaborate a YouTube video to transcript the content, return the transcript. | |
| Args: | |
| youtube_url: The YouTube video URL.""" | |
| loader = YoutubeLoader.from_youtube_url( | |
| youtube_url, | |
| add_video_info=True | |
| ) | |
| return {"youtube_transcript": loader.load()} | |
| tools = [ | |
| multiply, | |
| add, | |
| subtract, | |
| divide, | |
| modulus, | |
| wiki_search, | |
| web_search_duckduckgo if os.getenv("USE_DDG", "false") == "true" else web_search_tavily, | |
| youtube_loader | |
| ] |