Spaces:
Sleeping
Sleeping
Update tools.py
Browse files
tools.py
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
|
|
|
| 3 |
|
| 4 |
@tool
|
| 5 |
def add(a: int, b: int) -> int:
|
|
@@ -59,4 +60,24 @@ def web_search(query: str) -> str:
|
|
| 59 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 60 |
for doc in search_docs
|
| 61 |
])
|
| 62 |
-
return {"web_results": formatted_search_docs}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from langchain_core.tools import tool
|
| 2 |
from langchain_community.tools.tavily_search import TavilySearchResults
|
| 3 |
+
from youtube_transcript_api import YouTubeTranscriptApi
|
| 4 |
|
| 5 |
@tool
|
| 6 |
def add(a: int, b: int) -> int:
|
|
|
|
| 60 |
f'<Document source="{doc.metadata["source"]}" page="{doc.metadata.get("page", "")}"/>\n{doc.page_content}\n</Document>'
|
| 61 |
for doc in search_docs
|
| 62 |
])
|
| 63 |
+
return {"web_results": formatted_search_docs}
|
| 64 |
+
|
| 65 |
+
@tool
|
| 66 |
+
def fetch_youtube_transcript(video_url: str) -> str:
|
| 67 |
+
"""
|
| 68 |
+
Fetch transcript of a YouTube video by URL.
|
| 69 |
+
Returns plain text if transcript is available.
|
| 70 |
+
"""
|
| 71 |
+
import re
|
| 72 |
+
video_id_match = re.search(r"(?:v=|youtu\.be/)([\w-]+)", video_url)
|
| 73 |
+
if not video_id_match:
|
| 74 |
+
return "Invalid YouTube URL."
|
| 75 |
+
|
| 76 |
+
video_id = video_id_match.group(1)
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
transcript_list = YouTubeTranscriptApi.get_transcript(video_id, languages=['en'])
|
| 80 |
+
transcript_text = " ".join([entry['text'] for entry in transcript_list])
|
| 81 |
+
return transcript_text[:4000] # Limit to fit into context window
|
| 82 |
+
except Exception as e:
|
| 83 |
+
return f"Transcript not available or error: {str(e)}"
|