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 @tool def multiply(a: int, b: int) -> int: """Multiply two numbers. Args: a: first int b: second int """ return a * b @tool def add(a: int, b: int) -> int: """Add two numbers. Args: a: first int b: second int """ return a + b @tool def subtract(a: int, b: int) -> int: """Subtract two numbers. Args: a: first int b: second int """ return a - b @tool 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 @tool def modulus(a: int, b: int) -> int: """Get the modulus of two numbers. Args: a: first int b: second int """ return a % b @tool 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'\n{doc.page_content}\n' for doc in search_docs ]) return {"wiki_results": formatted_search_docs} @tool 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'\n{doc.get("content", "")}\n' for doc in search_docs ]) return {"web_results": formatted_search_docs} @tool 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'\n{doc.get("content", "")}\n' for doc in search_docs ]) return {"web_results": formatted_search_docs} @tool 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 ]