beitemian's picture
updated tools and req
1b66ea9
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()