|
from langchain.tools import tool |
|
|
|
import cmath |
|
@tool(description="Multiplies two numbers.") |
|
def multiply(a: float, b: float) -> float: |
|
return a * b |
|
|
|
@tool(description="Adds two numbers.") |
|
def add(a: float, b: float) -> float: |
|
return a + b |
|
|
|
@tool(description="Subtracts two numbers.") |
|
def subtract(a: float, b: float) -> int: |
|
return a - b |
|
|
|
@tool(description="Divides two numbers.") |
|
def divide(a: float, b: float) -> float: |
|
if b == 0: |
|
raise ValueError("Cannot divided by zero.") |
|
return a / b |
|
|
|
@tool(description="Get the modulus of two numbers.") |
|
def modulus(a: int, b: int) -> int: |
|
return a % b |
|
|
|
@tool(description="Get the power of two numbers.") |
|
def power(a: float, b: float) -> float: |
|
return a**b |
|
|
|
@tool(description="Get the square root of a number.") |
|
def square_root(a: float) -> float | complex: |
|
if a >= 0: |
|
return a**0.5 |
|
return cmath.sqrt(a) |
|
|
|
from langchain_community.document_loaders import WikipediaLoader |
|
|
|
@tool(description="Search Wikipedia for a query and return up to 2 results.") |
|
def wiki_search(query: str) -> dict: |
|
print(f"Searching Wikipedia for: {query}") |
|
|
|
try: |
|
|
|
search_docs = WikipediaLoader(query=query, load_max_docs=2).load() |
|
|
|
|
|
formatted_search_docs = "\n\n---\n\n".join( |
|
[ |
|
f'<Document source="{doc.metadata.get("source", "")}" page="{doc.metadata.get("page", "")}">\n{doc.page_content}\n</Document>' |
|
for doc in search_docs |
|
] |
|
) |
|
|
|
print(f"Here are the outputs:\n{formatted_search_docs}") |
|
return {"wiki_results": formatted_search_docs} |
|
|
|
except Exception as e: |
|
print(f"Error occurred: {e}") |
|
return {"wiki_results": f"None found for {query}, use the results from the other tools or similar questions obtained earlier."} |
|
|
|
|
|
from langchain_community.document_loaders import ArxivLoader |
|
@tool(description="Search Arxiv for a query and return maximum 3 result.") |
|
def arvix_search(query: str) -> str: |
|
search_docs = ArxivLoader(query=query, load_max_docs=3).load() |
|
formatted_search_docs = "\n\n---\n\n".join( |
|
[ |
|
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content[:1000]}</Document>' |
|
for doc in search_docs |
|
]) |
|
return {"arvix_results": formatted_search_docs} |
|
|
|
from langchain_community.tools.tavily_search import TavilySearchResults |
|
@tool(description="Search Tavily for a query and return maximum 3 results.") |
|
def web_search(query: str) -> str: |
|
search_docs = TavilySearchResults(max_results=3).invoke(query=query) |
|
formatted_search_docs = "\n\n---\n\n".join( |
|
[ |
|
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content}</Document>' |
|
for doc in search_docs |
|
]) |
|
return {"web_results": formatted_search_docs} |
|
|
|
from upload_metadata_n_setup_retrivers import retriever |
|
def vector_search(query:str) -> str: |
|
search_docs = retriever.invoke(input=query) |
|
formatted_search_docs = "\n\n---\n\n".join( |
|
[ |
|
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content}</Document>' |
|
for doc in search_docs |
|
]) |
|
return formatted_search_docs |