Spaces:
Sleeping
Sleeping
Add reverse_text and analyze_youtube_video tools to agent.py for text manipulation and YouTube video analysis
Browse files
agent.py
CHANGED
|
@@ -11,6 +11,7 @@ from langchain_core.tools import tool
|
|
| 11 |
import requests
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import urllib.parse
|
|
|
|
| 14 |
|
| 15 |
load_dotenv()
|
| 16 |
|
|
@@ -83,6 +84,57 @@ def arxiv_search(query: str) -> str:
|
|
| 83 |
except Exception as e:
|
| 84 |
return f"Error searching Arxiv: {str(e)}"
|
| 85 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# System prompt
|
| 87 |
system_prompt = """You are a highly accurate question-answering assistant. Your task is to provide precise, direct answers to questions.
|
| 88 |
|
|
@@ -117,7 +169,10 @@ Key Rules:
|
|
| 117 |
- For historical dates: Use the most widely accepted date
|
| 118 |
- For scientific terms: Use the standard scientific notation
|
| 119 |
- For geographical locations: Use official names without abbreviations
|
| 120 |
-
- For audio/video questions: Focus on the specific detail requested
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# System message
|
| 123 |
sys_msg = SystemMessage(content=system_prompt)
|
|
@@ -127,6 +182,8 @@ tools = [
|
|
| 127 |
wiki_search,
|
| 128 |
web_search,
|
| 129 |
arxiv_search,
|
|
|
|
|
|
|
| 130 |
]
|
| 131 |
|
| 132 |
def build_graph():
|
|
|
|
| 11 |
import requests
|
| 12 |
from bs4 import BeautifulSoup
|
| 13 |
import urllib.parse
|
| 14 |
+
import re
|
| 15 |
|
| 16 |
load_dotenv()
|
| 17 |
|
|
|
|
| 84 |
except Exception as e:
|
| 85 |
return f"Error searching Arxiv: {str(e)}"
|
| 86 |
|
| 87 |
+
@tool
|
| 88 |
+
def reverse_text(text: str) -> str:
|
| 89 |
+
"""Reverse the given text.
|
| 90 |
+
|
| 91 |
+
Args:
|
| 92 |
+
text: The text to reverse."""
|
| 93 |
+
return text[::-1]
|
| 94 |
+
|
| 95 |
+
@tool
|
| 96 |
+
def analyze_youtube_video(video_url: str) -> str:
|
| 97 |
+
"""Analyze a YouTube video for specific information.
|
| 98 |
+
|
| 99 |
+
Args:
|
| 100 |
+
video_url: The URL of the YouTube video."""
|
| 101 |
+
try:
|
| 102 |
+
# Extract video ID from URL
|
| 103 |
+
video_id = re.search(r'(?:v=|\/)([0-9A-Za-z_-]{11}).*', video_url)
|
| 104 |
+
if not video_id:
|
| 105 |
+
return "Invalid YouTube URL"
|
| 106 |
+
|
| 107 |
+
video_id = video_id.group(1)
|
| 108 |
+
|
| 109 |
+
# Use YouTube API or web search to get video information
|
| 110 |
+
encoded_query = urllib.parse.quote(f"youtube video {video_id} transcript description")
|
| 111 |
+
url = f"https://html.duckduckgo.com/html/?q={encoded_query}"
|
| 112 |
+
|
| 113 |
+
headers = {
|
| 114 |
+
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
|
| 115 |
+
}
|
| 116 |
+
|
| 117 |
+
response = requests.get(url, headers=headers)
|
| 118 |
+
response.raise_for_status()
|
| 119 |
+
|
| 120 |
+
soup = BeautifulSoup(response.text, 'html.parser')
|
| 121 |
+
|
| 122 |
+
results = []
|
| 123 |
+
for result in soup.find_all('div', class_='result__body'):
|
| 124 |
+
title = result.find('h2', class_='result__title')
|
| 125 |
+
snippet = result.find('a', class_='result__snippet')
|
| 126 |
+
|
| 127 |
+
if title and snippet:
|
| 128 |
+
results.append(f"Title: {title.get_text()}\nSnippet: {snippet.get_text()}")
|
| 129 |
+
|
| 130 |
+
if len(results) >= 3:
|
| 131 |
+
break
|
| 132 |
+
|
| 133 |
+
return {"video_results": "\n\n".join(results) if results else "No results found"}
|
| 134 |
+
|
| 135 |
+
except Exception as e:
|
| 136 |
+
return f"Error analyzing YouTube video: {str(e)}"
|
| 137 |
+
|
| 138 |
# System prompt
|
| 139 |
system_prompt = """You are a highly accurate question-answering assistant. Your task is to provide precise, direct answers to questions.
|
| 140 |
|
|
|
|
| 169 |
- For historical dates: Use the most widely accepted date
|
| 170 |
- For scientific terms: Use the standard scientific notation
|
| 171 |
- For geographical locations: Use official names without abbreviations
|
| 172 |
+
- For audio/video questions: Focus on the specific detail requested
|
| 173 |
+
- For reversed text: Use the reverse_text tool to decode
|
| 174 |
+
- For YouTube videos: Use the analyze_youtube_video tool to get information
|
| 175 |
+
- For chess positions: Provide the move in standard algebraic notation"""
|
| 176 |
|
| 177 |
# System message
|
| 178 |
sys_msg = SystemMessage(content=system_prompt)
|
|
|
|
| 182 |
wiki_search,
|
| 183 |
web_search,
|
| 184 |
arxiv_search,
|
| 185 |
+
reverse_text,
|
| 186 |
+
analyze_youtube_video,
|
| 187 |
]
|
| 188 |
|
| 189 |
def build_graph():
|