File size: 3,645 Bytes
2df4234
 
1b66ea9
 
 
2df4234
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b66ea9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
from linkup import LinkupClient
from dotenv import load_dotenv
from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
from llama_index.readers.youtube_transcript.utils import is_youtube_video
from llama_index.core import Document
import os
load_dotenv()
linkup_client = LinkupClient(api_key=os.getenv("LINKUP_API_KEY"))

async def web_search(query: str) -> dict:
    """Search the web for information about a given topic, or access real-time data through the web_search tool about anything on the internet. Returns a dictionary containing the answer and a list of sources."""
    response = linkup_client.search(
        query=query,
        depth='standard',
        output_type="sourcedAnswer"
    )
    answer = response.answer
    # Format sources as a list of markdown links
    sources = [f"- [{source.name}]({source.url})" for source in response.sources]
    return {"answer": answer, "sources": sources}

# MathTools
class MathTools:
    """A class containing basic mathematical operations."""
    
    def multiply(self, a: float, b: float) -> float:
        """Multiply two numbers.
        Args:
            a: first number
            b: second number
        Returns:
            The product of a and b
        """
        return a * b

    def add(self, a: float, b: float) -> float:
        """Add two numbers.
        
        Args:
            a: first number
            b: second number
        Returns:
            The sum of a and b
        """
        return a + b

    def subtract(self, a: float, b: float) -> float:
        """Subtract two numbers.
        
        Args:
            a: first number
            b: second number
        Returns:
            The difference of a and b
        """
        return a - b

    def divide(self, a: float, b: float) -> float:
        """Divide two numbers.
        
        Args:
            a: dividend
            b: divisor
        Returns:
            The quotient of a divided by b
        Raises:
            ValueError: If b is zero
        """
        if b == 0:
            raise ValueError("Cannot divide by zero.")
        return a / b

    def modulus(self, a: int, b: int) -> int:
        """Get the modulus of two numbers.
        
        Args:
            a: dividend
            b: divisor
        Returns:
            The remainder of a divided by b
        Raises:
            ValueError: If b is zero
        """
        if b == 0:
            raise ValueError("Cannot perform modulus with zero.")
        return a % b

# Create an instance for easy importing
math_tools = MathTools()

#File management tools
class FileManagementTools:
    """A class for interacting with files (csv, jpeg, png, pdf, etc.)"""
    def __init__(self): #TODO: Add proper init
        self.file_path = None
        self.file_type = None
        self.file_name = None
        self.file_size = None
        self.file_content = None
    #TODO: Finish the class with proper source handling and additional file type reading.
    def read_youtube_video(self,urls: list[str]) -> list[Document]:
        """Read a youtube video and return the transcript."""
        valid_urls = [url for url in urls if is_youtube_video(url)]
        
        if not valid_urls:
            raise ValueError("No valid YouTube URLs provided")
        # Load transcripts for valid URLs
        try:
            loader = YoutubeTranscriptReader()
            documents = loader.load_data(ytlinks=valid_urls)
            return documents
        except Exception as e:
            raise ValueError(f"Error loading YouTube transcripts: {e}")
file_management_tools = FileManagementTools()