Skip to content

Commit

Permalink
feat: added youtube tool (#116)
Browse files Browse the repository at this point in the history
* feat: added youtube tool

* fix: fixed excessive whitespace in patch example prompt
  • Loading branch information
ErikBjare authored Sep 11, 2024
1 parent 78f461c commit ad66992
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 1 deletion.
2 changes: 2 additions & 0 deletions gptme/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from .shell import tool as shell_tool
from .subagent import tool as subagent_tool
from .tmux import tool as tmux_tool
from .youtube import tool as youtube_tool

logger = logging.getLogger(__name__)

Expand All @@ -37,6 +38,7 @@
browser_tool,
gh_tool,
chats_tool,
youtube_tool,
# python tool is loaded last to ensure all functions are registered
get_python_tool,
]
Expand Down
42 changes: 42 additions & 0 deletions gptme/tools/youtube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import logging

from ..llm import summarize
from .base import ToolSpec

logger = logging.getLogger(__name__)

try:
# noreorder
from youtube_transcript_api import YouTubeTranscriptApi # fmt: skip
except ImportError:
YouTubeTranscriptApi = None
logger.warning(
"youtube_transcript_api not available. YouTube tool will be disabled."
)


def get_transcript(video_id: str) -> str:
if not YouTubeTranscriptApi:
return "Error: youtube_transcript_api is not installed."
try:
transcript = YouTubeTranscriptApi.get_transcript(video_id)
return " ".join([entry["text"] for entry in transcript])
except Exception as e:
logger.error(f"Error fetching transcript: {e}")
return f"Error fetching transcript: {e}"


def summarize_transcript(transcript: str) -> str:
return summarize(transcript).content


tool: ToolSpec = ToolSpec(
name="youtube",
desc="Fetch and summarize YouTube video transcripts",
instructions="""
To use this tool, provide a YouTube video ID and specify whether you want the transcript or a summary.
""",
functions=[get_transcript, summarize_transcript],
block_types=["youtube"],
available=bool(YouTubeTranscriptApi),
)
16 changes: 15 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ playwright = {version = "^1.37.0", optional=true}
openai = "^1.0"
anthropic = "^0.34.0"

# tools
youtube_transcript_api = {version = "^0.6.1", optional = true}

# datascience essentials
matplotlib = {version = "*", optional=true}
pandas = {version = "*", optional=true}
Expand Down

0 comments on commit ad66992

Please sign in to comment.