from typing import Any, Optional from smolagents.tools import Tool import duckduckgo_search from googleapiclient.discovery import build import os YOUTUBE_API_KEY=os.environ.get('YOUTUBE_API_KEY') def youtube_search(query): max_results=10 print(f"query -> {query}") return_result = [] youtube = build('youtube', 'v3', developerKey=YOUTUBE_API_KEY) request = youtube.search().list( q=query, part='snippet', maxResults=max_results, type='video' ) response = request.execute() for item in response.get('items', []): print(f"Title: {item['snippet']['title']}") print(f"Video ID: {item['id']['videoId']}") print() return_result.append({'aquery': query, 'titlevideo': item['snippet']['title'], 'videoId': item['id']['videoId']}) return return_result class YoutubeSearchTool(Tool): name = "youtube_search" #description = "Performs a search with youtube API to select a potentially related song of band based on your query and return top X results." description = "Performs a search on youtube to select a potentially related song of band based on your query and return top X results. Never use named parameter use on positional parameters" inputs = {'aquery': {'type': 'string', 'description': 'The search query to perform.'}} output_type = "object" # TODO or Dict or List ????!??? def __init__(self, max_results=10, **kwargs): super().__init__() self.max_results = max_results def forward(self, aquery: str) -> str: results = youtube_search(aquery) if len(results) == 0: raise Exception("No results found! Try a less restrictive/shorter query.") return results