File size: 1,532 Bytes
7c51b43 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 | """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]
|