Download tools.py from rsobieski/Final_Assignment_Agents_Course: direct link, hf CLI and curl.
- Browser
- Download file 1.63 kB
-
https://huggingface.co/spaces/rsobieski/Final_Assignment_Agents_Course/resolve/main/tools.py
- Command line
-
hf download hf://spaces/rsobieski/Final_Assignment_Agents_Course/tools.py
-
curl -L -o tools.py https://huggingface.co/spaces/rsobieski/Final_Assignment_Agents_Course/resolve/main/tools.py
1.63 kB
| from langchain_core.tools import tool | |
| from langchain_community.document_loaders import WikipediaLoader, ArxivLoader | |
| from langchain_community.tools.tavily_search import TavilySearchResults | |
| def multiply(a: int, b: int) -> int: | |
| """Multiply two numbers.""" | |
| return a * b | |
| def add(a: int, b: int) -> int: | |
| """Add two numbers.""" | |
| return a + b | |
| def subtract(a: int, b: int) -> int: | |
| """Subtract two numbers.""" | |
| return a - b | |
| def divide(a: int, b: int) -> float: | |
| """Divide two numbers.""" | |
| if b == 0: | |
| raise ValueError("Cannot divide by zero.") | |
| return a / b | |
| def modulus(a: int, b: int) -> int: | |
| """Get modulus of two numbers.""" | |
| return a % b | |
| def wiki_search(query: str) -> str: | |
| """Search Wikipedia for a query and return top results.""" | |
| docs = WikipediaLoader(query=query, load_max_docs=2).load() | |
| return "\n\n".join(doc.page_content for doc in docs) | |
| def arxiv_search(query: str) -> str: | |
| """Search Arxiv for a query and return top results.""" | |
| docs = ArxivLoader(query=query, load_max_docs=2).load() | |
| return "\n\n".join(doc.page_content[:1000] for doc in docs) | |
| def web_search(query: str) -> str: | |
| """Search the web using Tavily.""" | |
| docs = TavilySearchResults(max_results=3).invoke(query=query) | |
| return "\n\n".join(doc.page_content for doc in docs) | |
| # Tools as a dict for manual agent usage | |
| TOOLS = { | |
| "multiply": multiply, | |
| "add": add, | |
| "subtract": subtract, | |
| "divide": divide, | |
| "modulus": modulus, | |
| "wiki_search": wiki_search, | |
| "arxiv_search": arxiv_search, | |
| "web_search": web_search, | |
| } | |