sqfoo commited on
Commit
0b0c2eb
·
verified ·
1 Parent(s): 8af254c

Update agent.py

Browse files
Files changed (1) hide show
  1. agent.py +143 -10
agent.py CHANGED
@@ -1,11 +1,12 @@
1
  import os
2
  from dotenv import load_dotenv
3
  from typing import TypedDict, List, Dict, Any, Optional
 
4
  from langgraph.graph import StateGraph, START, END, MessagesState
5
  from langchain.agents import create_tool_calling_agent, ConversationalAgent, AgentExecutor, initialize_agent, create_react_agent
6
  from langchain_google_genai import ChatGoogleGenerativeAI
7
  from langchain_groq import ChatGroq
8
- from langchain_core.tools import tool
9
  from langchain_core.messages import HumanMessage, SystemMessage
10
  from langchain.memory import ConversationBufferMemory
11
  from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
@@ -15,12 +16,12 @@ from langgraph.prebuilt import tools_condition
15
  # 1. Web Browsing
16
  from langchain_community.tools import DuckDuckGoSearchResults
17
  from langchain_community.document_loaders import ImageCaptionLoader
18
- import requests, time
19
  import pandas as pd
20
  from pathlib import Path
21
  from bs4 import BeautifulSoup
22
  from langchain_community.tools import WikipediaQueryRun
23
- from langchain_community.utilities import WikipediaAPIWrapper
24
  from langchain_community.document_loaders import YoutubeLoader
25
  from langchain_community.document_loaders import UnstructuredExcelLoader
26
  from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
@@ -37,9 +38,14 @@ def duckduck_websearch(query: str) -> str:
37
  query: what you want to search
38
  """
39
  try:
40
- search = DuckDuckGoSearchResults()
41
- results = search.invoke(query)
42
- return "\n".join(results)
 
 
 
 
 
43
  except Exception as e:
44
  print(str(e))
45
  print('Try to use request method for duckcudckgo Search')
@@ -301,13 +307,80 @@ class BasicAgent:
301
  If Task ID is included in the question, remember to call the relevant read tools [ie. read_file, excel_read, csv_read, mp3_listen, image_caption]
302
  Note: python_tool is called when the question mentions the term "Python" or any math calculation.
303
  """
304
- self.tools = [duckduck_websearch, serper_websearch, visit_webpage, wiki_search, text_splitter, youtube_transcript, read_file, excel_read, csv_read, mp3_listen, image_caption, run_python]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
305
  # Setup memory
306
  self.memory = ConversationBufferMemory(
307
  memory_key="chat_history",
308
  return_messages=True
309
  )
310
- self.agent = self.__setup__agent__()
311
  # self.prompt = ChatPromptTemplate.from_messages([
312
  # ("system", self.sys_prompt),
313
  # ("human", "{input}")
@@ -337,7 +410,7 @@ class BasicAgent:
337
  fixed_answer = "This is a default answer."
338
 
339
 
340
- max_retries = 3
341
  base_sleep = 1
342
  for attempt in range(max_retries):
343
  try:
@@ -355,7 +428,67 @@ class BasicAgent:
355
  return f"Error processing query after {max_retries} attempts: {str(e)}"
356
  return fixed_answer
357
 
358
- def __setup__agent__(self) -> AgentExecutor:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
359
  PREFIX = """
360
  You are a general AI assistant that can use various tools to answer question. I will ask you a question. Report your thoughts, and finish your answer with the following template:
361
  FINAL ANSWER: [YOUR FINAL ANSWER].
 
1
  import os
2
  from dotenv import load_dotenv
3
  from typing import TypedDict, List, Dict, Any, Optional
4
+ from urllib.parse import urlparse
5
  from langgraph.graph import StateGraph, START, END, MessagesState
6
  from langchain.agents import create_tool_calling_agent, ConversationalAgent, AgentExecutor, initialize_agent, create_react_agent
7
  from langchain_google_genai import ChatGoogleGenerativeAI
8
  from langchain_groq import ChatGroq
9
+ from langchain_core.tools import tool, Tool
10
  from langchain_core.messages import HumanMessage, SystemMessage
11
  from langchain.memory import ConversationBufferMemory
12
  from langchain_core.prompts import ChatPromptTemplate, PromptTemplate
 
16
  # 1. Web Browsing
17
  from langchain_community.tools import DuckDuckGoSearchResults
18
  from langchain_community.document_loaders import ImageCaptionLoader
19
+ import requests, time, yt_dlp
20
  import pandas as pd
21
  from pathlib import Path
22
  from bs4 import BeautifulSoup
23
  from langchain_community.tools import WikipediaQueryRun
24
+ from langchain_community.utilities import WikipediaAPIWrapper, DuckDuckGoSearchAPIWrapper
25
  from langchain_community.document_loaders import YoutubeLoader
26
  from langchain_community.document_loaders import UnstructuredExcelLoader
27
  from langchain_community.document_loaders import AssemblyAIAudioTranscriptLoader
 
38
  query: what you want to search
39
  """
40
  try:
41
+ # search = DuckDuckGoSearchResults()
42
+ # results = search.invoke(query)
43
+ search = search = DuckDuckGoSearchAPIWrapper(max_results=5)
44
+ results = search.run(query)
45
+ if not results or results.strip() == "":
46
+ return "No search results found."
47
+
48
+ return results
49
  except Exception as e:
50
  print(str(e))
51
  print('Try to use request method for duckcudckgo Search')
 
307
  If Task ID is included in the question, remember to call the relevant read tools [ie. read_file, excel_read, csv_read, mp3_listen, image_caption]
308
  Note: python_tool is called when the question mentions the term "Python" or any math calculation.
309
  """
310
+ # self.tools = [duckduck_websearch, serper_websearch, visit_webpage, wiki_search, text_splitter, self._analyze_video, youtube_transcript, read_file, excel_read, csv_read, mp3_listen, image_caption, run_python]
311
+ self.tools = [
312
+ Tool(
313
+ name="duckduck_websearch",
314
+ func=duckduck_websearch,
315
+ description="Search the web for information with DuckDuckGo"
316
+ ),
317
+ Tool(
318
+ name="serper_websearch",
319
+ func=serper_websearch,
320
+ description="Search the web for information with Serper"
321
+ ),
322
+ Tool(
323
+ name="visit_webpage",
324
+ func=visit_webpage,
325
+ description="Directly visit the webpage"
326
+ ),
327
+ Tool(
328
+ name="wiki_search",
329
+ func=wiki_search,
330
+ description="Search the information on Wikipedia"
331
+ ),
332
+ Tool(
333
+ name="text_splitter",
334
+ func=text_splitter,
335
+ description="Split text into chunks"
336
+ ),
337
+ Tool(
338
+ name="analyze_video",
339
+ func=self.analyze_video,
340
+ description+"Analyze YouTube video content directly"
341
+ ),
342
+ Tool(
343
+ name="youtube_transcript",
344
+ func=youtube_transcript,
345
+ description="Fetch the transcript of YouTube video"
346
+ ),
347
+ Tool(
348
+ name="read_file",
349
+ func=read_file,
350
+ description="Read the file content"
351
+ ),
352
+ Tool(
353
+ name="excel_read",
354
+ func=excel_read,
355
+ description="Read the content of Excel file"
356
+ ),
357
+ Tool(
358
+ name="csv_read",
359
+ func=csv_read,
360
+ description="Read the content of CSV file"
361
+ ),
362
+ Tool(
363
+ name='mp3_listen',
364
+ func=mp3_listen,
365
+ description="Listen to the MP3 file"
366
+ ),
367
+ Tool(
368
+ name="image_caption",
369
+ func=image_caption,
370
+ description="Understand the image content"
371
+ ),
372
+ Tool(
373
+ name="run_python",
374
+ func=run_python,
375
+ description="Run Python code"
376
+ )
377
+ ]
378
  # Setup memory
379
  self.memory = ConversationBufferMemory(
380
  memory_key="chat_history",
381
  return_messages=True
382
  )
383
+ self.agent = self.__setup_agent__()
384
  # self.prompt = ChatPromptTemplate.from_messages([
385
  # ("system", self.sys_prompt),
386
  # ("human", "{input}")
 
410
  fixed_answer = "This is a default answer."
411
 
412
 
413
+ max_retries = 5
414
  base_sleep = 1
415
  for attempt in range(max_retries):
416
  try:
 
428
  return f"Error processing query after {max_retries} attempts: {str(e)}"
429
  return fixed_answer
430
 
431
+ @tool
432
+ def _analyze_video(self, url: str) -> str:
433
+ """Analyze video content using Gemini's video understanding capabilities."""
434
+ try:
435
+ # Validate URL
436
+ parsed_url = urlparse(url)
437
+ if not all([parsed_url.scheme, parsed_url.netloc]):
438
+ return "Please provide a valid video URL with http:// or https:// prefix."
439
+
440
+ # Check if it's a YouTube URL
441
+ if 'youtube.com' not in url and 'youtu.be' not in url:
442
+ return "Only YouTube videos are supported at this time."
443
+
444
+ try:
445
+ # Configure yt-dlp with minimal extraction
446
+ ydl_opts = {
447
+ 'quiet': True,
448
+ 'no_warnings': True,
449
+ 'extract_flat': True,
450
+ 'no_playlist': True,
451
+ 'youtube_include_dash_manifest': False
452
+ }
453
+
454
+ with yt_dlp.YoutubeDL(ydl_opts) as ydl:
455
+ try:
456
+ # Try basic info extraction
457
+ info = ydl.extract_info(url, download=False, process=False)
458
+ if not info:
459
+ return "Could not extract video information."
460
+
461
+ title = info.get('title', 'Unknown')
462
+ description = info.get('description', '')
463
+
464
+ # Create a detailed prompt with available metadata
465
+ prompt = f"""Please analyze this YouTube video:
466
+ Title: {title}
467
+ URL: {url}
468
+ Description: {description}
469
+ Please provide a detailed analysis focusing on:
470
+ 1. Main topic and key points from the title and description
471
+ 2. Expected visual elements and scenes
472
+ 3. Overall message or purpose
473
+ 4. Target audience"""
474
+
475
+ # Use the LLM with proper message format
476
+ messages = [HumanMessage(content=prompt)]
477
+ response = self.model.invoke(messages)
478
+ return response.content if hasattr(response, 'content') else str(response)
479
+
480
+ except Exception as e:
481
+ if 'Sign in to confirm' in str(e):
482
+ return "This video requires age verification or sign-in. Please provide a different video URL."
483
+ return f"Error accessing video: {str(e)}"
484
+
485
+ except Exception as e:
486
+ return f"Error extracting video info: {str(e)}"
487
+
488
+ except Exception as e:
489
+ return f"Error analyzing video: {str(e)}"
490
+
491
+ def __setup_agent__(self) -> AgentExecutor:
492
  PREFIX = """
493
  You are a general AI assistant that can use various tools to answer question. I will ask you a question. Report your thoughts, and finish your answer with the following template:
494
  FINAL ANSWER: [YOUR FINAL ANSWER].