File size: 2,460 Bytes
0e2dfdd
a094267
7a55fbb
 
9b7b003
5070fda
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0e2dfdd
 
 
 
 
 
 
 
 
 
 
23d26db
 
 
 
ddb29ab
 
 
 
 
 
 
0e2dfdd
7a55fbb
 
 
 
 
 
 
 
 
 
 
 
 
3f0af46
a094267
7a55fbb
0458a9e
7a55fbb
9b7b003
 
 
 
 
 
 
 
 
 
 
 
1f38ca5
 
9b7b003
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
import json
import os

from langchain_community.document_loaders import \
    AssemblyAIAudioTranscriptLoader, WikipediaLoader, YoutubeLoader
from langchain_core.tools import tool


@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) -> int:
    """
    Divide two numbers.

    Args:
        a: numerator
        b: denominator
    """
    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) -> str:
    """
    Search Wikipedia for a query and return maximum 2 results. The results
    are formatted as a JSON string with the source and content of each
    document.

    Args:
        query: The search query."""
    search_docs = WikipediaLoader(
        query=query,
        load_max_docs=2,
    ).load()
    formatted_search_docs = [
        {
            "source": doc.metadata["source"],
            "content": doc.page_content,
        }
        for doc in search_docs
    ]
    return json.dumps({"wiki_results": formatted_search_docs})


@tool
def audio_transcript(
    audiofile_path: str
) -> str:
    """
    Transcribe an audio file to text.

    Args:
        audiofile_path: The path to the audio file to transcribe.
    """
    loader = AssemblyAIAudioTranscriptLoader(
        file_path=os.path.join('data', audiofile_path),
        api_key=os.environ.get("ASSEMBLY_API_KEY"),
    )
    docs = loader.load()
    return docs[0].page_content if docs else "No transcription available."


@tool
def youtube_transcript(
    video_url: str
) -> str:
    """
    Get the transcript of a YouTube video.

    Args:
        video_url: The URL of the YouTube video.
    """
    loader = YoutubeLoader.from_youtube_url(
        youtube_url=video_url,
        add_video_info=False
    )
    docs = loader.load()
    return docs[0].page_content if docs else "No transcript available."