Create other_tools.py
Browse files- other_tools.py +92 -0
other_tools.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from langchain.tools import tool
|
| 2 |
+
#mathematical operations
|
| 3 |
+
import cmath
|
| 4 |
+
@tool(description="Multiplies two numbers.")
|
| 5 |
+
def multiply(a: float, b: float) -> float:
|
| 6 |
+
return a * b
|
| 7 |
+
|
| 8 |
+
@tool(description="Adds two numbers.")
|
| 9 |
+
def add(a: float, b: float) -> float:
|
| 10 |
+
return a + b
|
| 11 |
+
|
| 12 |
+
@tool(description="Subtracts two numbers.")
|
| 13 |
+
def subtract(a: float, b: float) -> int:
|
| 14 |
+
return a - b
|
| 15 |
+
|
| 16 |
+
@tool(description="Divides two numbers.")
|
| 17 |
+
def divide(a: float, b: float) -> float:
|
| 18 |
+
if b == 0:
|
| 19 |
+
raise ValueError("Cannot divided by zero.")
|
| 20 |
+
return a / b
|
| 21 |
+
|
| 22 |
+
@tool(description="Get the modulus of two numbers.")
|
| 23 |
+
def modulus(a: int, b: int) -> int:
|
| 24 |
+
return a % b
|
| 25 |
+
|
| 26 |
+
@tool(description="Get the power of two numbers.")
|
| 27 |
+
def power(a: float, b: float) -> float:
|
| 28 |
+
return a**b
|
| 29 |
+
|
| 30 |
+
@tool(description="Get the square root of a number.")
|
| 31 |
+
def square_root(a: float) -> float | complex:
|
| 32 |
+
if a >= 0:
|
| 33 |
+
return a**0.5
|
| 34 |
+
return cmath.sqrt(a)
|
| 35 |
+
|
| 36 |
+
from langchain_community.document_loaders import WikipediaLoader
|
| 37 |
+
|
| 38 |
+
@tool(description="Search Wikipedia for a query and return up to 2 results.")
|
| 39 |
+
def wiki_search(query: str) -> dict:
|
| 40 |
+
print(f"Searching Wikipedia for: {query}")
|
| 41 |
+
|
| 42 |
+
try:
|
| 43 |
+
# Load up to 2 documents from Wikipedia
|
| 44 |
+
search_docs = WikipediaLoader(query=query, load_max_docs=2).load()
|
| 45 |
+
|
| 46 |
+
# Format the output for each document
|
| 47 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 48 |
+
[
|
| 49 |
+
f'<Document source="{doc.metadata.get("source", "")}" page="{doc.metadata.get("page", "")}">\n{doc.page_content}\n</Document>'
|
| 50 |
+
for doc in search_docs
|
| 51 |
+
]
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
print(f"Here are the outputs:\n{formatted_search_docs}")
|
| 55 |
+
return {"wiki_results": formatted_search_docs}
|
| 56 |
+
|
| 57 |
+
except Exception as e:
|
| 58 |
+
print(f"Error occurred: {e}")
|
| 59 |
+
return {"wiki_results": f"None found for {query}, use the results from the other tools or similar questions obtained earlier."}
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
from langchain_community.document_loaders import ArxivLoader
|
| 63 |
+
@tool(description="Search Arxiv for a query and return maximum 3 result.")
|
| 64 |
+
def arvix_search(query: str) -> str:
|
| 65 |
+
search_docs = ArxivLoader(query=query, load_max_docs=3).load()
|
| 66 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 67 |
+
[
|
| 68 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content[:1000]}</Document>'
|
| 69 |
+
for doc in search_docs
|
| 70 |
+
])
|
| 71 |
+
return {"arvix_results": formatted_search_docs}
|
| 72 |
+
|
| 73 |
+
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 74 |
+
@tool(description="Search Tavily for a query and return maximum 3 results.")
|
| 75 |
+
def web_search(query: str) -> str:
|
| 76 |
+
search_docs = TavilySearchResults(max_results=3).invoke(query=query)
|
| 77 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 78 |
+
[
|
| 79 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content}</Document>'
|
| 80 |
+
for doc in search_docs
|
| 81 |
+
])
|
| 82 |
+
return {"web_results": formatted_search_docs}
|
| 83 |
+
|
| 84 |
+
from upload_metadata_n_setup_retrivers import retriever
|
| 85 |
+
def vector_search(query:str) -> str:
|
| 86 |
+
search_docs = retriever.invoke(input=query)
|
| 87 |
+
formatted_search_docs = "\n\n---\n\n".join(
|
| 88 |
+
[
|
| 89 |
+
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>{doc.page_content}</Document>'
|
| 90 |
+
for doc in search_docs
|
| 91 |
+
])
|
| 92 |
+
return formatted_search_docs
|