Bandook01's picture
add more tools
3839dec
Raw
History Blame Contribute Delete
3.67 kB
from smolagents import PythonInterpreterTool, tool
import requests
from llama_index.core.agent.workflow import FunctionAgent
from llama_index.core.tools.tool_spec.load_and_search import (
LoadAndSearchToolSpec,
)
from llama_index.tools.wikipedia import WikipediaToolSpec
#from recognizer import predict_fen
from llama_index.readers.youtube_transcript import YoutubeTranscriptReader
from llama_index.readers.file import (
DocxReader,
HWPReader,
PDFReader,
EpubReader,
FlatReader,
HTMLTagReader,
ImageCaptionReader,
ImageReader,
ImageVisionLLMReader,
IPYNBReader,
MarkdownReader,
MboxReader,
PptxReader,
PandasCSVReader,
VideoAudioReader,
UnstructuredReader,
PyMuPDFReader,
ImageTabularChartReader,
XMLReader,
PagedCSVReader,
CSVReader,
RTFReader,
)
def ImageReaderTool(text:str) -> str:
"""Image reader takes image files and parse it for llm to use"""
parser = ImageReader()
file_extractor = {
".jpg": parser,
".jpeg": parser,
".png": parser,
} # Add other image formats as needed
documents = SimpleDirectoryReader(
"./data", file_extractor=file_extractor
).load_data()
return documents
def CSVReaderTool(path: str) -> str:
"""CSV reader takes csv file and parse it for llm to use"""
parser = CSVReader()
file_extractor = {".csv": parser} # Add other CSV formats as needed
documents = SimpleDirectoryReader(
"./data", file_extractor=file_extractor
).load_data()
return documents
def YoutubeTranscript(link: str) -> str:
"""
Get video transcript from youtube link
Args:
link (str): youtube link
Returns:
str: transcript of video to llm
"""
loader = YoutubeTranscriptReader()
documents = loader.load_data(
ytlinks=[link]
)
return documents
wiki_spec = WikipediaToolSpec()
# Get the search wikipedia tool
wiki_tool = wiki_spec.to_tool_list()[1]
""" def GetChessPosition(file_path:str) -> str:
fen = predict_fen("../images/chess_image.png")
return(fen) """
def ReverseTextTool(text: str) -> str:
"""
Reverses a text string character by character.
Args:
text (str): The text to reverse
Returns:
str: The reversed text
"""
return text[::-1]
def RunPythonFileTool(file_path: str) -> str:
"""
Executes a Python script loaded from the specified path using the PythonInterpreterTool.
Args:
file_path (str): The full path to the python (.py) file containing the Python code.
Returns:
str: The output produced by the code execution, or an error message if it fails.
"""
try:
with open(file_path, "r") as f:
code = f.read()
interpreter = PythonInterpreterTool()
result = interpreter.run({"code": code})
return result.get("output", "No output returned.")
except Exception as e:
return f"Execution failed: {e}"
def download_server(url: str, save_path: str) -> str:
"""
Downloads a file from a URL and saves it to the given path.
Args:
url (str): The URL from which to download the file.
save_path (str): The local file path where the downloaded file will be saved.
Returns:
str: A message indicating the result of the download operation.
"""
try:
response = requests.get(url, timeout=30)
response.raise_for_status()
with open(save_path, "wb") as f:
f.write(response.content)
return f"File downloaded to {save_path}"
except Exception as e:
return f"Failed to download: {e}"