rsobieski's picture
Update tools.py
7c51b43 verified
Raw
History Blame
1.53 kB
"""Tool definitions for LangGraph agent."""
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)
# Export list of all tools
TOOLS = [multiply, add, subtract, divide, modulus, wiki_search, arxiv_search, web_search]