shrewd-agent / tools /youtube_transcript.py
Pycrolis
feat(tool): add YouTube transcript tool
eb08b08
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)