zeerafle commited on
Commit
af5aa56
·
1 Parent(s): 483e937

Add YoutubeUnderstandingTool for analyzing YouTube videos

Browse files
Files changed (1) hide show
  1. tools/youtube_understanding.py +60 -0
tools/youtube_understanding.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Any, Optional
2
+ from langchain_core.callbacks import CallbackManagerForToolRun
3
+ from langchain_core.tools import BaseTool
4
+ from langchain_core.tools.base import ArgsSchema
5
+ from pydantic import BaseModel, Field
6
+ from google.genai.types import Tool, ToolCodeExecution, GenerateContentConfig, Content, Part, FileData
7
+ from google import genai
8
+
9
+ class YoutubeUnderstandingInput(BaseModel):
10
+ video_url: str = Field(description="The URL of the YouTube video to understand")
11
+ task: str = Field(description="The task to perform on the video")
12
+
13
+ class YoutubeUnderstandingTool(BaseTool):
14
+ name: str = "youtube_understanding"
15
+ description: str = "Useful for understanding youtube videos"
16
+ args_schema: Optional[ArgsSchema] = YoutubeUnderstandingInput
17
+ client: Any = None
18
+ model_id: str = "gemini-2.0-flash"
19
+ return_direct: bool = False
20
+
21
+ def __init__(self, api_key: Optional[str] = None, **kwargs):
22
+ super().__init__(**kwargs)
23
+ self.client = genai.Client(api_key=api_key)
24
+
25
+ def _run(self, video_url: str, task: str, run_manager: Optional[CallbackManagerForToolRun]=None) -> str:
26
+ """"""
27
+ try:
28
+ response = self.client.models.generate_content(
29
+ model=self.model_id,
30
+ contents=Content(
31
+ parts=[
32
+ Part(
33
+ file_data=FileData(file_uri=video_url)
34
+ ),
35
+ Part(text=task)
36
+ ]
37
+ )
38
+ )
39
+
40
+ return response.text
41
+ except Exception as e:
42
+ return f"Error performing code execution: {str(e)}"
43
+
44
+ async def _arun(self, video_url: str, task: str, run_manager: Optional[CallbackManagerForToolRun]=None) -> str:
45
+ """Run the code execution tool asynchronously."""
46
+ return self._run(video_url, task, run_manager=run_manager.get_sync())
47
+
48
+ # Example usage
49
+ if __name__ == "__main__":
50
+ from dotenv import load_dotenv
51
+
52
+ # Load API key from environment variables
53
+ load_dotenv()
54
+
55
+ # Create the Google Search runnable
56
+ yt_understanding= YoutubeUnderstandingTool()
57
+
58
+ # Run a search query
59
+ result = yt_understanding.invoke({"task": "Examine the video. What does Teal'c say in response to the question \"Isn't that hot?\"", "video_url": "https://www.youtube.com/watch?v=1htKBjuUWec"})
60
+ print(result)