File size: 3,403 Bytes
3d5be75
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
from langchain.tools import tool
#mathematical operations
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:
        # Load up to 2 documents from Wikipedia
        search_docs = WikipediaLoader(query=query, load_max_docs=2).load()

        # Format the output for each document
        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