Chi-AI-Rookie commited on
Commit
7c8e9de
·
1 Parent(s): 9738813

Add the missing duckdukgo function

Browse files
Files changed (2) hide show
  1. agent.py +67 -43
  2. requirements.txt +2 -1
agent.py CHANGED
@@ -1,5 +1,8 @@
1
  from typing import TypedDict, Annotated
2
  import os
 
 
 
3
  from langchain_community.tools import DuckDuckGoSearchRun
4
  from langchain_community.document_loaders import WikipediaLoader, YoutubeLoader
5
  from langchain_community.document_loaders.youtube import TranscriptFormat
@@ -57,49 +60,6 @@ def analyze_youtube_video(video_url: str) -> str:
57
  Returns:
58
  str: The transcript content of the YouTube video
59
  """
60
- # try:
61
- # # Method 1: Try with basic YoutubeLoader first
62
- # try:
63
- # loader = YoutubeLoader.from_youtube_url(
64
- # video_url,
65
- # add_video_info=True,
66
- # language=["en", "en-US", "en-GB"] # Try multiple English variants
67
- # )
68
- # docs = loader.load()
69
-
70
- # if docs:
71
- # content = ""
72
- # for doc in docs:
73
- # title = doc.metadata.get('title', 'Unknown Video')
74
- # author = doc.metadata.get('author', 'Unknown Author')
75
- # length = doc.metadata.get('length', 'Unknown')
76
-
77
- # content += f"Video Title: {title}\n"
78
- # content += f"Author: {author}\n"
79
- # content += f"Length: {length} seconds\n"
80
- # content += f"Transcript:\n{doc.page_content}\n\n"
81
-
82
- # return content
83
- # except Exception as e1:
84
- # print(f"Method 1 failed: {e1}")
85
-
86
- # Method 2: Try without video info
87
- # try:
88
- # loader = YoutubeLoader.from_youtube_url(
89
- # video_url,
90
- # add_video_info=False,
91
- # language=["en"]
92
- # )
93
- # docs = loader.load()
94
-
95
- # if docs:
96
- # content = f"Video URL: {video_url}\n"
97
- # content += f"Transcript:\n{docs[0].page_content}\n\n"
98
- # return content
99
- # except Exception as e2:
100
- # print(f"Method 2 failed: {e2}")
101
-
102
- # # Method 3: Try with chunked format
103
  try:
104
  loader = YoutubeLoader.from_youtube_url(
105
  video_url,
@@ -118,6 +78,70 @@ def analyze_youtube_video(video_url: str) -> str:
118
  except Exception as e:
119
  print(f"Analyze video failed: {e}")
120
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
  # Initialize Langfuse CallbackHandler globally
122
  def get_langfuse_handler():
123
  """Get configured Langfuse handler"""
 
1
  from typing import TypedDict, Annotated
2
  import os
3
+ import requests
4
+ from urllib.parse import urlparse
5
+ from pathlib import Path
6
  from langchain_community.tools import DuckDuckGoSearchRun
7
  from langchain_community.document_loaders import WikipediaLoader, YoutubeLoader
8
  from langchain_community.document_loaders.youtube import TranscriptFormat
 
60
  Returns:
61
  str: The transcript content of the YouTube video
62
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
63
  try:
64
  loader = YoutubeLoader.from_youtube_url(
65
  video_url,
 
78
  except Exception as e:
79
  print(f"Analyze video failed: {e}")
80
 
81
+ # Create file download tool
82
+ @tool
83
+ def download_file(url: str, filename: str = None, download_dir: str = "downloaded_files") -> str:
84
+ """Download a file from a URL and save it locally.
85
+
86
+ Args:
87
+ url: The URL of the file to download
88
+ filename: Optional custom filename. If not provided, will extract from URL
89
+ download_dir: Directory to save the file (default: 'downloaded_files')
90
+
91
+ Returns:
92
+ str: Success message with file path or error message
93
+ """
94
+ try:
95
+ # Create download directory if it doesn't exist
96
+ Path(download_dir).mkdir(exist_ok=True)
97
+
98
+ # Parse URL to get filename if not provided
99
+ if not filename:
100
+ parsed_url = urlparse(url)
101
+ filename = os.path.basename(parsed_url.path)
102
+ if not filename:
103
+ filename = "downloaded_file"
104
+
105
+ # Full file path
106
+ file_path = os.path.join(download_dir, filename)
107
+
108
+ # Download the file
109
+ print(f"Downloading file from: {url}")
110
+ response = requests.get(url, stream=True, timeout=30)
111
+ response.raise_for_status()
112
+
113
+ # Get file size for progress tracking
114
+ total_size = int(response.headers.get('content-length', 0))
115
+
116
+ # Write file in chunks
117
+ with open(file_path, 'wb') as file:
118
+ downloaded = 0
119
+ for chunk in response.iter_content(chunk_size=8192):
120
+ if chunk:
121
+ file.write(chunk)
122
+ downloaded += len(chunk)
123
+
124
+ # Get file info
125
+ file_size = os.path.getsize(file_path)
126
+ file_size_mb = file_size / (1024 * 1024)
127
+
128
+ return f"""File downloaded successfully!
129
+
130
+ File Details:
131
+ - URL: {url}
132
+ - Saved as: {file_path}
133
+ - File size: {file_size_mb:.2f} MB
134
+ - Content type: {response.headers.get('content-type', 'Unknown')}
135
+
136
+ You can now access the file at: {os.path.abspath(file_path)}"""
137
+
138
+ except requests.exceptions.RequestException as e:
139
+ return f"Error downloading file from {url}: Network error - {str(e)}"
140
+ except OSError as e:
141
+ return f"Error saving file: File system error - {str(e)}"
142
+ except Exception as e:
143
+ return f"Error downloading file from {url}: {str(e)}"
144
+
145
  # Initialize Langfuse CallbackHandler globally
146
  def get_langfuse_handler():
147
  """Get configured Langfuse handler"""
requirements.txt CHANGED
@@ -6,4 +6,5 @@ langgraph
6
  langfuse
7
  langchain-openai
8
  youtube-transcript-api
9
- pytube
 
 
6
  langfuse
7
  langchain-openai
8
  youtube-transcript-api
9
+ pytube
10
+ duckduckgo-search