File size: 1,182 Bytes
eb08b08
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
from langchain_core.tools import tool
from loguru import logger
from youtube_transcript_api import YouTubeTranscriptApi, FetchedTranscript


@tool("youtube_transcript_tool", parse_docstring=True)
def youtube_transcript(video_id: str) -> str:
    """
    Fetches the transcript of a YouTube video using its video ID.

    The video ID must be provided to successfully fetch the transcript.

    Args:
        video_id (str): The unique identifier of a YouTube video. You can retrieve the video_id from the URL of the video. For example, with the URL https://www.youtube.com/watch?v=12345 the video_id is 12345.

    Returns:
        FetchedTranscript: The transcript of the specified YouTube video.

    Raises:
        Any exceptions related to YouTubeTranscriptApi when a problem
        occurs during fetching the transcript.
    """
    logger.info(f"use youtube_transcript with param: {video_id}")
    transcript = YouTubeTranscriptApi().fetch(video_id).to_raw_data()

    bullet_points = '\n'.join(f"- {entry['text']}" for entry in transcript)

    return bullet_points

if __name__ == "__main__":
    transcript = youtube_transcript.invoke("1htKBjuUWec")
    print(transcript)