rsobieski's picture
Update tools.py
95f4932 verified
from langchain_core.tools import tool
from langchain_community.document_loaders import WikipediaLoader, ArxivLoader
from langchain_community.tools.tavily_search import TavilySearchResults
@tool
def multiply(a: int, b: int) -> int:
"""Multiply two numbers."""
return a * b
@tool
def add(a: int, b: int) -> int:
"""Add two numbers."""
return a + b
@tool
def subtract(a: int, b: int) -> int:
"""Subtract two numbers."""
return a - b
@tool
def divide(a: int, b: int) -> float:
"""Divide two numbers."""
if b == 0:
raise ValueError("Cannot divide by zero.")
return a / b
@tool
def modulus(a: int, b: int) -> int:
"""Get modulus of two numbers."""
return a % b
@tool
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)
@tool
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)
@tool
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,
}