from langchain_core.tools import tool import dotenv dotenv.load_dotenv() from langchain_tavily import TavilySearch, TavilyExtract from langchain_openai import ChatOpenAI from langchain_core.messages import HumanMessage from langchain_community.tools.riza.command import ExecPython from langchain_community.tools import BraveSearch from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader from youtube_transcript_api import YouTubeTranscriptApi, TranscriptsDisabled, NoTranscriptFound from typing import Optional import base64 import pandas as pd @tool def describe_image_tool(image_path: str, prompt: str = "Describe the following image:") -> str: """ Describe an image using a vision language model. Args: image_path (str): Path to the image file. prompt (str): Prompt to guide the description. Returns: str: Description of the image. """ print(f"Describing image: {image_path} with prompt: {prompt}") vision_llm = ChatOpenAI(model="gpt-4o") with open(image_path, "rb") as image_file: image_content = image_file.read() image_base64 = base64.b64encode(image_content).decode('utf-8') message = [ HumanMessage( content=[ {"type": "text", "text": prompt}, {"type": "image_url", "image_url": {"url": f"data:image/jpeg;base64,{image_base64}"}} ] ) ] response = vision_llm.invoke(message) return response.content if response else f"[Image description for {image_path} with prompt '{prompt}' not available." @tool def parse_excel_tool(file_path: str) -> str: """ Parse an Excel file and return its content as a string. Args: file_path (str): Path to the Excel file. Returns: str: Content of the Excel file as a string. """ try: df = pd.read_excel(io=file_path) return df.to_string() if not df.empty else "The Excel file is empty." except Exception as e: return f"Error parsing Excel file: {e}" @tool def audio_file_transcriber(file_path: str) -> str : """ Transcribe an audio file Args: file_path (str): the file path of the audio file Returns: str: A transcription of the audio """ loader = AssemblyAIAudioTranscriptLoader(file_path=file_path) docs = loader.load() return docs[0].page_content @tool def get_youtube_transcript(video_url: str, lang: Optional[str] = 'en') -> str: """Extracts and returns the transcript of a YouTube video. Args: video_url (str): The full YouTube video URL. lang (Optional[str]): The language of the transcript. Defaults to 'en'. Returns: str: The full transcript as a string, or a message if not available. """ import re video_id_match = re.search(r"(?:v=|youtu.be/)([\w-]{11})", video_url) if not video_id_match: return "Invalid YouTube URL" video_id = video_id_match.group(1) try: transcript_list = YouTubeTranscriptApi.list_transcripts(video_id) transcript = transcript_list.find_transcript([lang]) transcript_text = " ".join([entry['text'] for entry in transcript.fetch()]) return transcript_text except TranscriptsDisabled: return "Transcript is disabled for this video." except NoTranscriptFound: return f"No transcript found in language: {lang}" except Exception as e: return f"Error retrieving transcript: {str(e)}" brave_web_search = BraveSearch.from_search_kwargs({"max_results": 4}) # search_tool = TavilySearch( # max_results=5, # include_answer="basic", # include_images=True, # include_image_descriptions=True, # search_depth="advanced" # ) webpage_extraction_tool = TavilyExtract( extract_depth="basic", include_images=True ) python_code_interpreter_tool = ExecPython()