Skip to content

Youtube search

YoutubeSearchTool

Bases: StructuredTool

StructuredTool that searches videos related to a query.

Source code in libs/gptstonks-multiagents/gptstonks/multiagents/tools/youtube_search.py
class YoutubeSearchTool(StructuredTool):
    """StructuredTool that searches videos related to a query."""

    class YoutubeSearchInput(BaseModel):
        query: str = Field(description="query for the videos to search on Youtube.")

    @classmethod
    def search_videos(cls, query: str, include_description: bool) -> str:
        """Searches videos on Youtube related to a query."""

        s = Search(query)
        # return top URL
        top_video_data = {
            "query": query,
            "top_video": s.results[0].watch_url,
            "title": s.results[0].title,
        }
        if include_description:
            top_video_data["description"] = s.results[0].description
        return json.dumps(top_video_data)

    @classmethod
    def create(
        cls,
        name: str = "YoutubeSearch",
        description: str = "Useful to search Youtube videos",
        return_direct: bool = False,
        include_description: bool = False,
    ) -> StructuredTool:
        return cls.from_function(
            func=partial(cls.search_videos, include_description=include_description),
            name=name,
            description=description,
            args_schema=cls.YoutubeSearchInput,
            return_direct=return_direct,
        )

search_videos(query, include_description) classmethod

Searches videos on Youtube related to a query.

Source code in libs/gptstonks-multiagents/gptstonks/multiagents/tools/youtube_search.py
@classmethod
def search_videos(cls, query: str, include_description: bool) -> str:
    """Searches videos on Youtube related to a query."""

    s = Search(query)
    # return top URL
    top_video_data = {
        "query": query,
        "top_video": s.results[0].watch_url,
        "title": s.results[0].title,
    }
    if include_description:
        top_video_data["description"] = s.results[0].description
    return json.dumps(top_video_data)